The android-go project aims to provide a platform (namely an SDK) for writing native Android apps in Go programming language. All things here were designed to be less coupled as possible, thus maintaining a great level of flexibility while keeping things simple and easy to use. The approach and tools are different from gomobile, please distinguish them apart.
Package android provides Go bindings for the Android NDK API. They've been
automatically generated by cgogen using the
official NDK headers from android-23
platform. Keep in mind that different NDK
platforms may implement different sets of API available, thus some of features
used by this binding may not be available in older versions of platform. But I
tested with the android-21
toolchain and got no issues. Some files, like
android/sensors.go for example, have been written by hand to expose some
features that CGO does not handle well.
This package allows to write code that works directly with NDK API, bypassing all the CGO bookkeeping and boilerplate bloat in your code. But safety is strictly advised.
Example usages: app/queue.go, example, example-egl
Package app implements a NativeActivity glue layer required to properly
handle the startup process and the native activity events. Import this package
into your Go application to make it Android-compatible. Some pieces required
for a proper main.main
trampoline have been borrowed from gomobile, the
absolute minimum to get this stuff invokable as a native activity. Most of the
code in this package provides wrappers around NativeActivity event callbacks.
Example usages: example, example-egl
Package egl provides Go bindings for EGL API. They've been automatically
generated by cgogen using the official NDK headers
from android-23
platform. All functions have their reference to the offical
Khronos documentation. Some files, like egl/errors.go for example, have been
written by hand to expose some features that would make it more idiomatic in the
Go world.
Example of usage in conjuction with the android package: example-egl.
Package gles provides Go bindings for the OpenGL ES v1 API. They've been
automatically generated by cgogen using the
official NDK headers from android-23
platform. All functions have their
reference to the offical documentation.
Example of usage in conjuction with the android package: example-egl.
Package gles2 provides Go bindings for the OpenGL ES v2 API. They've been
automatically generated by cgogen using the
official NDK headers from android-23
platform. All functions have their
reference to the offical documentation.
Refer the example links to get more info about them.
There are two beautiful examples. The first example is a template showing how to create and build an application using the absolute minimum of code and boilerplate. It also shows the primitives of an activity and how to handle activity events, there is no visual part, so be ready to read the lines in the ADB Shell. If this one works correctly on your device, my congratulations. If not, please open an issue.
And example-egl leverages all three packages together: android, egl and of course gles to create quite a rich app that animates its color based on the accelerometer values. It also reads input events such as key events and multitouch motion events (with pressure, if supported by the device), you can check these events in the ADB Shell. Check out the video of the expected behaviour:
TL;DR it's a three wheel bicycle.
GoMobile has different aims and motivation with a lot of implications. First of all, it has been grown as a bootstrap-script. That's it: a tool with a lot of hardcoded logic that helped to popularize Go on mobile platforms. Still provides a simple way to start building Android and iOS apps in no time, without diving deeply into any of the platform-specific complications. It provides a lot of wrappers and helpers that abstract all the platform-related boilerplate and logic away, making your apps look the same on any platform: Android, iOS, PC (Linux/OS X/Windows), the latter is for debug purposes. It also downloads a stripped prebuilt SDK/NDK for you.. oh wait!!1
So, for Android developent, gomobile defaults to r10
SDK with android-15
stripped-down NDK, there is a lot of code written to automate this process and
there is no way to override this environment, even by rewriting a lot of
gomobile bootstrapping code. It's a great mess. Too much time wasted trying to
switch to the r12-beta1
SDK and the android-23
NDK platform. Instead of providing
a good document how to build the Go-based apps and incorporate them into any of
the existing development process, the Gomobile project introduces tons of
hardcoded scripts in Go. Nuff said, even the article that shows the principle
behind the main.main
trampoline does it by reverse engineering an APK produced by
Gomobile. Any self-documenting code? Hell no.
Feel free to share bugs, I expect a lot of weird cases. Please also share this project in social networks so more people would know how to write Go apps for Android that are not limited. Follow me at @xlab_dev.
All the code except when stated otherwise is licensed under the MIT license.