Download - Develop Android/iOS app using golang

Transcript
Page 1: Develop Android/iOS app using golang

HelloMobile.goSeongJae Park <[email protected]>

Page 2: Develop Android/iOS app using golang

This work by SeongJae Park is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To

view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.

Page 3: Develop Android/iOS app using golang

This slides were presented duringGopherCon Korea 2015

(https://plus.google.com/u/0/events/c4b79ocq4k9ac11bpb5b2govkes)

Page 4: Develop Android/iOS app using golang

Nice To Meet You

SeongJae Park

[email protected]

golang newbie programmer

Page 5: Develop Android/iOS app using golang

Warning

● This speech could be useless for you○ The speaker is doing this just for fun

https://github.com/golang/mobile/raw/master/doc/caution.png

Page 6: Develop Android/iOS app using golang

Warning

● This speech could be useless for you○ The speaker is doing this just for fun

● Don’t try this at office carelessly○ It’s an experiment yet

https://github.com/golang/mobile/raw/master/doc/caution.png

Page 7: Develop Android/iOS app using golang

Java is Dangerous, Now

● Oracle may take Java away from Android● Swift is open source now, but...● We need alternatives

http://img.talkandroid.com/uploads/2011/10/OracleGoogle.jpg

Page 8: Develop Android/iOS app using golang

golang: Programming Language

● For simple, reliable, and efficient software.

http://blog.golang.org/5years/gophers5th.jpg

Page 9: Develop Android/iOS app using golang

golang: Programming Language

● For simple, reliable, and efficient software.● Could it be used for simple, reliable, efficient,

and free mobile application?

http://blog.golang.org/5years/gophers5th.jpg

Page 10: Develop Android/iOS app using golang

Golang on Mobile

● Golang supports Android from v1.4● Golang supports iOS from v1.5

○ Though it’s still in experimental stage, there were many improvements especially in tools

● https://github.com/golang/mobile○ Repo of packages and tools for Go on Mobile

Page 11: Develop Android/iOS app using golang

Goal of This Speak

● Showing how we can use golang on Mobile○ Focus on Android, rather than iOS

(Speaker has no iPhone…)○ By exploring example code

Page 12: Develop Android/iOS app using golang

Goal of This Speak

● Showing how we can use golang on Mobile○ Focus on Android, rather than iOS

(Speaker has no iPhone…)○ By exploring example code

● We will explore code based on go1.4, go1.5○ Code based on go1.4 shows naked face of build

process■ based on go1.4: https://github.com/golang/mobile/tree/40f92f7d9f9092072ea68570fd6f5725a5cbcd6b

■ based on go1.5: https://github.com/golang/mobile/tree/25faf494e186f45f8d9704fb53a6c6c555366d20

Page 13: Develop Android/iOS app using golang

Pre-requisites

● Basic development environment(vim, git, gcc, java, gradle, …)

http://www.theprospect.net/wp-content/uploads/2014/02/one-does-not-simply-skip-a-step.jpg

Page 14: Develop Android/iOS app using golang

Pre-requisites

● Basic development environment(vim, git, gcc, java, gradle, …)

● Android SDK & NDK

http://www.theprospect.net/wp-content/uploads/2014/02/one-does-not-simply-skip-a-step.jpg

Page 15: Develop Android/iOS app using golang

Pre-requisites

● Basic development environment(vim, git, gcc, java, gradle, …)

● Android SDK & NDK● Golang 1.4 cross-compiled for GOOS=android

or higher versions

http://www.theprospect.net/wp-content/uploads/2014/02/one-does-not-simply-skip-a-step.jpg

Page 16: Develop Android/iOS app using golang

Pure Golang Mobile AppNO JAVA!

Page 17: Develop Android/iOS app using golang

Main Idea: NDK

● c / c++ only apk is availableusing NativeActivity○ Golang is a compiled language, too. Why not?

http://www.android.pk/images/android-ndk.jpg

Page 18: Develop Android/iOS app using golang

Main Idea: NDK

● c / c++ only apk is availableusing NativeActivity○ Golang is a compiled language, too. Why not?

Plan is as below:● Build golang program as .so file

○ ELF shared object● Process events(draw, touch, …) via OpenGL● Build NativeActivity apk using NDK / SDK

http://www.android.pk/images/android-ndk.jpg

Page 20: Develop Android/iOS app using golang

Example Code (1.4 based)

https://github.com/golang/mobile/tree/40f92f7d9f9092072ea68570fd6f5725a5cbcd6b/example/basic

$ tree.├── all.bash├── all.bat├── AndroidManifest.xml├── build.xml├── jni│ └── Android.mk├── main.go├── make.bash└── make.bat

1 directory, 8 files

Page 21: Develop Android/iOS app using golang

main.go: Register Callbacks

Register callbacks from golang entrypoint(After 1.5, programming model changed a little)

func main() {

app.Run(app.Callbacks{

Start: start,

Stop: stop,

Draw: draw,

Touch: touch,

})

}

(mobile/example/basic/main.go)

Page 22: Develop Android/iOS app using golang

main.go: Use OpenGL

func draw() {gl.ClearColor(1, 0, 0, 1)...green += 0.01if green > 1 {

green = 0}gl.Uniform4f(color, 0, green, 0, 1)...debug.DrawFPS()

}

(mobile/example/basic/main.go)

Page 23: Develop Android/iOS app using golang

NativeActivity

NativeActivity only application doesn’t need JAVA

<application android:label="Basic" android:hasCode="false"><activity android:name="android.app.NativeActivity"

android:label="Basic"android:configChanges="orientation|keyboardHidden"><meta-data android:name="android.app.lib_name" android:value="basic" /><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity></application>

(mobile/example/basic/AndroidManifest.xml)

Page 24: Develop Android/iOS app using golang

NativeActivity

NativeActivity only application doesn’t need JAVA

<application android:label="Basic" android:hasCode="false"><activity android:name="android.app.NativeActivity"

android:label="Basic"android:configChanges="orientation|keyboardHidden"><meta-data android:name="android.app.lib_name" android:value="basic" /><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity></application>

(mobile/example/basic/AndroidManifest.xml)

Page 25: Develop Android/iOS app using golang

NativeActivity

NativeActivity only application doesn’t need JAVA

<application android:label="Basic" android:hasCode="false"><activity android:name="android.app.NativeActivity"

android:label="Basic"android:configChanges="orientation|keyboardHidden"><meta-data android:name="android.app.lib_name" android:value="basic" /><intent-filter>

<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity></application>

(mobile/example/basic/AndroidManifest.xml)

Page 26: Develop Android/iOS app using golang

Build Process

Build golang code into ELF shared object for ARM

mkdir -p jni/armeabiCGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \

go build -ldflags="-shared" -o jni/armeabi/libbasic.so .ndk-build NDK_DEBUG=1ant debug

(mobile/example/basic/make.bash)

Page 27: Develop Android/iOS app using golang

Build Process

Build golang code into ELF shared object for ARMNDK to add the so file

mkdir -p jni/armeabiCGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \

go build -ldflags="-shared" -o jni/armeabi/libbasic.so .ndk-build NDK_DEBUG=1ant debug

(mobile/example/basic/make.bash)

Page 28: Develop Android/iOS app using golang

Build Process

Build golang code into ELF shared object for ARMNDK to add the so fileSDK to build apk file

mkdir -p jni/armeabiCGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 \

go build -ldflags="-shared" -o jni/armeabi/libbasic.so .ndk-build NDK_DEBUG=1ant debug

(mobile/example/basic/make.bash)

Page 31: Develop Android/iOS app using golang

Example Code (1.5 based)

https://github.com/golang/mobile/tree/25faf494e186f45f8d9704fb53a6c6c555366d20/example/basic

gomobile command do build$ go get golang.org/x/mobile/cmd/gomobile

$ gomobile init # install ndk if needed

$ tree.└── main.go

0 directories, 1 file

Page 32: Develop Android/iOS app using golang

Example Code (1.5 based)

https://github.com/golang/mobile/tree/25faf494e186f45f8d9704fb53a6c6c555366d20/example/basic

gomobile command do build$ go get golang.org/x/mobile/cmd/gomobile

$ gomobile init # install ndk if needed

$ gomobile build -target=android golang.org/x/mobile/example/basic

$ tree.└── main.go

0 directories, 1 file

Page 33: Develop Android/iOS app using golang

Pure Golang Android App

Pros: No more JAVA! Yay!!!

Cons: Should I learn OpenGL to show a cat?

Page 34: Develop Android/iOS app using golang

Golang as a LibraryCooperate Java and Golang

Page 35: Develop Android/iOS app using golang

Main Idea: Gives binding

Java and C language connected via JNI

Java

CJNI

Page 36: Develop Android/iOS app using golang

Main Idea: Gives binding

Java and C language connected via JNIC language and Golang connected via cgo

Java

C GOJNI

CGO

Page 37: Develop Android/iOS app using golang

Main Idea: Gives binding

Java and C language connected via JNIC language and Golang connected via cgoObjC(Objective C) is a super-set of C language

Java

C GOJNI

CGOObjC

Page 38: Develop Android/iOS app using golang

Main Idea: Gives binding

Java and C language connected via JNIC language and Golang connected via cgoObjC(Objective C) is a super-set of C language

Golang supports Java/ObjC-Golang bind(Uses Cgo internally)

Java

C GOJNI

CGO

bind

ObjC

bind

Page 40: Develop Android/iOS app using golang

Example Code (1.4 based)

https://github.com/golang/mobile/tree/40f92f7d9f9092072ea68570fd6f5725a5cbcd6b/example/libhello

$ tree.├── all.bash├── all.bat├── AndroidManifest.xml├── build.xml├── hi│ ├── go_hi│ │ └── go_hi.go│ └── hi.go├── main.go├── make.bash├── make.bat├── README└── src ├── com │ └── example │ └── hello │ └── MainActivity.java └── go └── hi └── Hi.java

8 directories, 12 files

Page 41: Develop Android/iOS app using golang

Callee in Go

Golang code is implementing Hello() function

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

}

