Develop Android app using Golang
-
Upload
seongjae-park -
Category
Technology
-
view
2.391 -
download
10
Transcript of Develop Android app using Golang
![Page 1: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/1.jpg)
HelloAndroid.goSeongJae Park <[email protected]>
![Page 2: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/2.jpg)
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 app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/3.jpg)
This slides were presented during3rd GDG Korea Android Conference
(http://event.android.gdg.kr/3rd-GKAC/)
The talk video is available at: https://www.youtube.com/watch?v=vMZFjDipaK8&index=2&list=PL_WJkTbDHdBl5QXy6N_bMMBYlKLna5RER
![Page 5: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/5.jpg)
Warning
● This speech could be useless for you○ This is just for fun
http://m.c.lnkd.licdn.com/mpr/mpr/p/4/005/095/091/210ae8c.jpg
![Page 6: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/6.jpg)
Warning
● This speech could be useless for you○ This is just for fun
● Don’t try this at office○ The code is not stable yet
http://m.c.lnkd.licdn.com/mpr/mpr/p/4/005/095/091/210ae8c.jpg
![Page 7: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/7.jpg)
golang: Programming Language
● For simple, reliable, and efficient software.
http://blog.golang.org/5years/gophers5th.jpg
![Page 8: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/8.jpg)
golang: Programming Language
● For simple, reliable, and efficient software.● Could it be used for simple, reliable, and
efficient Android application?
http://blog.golang.org/5years/gophers5th.jpg
![Page 9: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/9.jpg)
Golang and Android
● Golang supports Android from v1.4○ Though it’s still unstable
![Page 10: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/10.jpg)
Golang and Android
● Golang supports Android from v1.4○ Though it’s still unstable
● https://github.com/golang/mobile○ Packages and build tools for using Go on Android
![Page 11: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/11.jpg)
Goal of This Speak
● Showing how we can use golang on Android○ By exploring example code
![Page 12: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/12.jpg)
Goal of This Speak
● Showing how we can use golang on Android○ By exploring example code
● Just for fun, rather than profit
![Page 13: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/13.jpg)
Pre-requisites
● Basic development environment(vim, git, gcc, java, …)
http://www.theprospect.net/wp-content/uploads/2014/02/one-does-not-simply-skip-a-step.jpg
![Page 14: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/14.jpg)
Pre-requisites
● Basic development environment(vim, git, gcc, java, …)
● Android SDK & NDK
http://www.theprospect.net/wp-content/uploads/2014/02/one-does-not-simply-skip-a-step.jpg
![Page 15: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/15.jpg)
Pre-requisites
● Basic development environment(vim, git, gcc, java, …)
● Android SDK & NDK● Golang 1.4 or higher cross-compiled for
GOOS=android
http://www.theprospect.net/wp-content/uploads/2014/02/one-does-not-simply-skip-a-step.jpg
![Page 16: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/16.jpg)
Pure Golang Android AppNO JAVA!
![Page 17: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/17.jpg)
Main Idea: Use NDK
● c / c++ only apk is availableusing NativeActivity○ Golang be compiled to native binary, too. Why not?
http://www.android.pk/images/android-ndk.jpg
![Page 18: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/18.jpg)
Main Idea: Use NDK
● c / c++ only apk is availableusing NativeActivity○ Golang be compiled to native binary, too. Why not?
Plan is...● Build golang program as .so file
○ ELF shared object● Implement every callbacks using OpenGL
http://www.android.pk/images/android-ndk.jpg
![Page 19: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/19.jpg)
Main Idea: Use NDK
● c / c++ only apk is availableusing NativeActivity○ Golang be compiled to native binary, too. Why not?
Plan is...● Build golang program as .so file
○ ELF shared object● Implement every callbacks using OpenGL● Build NativeActivity apk using NDK / SDK
http://www.android.pk/images/android-ndk.jpg
![Page 20: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/20.jpg)
Example Code
https://github.com/golang/mobile/tree/master/example/basic
![Page 21: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/21.jpg)
Example Code
https://github.com/golang/mobile/tree/master/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 22: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/22.jpg)
main.go: Register Callbacks
Register callbacks from golang entrypoint
func main() {
app.Run(app.Callbacks{
Start: start,
Stop: stop,
Draw: draw,
Touch: touch,
})
}
(mobile/example/basic/main.go)
![Page 23: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/23.jpg)
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 24: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/24.jpg)
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 app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/25.jpg)
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 app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/26.jpg)
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 27: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/27.jpg)
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 28: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/28.jpg)
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 29: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/29.jpg)
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 30: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/30.jpg)
Pure Golang Android App
Pros: No more JAVA! Yay!!!
Cons: Should I learn OpenGL to show a cat?
![Page 31: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/31.jpg)
Golang as a LibraryCooperate Java and Golang
![Page 32: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/32.jpg)
Java and C language connected via JNI
Main Idea: Use JNI-like way
Java CPPJNI
![Page 33: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/33.jpg)
Main Idea: Use JNI-like way
Java and C language connected via JNIC language and Golang connected via cgo
Java CPP GOJNI CGO
![Page 34: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/34.jpg)
Main Idea: Use JNI-like way
Java and C language connected via JNIC language and Golang connected via cgo
...But, JNI and then cgo looks tedious
Java CPP GOJNI CGO
![Page 35: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/35.jpg)
Main Idea: Use JNI-like way
Java and C language connected via JNIC language and Golang connected via cgo
...But, JNI and then cgo looks tedious
Golang supports Java-Golang bind
Java CPP GOJNI CGO
bind/seq
![Page 36: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/36.jpg)
Example Code
https://github.com/golang/mobile/tree/master/example/libhello
![Page 37: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/37.jpg)
Example Code
https://github.com/golang/mobile/tree/master/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 38: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/38.jpg)
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 39: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/39.jpg)
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 40: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/40.jpg)
gobind
generate language bindings that make it possible to call Go code and pass objects from Java
go install golang.org/x/mobile/cmd/gobindgobind -lang=go github.com/libhello/hi > hi/go_hi/go_hi.gogobind -lang=java github.com/libhello/hi > src/go/hi/Hi.java
![Page 41: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/41.jpg)
gobind: Generated .java
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 42: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/42.jpg)
gobind: Generated .go
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 43: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/43.jpg)
Golang as a Library
Pros: JAVA for UI, Golang for background (Looks efficient enough)
![Page 44: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/44.jpg)
Golang as a Library
Pros: JAVA for UI, Golang for background (Looks efficient enough)
Cons: bind/seq is not so efficient, yet
![Page 45: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/45.jpg)
Inter-Process Communication
Philosophy of Unix
![Page 46: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/46.jpg)
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 47: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/47.jpg)
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 48: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/48.jpg)
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
![Page 49: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/49.jpg)
Example Code
https://github.com/sjp38/goOnAndroidhttps://github.com/sjp38/goOnAndroidFA
![Page 50: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/50.jpg)
Example Code
https://github.com/sjp38/goOnAndroidhttps://github.com/sjp38/goOnAndroidFA
Were demonstrated by live-coding from GDG Korea DevFair 2014 (http://devfair2014.gdg.kr/) and GDG Golang Seoul Meetup 2015 (https://developers.google.com/events/5381849181323264/)
![Page 51: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/51.jpg)
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux
![Page 52: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/52.jpg)
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux2. Include the binary in assets/ of Android app
![Page 53: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/53.jpg)
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 54: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/54.jpg)
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 55: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/55.jpg)
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 56: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/56.jpg)
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 57: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/57.jpg)
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 58: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/58.jpg)
Inter Process Communication
Pros: Just normal unix way
![Page 59: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/59.jpg)
Inter Process Communication
Pros: Just normal unix way Golang team is using this for Camlistore (https://github.com/camlistore/camlistore)
![Page 60: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/60.jpg)
Inter Process Communication
Pros: Just normal unix way Golang team using this for Camlistore (https://github.com/camlistore/camlistore)
Cons: Hacky, a little
![Page 61: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/61.jpg)
Summary
You can use Golang for Androidthough it’s not stable yetJust for fun
![Page 62: Develop Android app using Golang](https://reader036.fdocuments.us/reader036/viewer/2022082201/55a68d6b1a28abc57d8b46ff/html5/thumbnails/62.jpg)
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/.