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:
But it’s not just a toy because of the following advantages:
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.
hg
adb
Firstly some environment variables need to be set (better be added to .bashrc
or .zshrc
etc.):
123 |
|
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:
12345 |
|
Now check out $GOBIN
folder, three commands go
godoc
gofmt
(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 $GOOS
and $GOARCH
combinations to support amd64
386
and arm
instruction sets all in one install base. See “Installing Go from source” for detail.
Now create Go source files for testing. Let’s start with the most favorite ‘Hello world!’ one.
12345 |
|
Compile it and generate executable:
1 |
|
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:
123 |
|
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.
The following code example is borrowed from rarnu’s blog post and modified a bit.
1234567891011121314151617181920212223242526272829303132 |
|
Handling JSON data returned from web services in Go is pretty neat and clean, right? Build it as above:
1234 |
|
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 net
package.
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:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
|
To run a native executable (whether written in Go or not) in Android apps:
go-exec
in the assets
folder.getAssets().open("go-exec")
to get an InputStream
./data/data/app-package-name/
, where the app has access to write files and make it executable.CommandResult.runCommand("/system/bin/chmod 744 /data/data/app-package-name/go-exec", 0)
/data/data/app-package-name/go-exec
using 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.
联系客服