Go is the language from Google and Android is the mobile OS from Google. The bond between them are just born with. So there should be no surprise that we can use Go to write Android programs, though some important restricts for now:
- There is no Android SDK for Go, so no system API nor GUI for Go program on Android.
- Go doesn’t support JNI for now. So Go programs have to be compiled as separate executables, wrapped as assets in apk and called within Java based apps.
- Maybe cross-compiling is really hard. We cannot build Android applications which use Cgo(C bridge for Go) on our desktop computer for now, as shown below.
But it’s not just a toy because of the following advantages:
- Go is fast and all C programmers just like it.
- Go language is bundled with high quality libraries, especially suitable for special tasks.
- Go is from Google! It does have a future! Well, at least let’s hope so.
Go is a very young language, thus there’s few guides or tutorials out there. But it also makes earlier investion provide more return. My friend and colleague @rarnu also likes the idea of writing Android program using Go and has written a short tutorial. I’d like to rewrite it here and fill all lost details which he was too lazy to include.
Preparing Go Language
Firstly some environment variables need to be set (better be added to
GOROOT should be the folder where Go source is in and
GOBIN should be in the system
PATH. Double check them and issue these commands in terminal:
Now check out
$GOBIN folder, three commands
go command line tools) and a folder
linux_arm(cross-compiling tools for Linux/ARM targets) should be in there. Line 2-5 can be run multiple times by setting different
$GOARCH combinations to support
arm instruction sets all in one install base. See “Installing Go from source” for detail.
Hello Android from Go!
Now create Go source files for testing. Let’s start with the most favorite ‘Hello world!’ one.
Compile it and generate executable:
The last command will generate an executable named
hello-arm which targets Linux/ARM system (i.e. Android). Now connect to an debuggable Android device and issue in the same folder:
If all things go right it will generate output as expected. Using the same routine showed above, all code examples on Go’s homepage can be verified working well on Android devices.
Real World Example
The following code example is borrowed from rarnu’s blog post and modified a bit.
Handling JSON data returned from web services in Go is pretty neat and clean, right? Build it as above:
When running it on my Galaxy Nexus (ARM v7 dual-core 1.2 GHz Cortex-A9, Android 4.0.2 with kernel 3.0.8) it crashed on line 14 - the
http.Get call failed. Because of some issues in the cross-compiling system, all calls using Cgo will cause failure like that, at least for now. Unfortunately the
net package depend on Cgo and nearly all useful applications need
Call Go Code in Android Apps
Exactly the same as calling any native executables in Android apps, which fully explained in this guide. I’ll list a more general and accurate solution below for convenience.
The following Java class shows how to invoke a process in Android apps (actually it is used by Google itself), which is the base of the following guide:
To run a native executable (whether written in Go or not) in Android apps:
- Include the binary
getAssets().open("go-exec")to get an
- Write it to
/data/data/app-package-name/, where the app has access to write files and make it executable.
- Make it executable using the code above, i.e.
CommandResult.runCommand("/system/bin/chmod 744 /data/data/app-package-name/go-exec", 0)
/data/data/app-package-name/go-execusing the code above.
Go 1 is now live! Article updated to match changes in
Go 1. And the
Go 1 release note is a must read.