GoLang on Android [Root]
If you’ve ever wanted to compile Go on Android this is the guide for you!
Why use Go on Android?
My primary reason for compiling Go on Android was to use a tool that was written in Go. I wanted to embed that binary in my custom ROM I was building from the AOSP source.
Prerequisites
To follow along with this tutorial you will need a rooted Android device, some knowledge of the Go language is helpful although not required. Some knowledge of ADB would be helpful but again it isn’t required since I’ll walk you through what the commands do.
Set up a development environment
The first thing you will need to do is get a copy of Go, if you don’t already have it installed you can get it here. Installation instructions are included once you select your appropriate download package for your system.
Hello Android from Go
The first thing we will need to do is write some Go, for the interest of keeping things simple we will make a simple hello world style program.
package mainimport "fmt"func main() {
fmt.Printf("Hello Android!\n")
}
Once you have the program written let’s save it, I’ll be saving it as hello_android.go in a folder called golang_android. We will want to run it on our host computer to ensure that it works before compiling it for Android. One of the very cool things about Go is that you can run it without compiling so let’s do that now, don’t forget to replace the path to the source with your path, I can run mine by calling the file because I’m working from within the same folder.
go run hello_android.go
Compile Go
To be able to run the program on Android, we’ll need to compile the source for ARM. I’m using an ARM to compile because I will be using a Snapdragon-based device to run the binary. If you’re using a different architecture like x86 you will need to use a different architecture.
Also, something to keep in mind, these commands are different on Windows vs Linux/macOS. I’ve outlined how to set the environment variables on either system below.
Set the environment variables(Windows)
set GOARCH=arm
set GOOS=linux
Set the environment variables(macOS, Linux)
GOARCH=arm
GOOS=linux
Build
go build hello_android.go
Push the Files
We will need to push the newly created hello_android binary to our Android device. I have chosen to use ADB to accomplish this and although there are many other ways to get the files on there I find this one is the easiest.
You can push the file anywhere on the device but for the sake of this guide, I will be putting it in /data/local/tmp. Don’t forget to change the source location to the location of the file that was created in the build step earlier.
adb push "E:\Files\2019\08-August\08-13-2019 - GoLang on Android\golang_android\hello_android" /data/local/tmp
Finally, to use it we will need to change the permissions to allow execution.
adb shell “chmod +x /data/local/tmp/hello_android"
We can now run the binary with the following command.
adb shell “/data/local/tmp/hello_android”
GoLang Text Adventure Game in Android
I think the next step is obviously to build a text-adventure game written in Go and run it on Android, so let’s do it! You can download a text-adventure game from GitHub here. This is GopherVenture created by Jacknet, let’s run it on our Android device. We will follow the same procedure as before and compile it and push it to the device.
Since all the steps are listed above I’m only going to cover what I did on Windows to compile the source. I cloned the repository on my computer to E:\sourcetree\gopherventure so you will need to change this to wherever you cloned the repo. Also the device I’m using is a Snapdragon 820 dev kit so when I enter the shell it shows the device as msm8996, yours will almost certainly show something different.
cd E:\sourcetree\gopherventure
set GOARCH=arm
set GOOS=linux
go build adventure.go
adb push ./adventure /data/local/tmp
adb shell "chmod +x /data/local/tmp/adventure"
adb shell
msm8996:/ #/data/local/tmp/adventure
Awesome!
There you have it, you just ran Go code on an Android device. Thanks for taking the time to read this, I hope you found it useful. If you did please consider applauding, sharing or subscribing for more content. If you have any questions or comments don’t hesitate to drop me a line.
If you enjoyed this guide you might like my similar guides Python on Android and NodeJS on Android.
About the author
My name is Aaron Watson and I’m a Software Engineer specializing in embedded Android development based out of Clarenville, Canada. If you’d like to get in touch here is my Email, LinkedIn, GitHub and website.