Go on Android

A preview

DroidCon

20 Sep 2014

David Crawshaw

Google

A brief introduction to Go

2

What is Go

Go is a general purpose programming language.

Born out of frustration with C++ and Java:
- Slow builds
- Too much complexity

Go is fast and simple.

More at: go.dev/talks/2012/splash.article

3

Five years later

Go is not driven by a platform.
Sinks or swims on its own merits.

Many users, e.g.
SoundCloud, Docker, Secret, The New York Times

"The cloud programming language."

4

Beyond Cloud

Go has found other uses.

Popular on embedded linux systems.
PayPal's Beacon hardware is powered by Go.

Large-scale data analysis.

For many of us, it replaces python/perl/ruby.

Where else can we use Go?
What about phones and tablets?

5

Go on Android

Android UI programming needs lots of Java APIs.
My first experiment was using these from Go.

It did not work.
Using Java APIs in Go is writing Java using Go syntax.

"You can write FORTRAN in any language"

So does it ever make sense to use Go on Android?
Yes, for portability.

6

Portability

Lots of apps are written for more than just Android.
Some apps start elsewhere and never make it to Android.

:-(

Today, developers solve platform portability with C++.
We can do better.

7

Two ways to use Go

1. Write libraries in Go, use them from Java apps. Or Objective-C/Swift apps.

2. Write apps entirely in Go, restricted to a set of common APIs across platforms.

8

Go libraries for apps

9

Cross language interfaces

Java is a silo.

To use another language, you need JNI.
JNI is tricky, buggy, painful.
It keeps Java programmers away from many good things.

So, no JNI.

Instead, we have a tool for that: gobind
It generates Java interfaces for you.

golang.org/s/gobind

10

gobind basics

package hi

import "fmt"

func Hello(name string) {
    fmt.Println("Hello, %s!\n", name)
}

Use gobind on package hi to generate Go helper code and a Java interface:

package go;

public abstract class Hi {
    public static final void Hello(String name) { .. }
}

Invoke from Java:

Hi.Hello("DroidCon")
11

gobind features

Today, gobind supports many basic Go types, structs, and callbacks.

When finished, gobind will support all Go types.

Go's simplicity makes language bindings simple.
For C++, SWIG has many hard-to-use features.

With Go we get configuration-free language bindings.
SWIG without the .swig files.

12

All-Go apps

13

NDK-style interfaces

Go will have common libraries:

In general: if it works on the NDK and iOS, it works in Go.

14

Games

The primary target for pure Go apps is games.

Better control over over allocation means fewer
garbage collector problems.

Unlikely language for high-budget 3D engines.
But lots of games can be written in Go.

But we are building a 2D sprite package.

15

Status: 2014

Android OS support will be built into the Go runtime in the December 1.4 release.

First version available from the go.mobile subrepository of

Sprite library will be in early testing.

Setup will still be a little trickier than I want, but it will work.

16

Demo

17

Roadmap: mid-2015

The plan is iOS support will be in the Go runtime July 1.5 release.

The same OpenGL bindings and touch events package will work. The gobind tool will generate Objective-C/Swift bindings.

Sprite will be ready for 2D games.

18

Questions

19

Backup slides

20

Go compared to Java

type Point struct {
    X float64
    Y float64
}

type Points []Point

More at: go.dev/talks/2014/go4java.slide

21

Thank you

DroidCon

20 Sep 2014

David Crawshaw

Google

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)