神刀安全网

Android go – a platform for writing native Android apps in Go

android-go Android go - a platform for writing native Android apps in Go

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 fromgomobile, please distinguish them apart.

Project structure

android

Package android provides Go bindings for the Android NDK API. They’ve been automatically generated by a tool not yet announced (sorry :)) 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, likeandroid/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

app

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

egl

Package egl provides Go bindings for EGL API. They’ve been automatically generated by a tool not yet announced (sorry :)) using the official NDK headers from android-23 platform. All functions have their reference to the offical Khronos documentation. Some files, likeegl/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.

gles

Package gles provides Go bindings for the OpenGL ES v1 API. They’ve been automatically generated by a tool not yet announced (sorry :)) 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.

gles2

Package gles2 provides Go bindings for the OpenGL ES v2 API. They’ve been automatically generated by a tool not yet announced (sorry :)) using the official NDK headers from android-23 platform. All functions have their reference to the offical documentation.

Examples

Refer the example links to get more info about them.

There are two beautiful examples. The firstexample 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.

Andexample-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:

Android go - a platform for writing native Android apps in Go

On existing tools or why not just use Gomobile

TL;DRit’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.

Contributing

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 .

License

All the code except when stated otherwise is licensed under the MIT license.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Android go – a platform for writing native Android apps in Go

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址