Go on Android
GothamGo
15 Nov 2014
David Crawshaw
GothamGo
15 Nov 2014
David Crawshaw
This talk was presented at GothamGo in New York City, November 2014.
2
The goal is to bring Go to Android and iOS,
starting with Android.
This is new territory for Go and a well-established
ecosystem. A lot of experimentation is necessary.
The coming 1.4 release can build binaries for Android OS.
With the mobile
subrepository and the Android SDK/NDK, it can:
.so
files for linking into Android Appsgobind
tool, for calling Go from Java: golang.org/s/gobindPackages for cross-device apps:
6There's more than one way to build an Android App.
None of them
We are working on this for Go 1.5.
Until it is done, using Go on Android requires some bravery.
7SDK Apps
Write your Android UI in Java.
Write your iOS UI in Objective-C/Swift.
Write your logic in Go.
Share the Go using interfaces generated by gobind
.
NDK Apps
Games.
Use OpenGL or the coming 2D sprite package to write to the screen.
Everything is written in Go.
Portable APIs, just Go. This is a complete app:
package main import ( "golang.org/x/mobile/app" "golang.org/x/mobile/app/debug" "golang.org/x/mobile/gl" ) func main() { app.Run(app.Callbacks{ Draw: draw, }) } func draw() { gl.ClearColor(1, 0, 0, 1) // RGBA value used to clear buffer: red gl.Clear(gl.COLOR_BUFFER_BIT) debug.DrawFPS() }
Developing with portable APIs means starting from very little.
It is a lot of work to get going and limits access to device features.
But it also frees us from device-specific build systems.
The app package includes Mac/X11 shims for starting as normal programs.
We can write Apps anywhere we can write Go.
(Windows coming soon.)
10package main import ( "fmt" "golang.org/x/mobile/app" "golang.org/x/mobile/event" "golang.org/x/mobile/gl" ) func main() { app.Run(app.Callbacks{ Draw: func() { gl.ClearColor(0, 0, 1, 1) // blue gl.Clear(gl.COLOR_BUFFER_BIT) }, Touch: func(e event.Touch) { fmt.Println(e) }, }) }
We are building a 2D rendering and compositing package.
It renders a scene graph. A scene starts with a root
*sprite.Node
, which can have child nodes.
Each node may have an affine transform and texture.
Rendering is done in a depth-first traversal of the scene,
with transforms applied relative to the parent.
Composition is done in OpenGL.
12GothamGo
15 Nov 2014