(mobile/example/libhello/hi/hi.go)

Page 42: Develop Android/iOS app using golang

Caller in JAVA

Java code is calling Golang function, Hello()

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Go.init(getApplicationContext()); Hi.Hello("world"); }

(mobile/example/libhello/src/com/example/hello/MainActivity.java)

Page 43: Develop Android/iOS app using golang

gobind

generate language bindings that make it possible to call Go code and pass objects from Java

$ go install golang.org/x/mobile/cmd/gobind$ gobind -lang=go github.com/libhello/hi > hi/go_hi/go_hi.go # stub$ gobind -lang=java github.com/libhello/hi > src/go/hi/Hi.java # proxy

Page 44: Develop Android/iOS app using golang

gobind: Generated Proxy java Code

Provides wrapper function for golang calling code

public static void Hello(String name) { go.Seq _in = new go.Seq(); go.Seq _out = new go.Seq(); _in.writeUTF16(name); Seq.send(DESCRIPTOR, CALL_Hello, _in, _out); }

private static final int CALL_Hello = 1; private static final String DESCRIPTOR = "hi";

(mobile/example/libhello/src/go/hi/Hi.java)

Page 45: Develop Android/iOS app using golang

gobind: Generated Stub go Code

Provides proxy and registering for the exported function

func proxy_Hello(out, in *seq.Buffer) {param_name := in.ReadUTF16()hi.Hello(param_name)

}

func init() {seq.Register("hi", 1, proxy_Hello)

}

(mobile/example/libhello/hi/go_hi/go_hi.go)

Page 46: Develop Android/iOS app using golang

Example Code (1.5 based)

https://github.com/golang/mobile/tree/25faf494e186f45f8d9704fb53a6c6c555366d20/example/bind

gomobile do binding automatically

$ gomobile bind \

-target=android \golang.org/x/mobile/example/bind/hello

$ tree.├── android│ ├── README ├── ...│ └── settings.gradle├── hello│ └── hello.go└── ios ├── bind │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ │ ├── LaunchScreen.xib │ │ └── Main.storyboard │ ├── Info.plist │ ├── main.m │ ├── ViewController.h │ └── ViewController.m ├── bind.xcodeproj │ └── project.pbxproj └── README

Page 47: Develop Android/iOS app using golang

Golang as a Library

Pros: JAVA for UI, Golang for background (Looks efficient enough)

Page 48: Develop Android/iOS app using golang

Golang as a Library

Pros: JAVA for UI, Golang for background (Looks efficient enough)

Cons: Binding supports only subset of Go types

Page 49: Develop Android/iOS app using golang

Inter-Process Communication

Philosophy of Unix

Page 50: Develop Android/iOS app using golang

Main Idea: Android is a Linux

http://www.kitguru.net/wp-content/uploads/2012/03/linux-android.png

Android is a variant of Linux system with ARMx86 Androids exist, though...

Page 51: Develop Android/iOS app using golang

Main Idea: Android is a Linux

http://www.kitguru.net/wp-content/uploads/2012/03/linux-android.png

Android is a variant of Linux system with ARMx86 Androids exist, though...

Go supports ARM & Linux Officiallywith static-linking

Page 52: Develop Android/iOS app using golang

Main Idea: Android is a Linux

http://www.kitguru.net/wp-content/uploads/2012/03/linux-android.png

Android is a variant of Linux system with ARMx86 Androids exist, though...

Go supports ARM & Linux Officiallywith static-linking

Remember the philosophy of Unix

Not tested for iOS

Page 53: Develop Android/iOS app using golang

Example Code

https://github.com/sjp38/goOnAndroidhttps://github.com/sjp38/goOnAndroidFA

Page 55: Develop Android/iOS app using golang

Golang Process on Android: Plan

1. Cross compile Go program as ARM / Linux

Page 56: Develop Android/iOS app using golang

Golang Process on Android: Plan

1. Cross compile Go program as ARM / Linux2. Include the binary in assets/ of Android app

Page 57: Develop Android/iOS app using golang

Golang Process on Android: Plan

1. Cross compile Go program as ARM / Linux2. Include the binary in assets/ of Android app3. Copy the binary in private space of the app

Page 58: Develop Android/iOS app using golang

Golang Process on Android: Plan

1. Cross compile Go program as ARM / Linux2. Include the binary in assets/ of Android app3. Copy the binary in private space of the app4. Give execute permission to the binary

/data/data/com.example.goRunner/files # ls -al-rwxrwxrwx u0_a55 u0_a55 4512840 2014-11-28 17:45 gobin

Page 59: Develop Android/iOS app using golang

Golang Process on Android: Plan

1. Cross compile Go program as ARM / Linux2. Include the binary in assets/ of Android app3. Copy the binary in private space of the app4. Give execute permission to the binary5. Execute it

/data/data/com.example.goRunner/files # ls -al-rwxrwxrwx u0_a55 u0_a55 4512840 2014-11-28 17:45 gobin

Page 60: Develop Android/iOS app using golang

Go bin Loading

Load golang program from assets to private dir

private void copyGoBinary() { String dstFile = getBaseContext().getFilesDir().getAbsolutePath() + "/verChecker.bin"; try { InputStream is = getAssets().open("go.bin"); FileOutputStream fos = getBaseContext().openFileOutput( "verChecker.bin", MODE_PRIVATE); byte[] buf = new byte[8192]; int offset; while ((offset = is.read(buf)) > 0) { fos.write(buf, 0, offset); } Runtime.getRuntime().exec("chmod 0777 " + dstFile); } catch (IOException e) { } }

Page 61: Develop Android/iOS app using golang

Execute Go process

Spawn new process for the program and communicates using stdio

ProcessBuilder pb = new ProcessBuilder(); pb.command(goBinPath()); pb.redirectErrorStream(false); goProcess = pb.start(); new CopyToAndroidLogThread("stderr",

goProcess.getErrorStream()) .start();

Page 62: Develop Android/iOS app using golang

Inter Process Communication

Pros: Just normal unix way

Page 63: Develop Android/iOS app using golang

Inter Process Communication

Pros: Just normal unix way Golang team is using this for Camlistore (https://github.com/camlistore/camlistore)

Page 64: Develop Android/iOS app using golang

Inter Process Communication

Pros: Just normal unix way Golang team is using this for Camlistore (https://github.com/camlistore/camlistore)

Cons: Hacky, a little x/mobile is comfortable enough, now Not sure whether it works on iOS

Page 65: Develop Android/iOS app using golang

Summary

● Go can run on Mobile○ As a native application,○ as a library,○ or, as a process

● Though it’s an experiment yet, it’s fairly comfortable