Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
HelloAndroid.go
SeongJae Park <sj38.park@gmail.com>
This work by SeongJae Park is licensed under the Creative
Commons Attribution-ShareAlike 3.0 Unported License. To
view a c...
This slides were presented during
3rd GDG Korea Android Conference
(http://event.android.gdg.kr/3rd-GKAC/)
The talk video ...
Nice To Meet You
SeongJae Park
sj38.park@gmail.com
golang newbie programmer
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/21...
Warning
● This speech could be useless for you
○ This is just for fun
● Don’t try this at office
○ The code is not stable ...
golang: Programming Language
● For simple, reliable, and efficient software.
http://blog.golang.org/5years/gophers5th.jpg
golang: Programming Language
● For simple, reliable, and efficient software.
● Could it be used for simple, reliable, and
...
Golang and Android
● Golang supports Android from v1.4
○ Though it’s still unstable
Golang and Android
● Golang supports Android from v1.4
○ Though it’s still unstable
● https://github.com/golang/mobile
○ P...
Goal of This Speak
● Showing how we can use golang on Android
○ By exploring example code
Goal of This Speak
● Showing how we can use golang on Android
○ By exploring example code
● Just for fun, rather than prof...
Pre-requisites
● Basic development environment(vim, git,
gcc, java, …)
http://www.theprospect.net/wp-content/uploads/2014/...
Pre-requisites
● Basic development environment(vim, git,
gcc, java, …)
● Android SDK & NDK
http://www.theprospect.net/wp-c...
Pre-requisites
● Basic development environment(vim, git,
gcc, java, …)
● Android SDK & NDK
● Golang 1.4 or higher cross-co...
Pure Golang Android App
NO JAVA!
Main Idea: Use NDK
● c / c++ only apk is available
using NativeActivity
○ Golang be compiled to native binary, too. Why no...
Main Idea: Use NDK
● c / c++ only apk is available
using NativeActivity
○ Golang be compiled to native binary, too. Why no...
Main Idea: Use NDK
● c / c++ only apk is available
using NativeActivity
○ Golang be compiled to native binary, too. Why no...
Example Code
https://github.
com/golang/mobile/tree/master/example/basic
Example Code
https://github.
com/golang/mobile/tree/master/example/basic
$ tree
.
├── all.bash
├── all.bat
├── AndroidMani...
main.go: Register Callbacks
Register callbacks from golang entrypoint
func main() {
app.Run(app.Callbacks{
Start: start,
S...
main.go: Use OpenGL
func draw() {
gl.ClearColor(1, 0, 0, 1)
...
green += 0.01
if green > 1 {
green = 0
}
gl.Uniform4f(colo...
NativeActivity
NativeActivity only application doesn’t need
JAVA
<application android:label="Basic" android:hasCode="false...
NativeActivity
NativeActivity only application doesn’t need
JAVA
<application android:label="Basic" android:hasCode="false...
NativeActivity
NativeActivity only application doesn’t need
JAVA
<application android:label="Basic" android:hasCode="false...
Build Process
Build golang code into ELF shared object for
ARM
mkdir -p jni/armeabi
CGO_ENABLED=1 GOOS=android GOARCH=arm ...
Build Process
Build golang code into ELF shared object for
ARM
NDK to add the so file
mkdir -p jni/armeabi
CGO_ENABLED=1 G...
Build Process
Build golang code into ELF shared object for
ARM
NDK to add the so file
SDK to build apk file
mkdir -p jni/a...
Pure Golang Android App
Pros: No more JAVA! Yay!!!
Cons: Should I learn OpenGL to show a cat?
Golang as a Library
Cooperate Java and Golang
Java and C language connected via JNI
Main Idea: Use JNI-like way
Java
CPP
JNI
Main Idea: Use JNI-like way
Java and C language connected via JNI
C language and Golang connected via cgo
Java
CPP
GO
JNI ...
Main Idea: Use JNI-like way
Java and C language connected via JNI
C language and Golang connected via cgo
...But, JNI and ...
Main Idea: Use JNI-like way
Java and C language connected via JNI
C language and Golang connected via cgo
...But, JNI and ...
Example Code
https://github.
com/golang/mobile/tree/
master/example/libhello
Example Code
https://github.
com/golang/mobile/tree/
master/example/libhello
$ tree
.
├── all.bash
├── all.bat
├── Android...
Callee in Go
Golang code is implementing Hello() function
func Hello(name string) {
fmt.Printf("Hello, %s!n", name)
}
(mob...
Caller in JAVA
Java code is calling Golang function, Hello()
protected void onCreate(Bundle savedInstanceState) {
super.on...
gobind
generate language bindings that make it
possible to call Go code and pass objects from
Java
go install golang.org/x...
gobind: Generated .java
Provides wrapper function for golang calling
code
public static void Hello(String name) {
go.Seq _...
gobind: Generated .go
Provides proxy and registering for the exported
function
func proxy_Hello(out, in *seq.Buffer) {
par...
Golang as a Library
Pros: JAVA for UI, Golang for background
(Looks efficient enough)
Golang as a Library
Pros: JAVA for UI, Golang for background
(Looks efficient enough)
Cons: bind/seq is not so efficient, ...
Inter-Process
Communication
Philosophy of Unix
Main Idea: Android is a Linux
http://www.kitguru.net/wp-content/uploads/2012/03/linux-android.png
Android is a variant of ...
Main Idea: Android is a Linux
http://www.kitguru.net/wp-content/uploads/2012/03/linux-android.png
Android is a variant of ...
Main Idea: Android is a Linux
http://www.kitguru.net/wp-content/uploads/2012/03/linux-android.png
Android is a variant of ...
Example Code
https://github.com/sjp38/goOnAndroid
https://github.com/sjp38/goOnAndroidFA
Example Code
https://github.com/sjp38/goOnAndroid
https://github.com/sjp38/goOnAndroidFA
Were demonstrated by live-coding ...
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux
2. Include the binary in assets/ of Android app
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux
2. Include the binary in assets/ of Android app...
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux
2. Include the binary in assets/ of Android app...
Golang Process on Android: Plan
1. Cross compile Go program as ARM / Linux
2. Include the binary in assets/ of Android app...
Go bin Loading
Load golang program from assets to private dir
private void copyGoBinary() {
String dstFile = getBaseContex...
Execute Go process
Spawn new process for the program and
communicates using stdio
ProcessBuilder pb = new ProcessBuilder()...
Inter Process Communication
Pros: Just normal unix way
Inter Process Communication
Pros: Just normal unix way
Golang team is using this for Camlistore
(https://github.com/camlis...
Inter Process Communication
Pros: Just normal unix way
Golang team using this for Camlistore
(https://github.com/camlistor...
Summary
You can use Golang for Android
though it’s not stable yet
Just for fun
This work by SeongJae Park is licensed under the
Creative Commons Attribution-ShareAlike 3.0 Unported
License. To view a c...
Upcoming SlideShare
Loading in …5
×

Develop Android app using Golang

30,110 views

Published on

Shows how go code could be used for Android application

Published in: Technology

Develop Android app using Golang

  1. 1. HelloAndroid.go SeongJae Park <sj38.park@gmail.com>
  2. 2. 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/.
  3. 3. This slides were presented during 3rd 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
  4. 4. Nice To Meet You SeongJae Park sj38.park@gmail.com golang newbie programmer
  5. 5. 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
  6. 6. 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
  7. 7. golang: Programming Language ● For simple, reliable, and efficient software. http://blog.golang.org/5years/gophers5th.jpg
  8. 8. 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
  9. 9. Golang and Android ● Golang supports Android from v1.4 ○ Though it’s still unstable
  10. 10. 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
  11. 11. Goal of This Speak ● Showing how we can use golang on Android ○ By exploring example code
  12. 12. Goal of This Speak ● Showing how we can use golang on Android ○ By exploring example code ● Just for fun, rather than profit
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. Pure Golang Android App NO JAVA!
  17. 17. Main Idea: Use NDK ● c / c++ only apk is available using NativeActivity ○ Golang be compiled to native binary, too. Why not? http://www.android.pk/images/android-ndk.jpg
  18. 18. Main Idea: Use NDK ● c / c++ only apk is available using 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
  19. 19. Main Idea: Use NDK ● c / c++ only apk is available using 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
  20. 20. Example Code https://github. com/golang/mobile/tree/master/example/basic
  21. 21. 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
  22. 22. 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)
  23. 23. main.go: Use OpenGL func draw() { gl.ClearColor(1, 0, 0, 1) ... green += 0.01 if green > 1 { green = 0 } gl.Uniform4f(color, 0, green, 0, 1) ... debug.DrawFPS() } (mobile/example/basic/main.go)
  24. 24. 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)
  25. 25. 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)
  26. 26. 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)
  27. 27. Build Process Build golang code into ELF shared object for ARM mkdir -p jni/armeabi CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-shared" -o jni/armeabi/libbasic.so . ndk-build NDK_DEBUG=1 ant debug (mobile/example/basic/make.bash)
  28. 28. Build Process Build golang code into ELF shared object for ARM NDK to add the so file mkdir -p jni/armeabi CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-shared" -o jni/armeabi/libbasic.so . ndk-build NDK_DEBUG=1 ant debug (mobile/example/basic/make.bash)
  29. 29. Build Process Build golang code into ELF shared object for ARM NDK to add the so file SDK to build apk file mkdir -p jni/armeabi CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-shared" -o jni/armeabi/libbasic.so . ndk-build NDK_DEBUG=1 ant debug (mobile/example/basic/make.bash)
  30. 30. Pure Golang Android App Pros: No more JAVA! Yay!!! Cons: Should I learn OpenGL to show a cat?
  31. 31. Golang as a Library Cooperate Java and Golang
  32. 32. Java and C language connected via JNI Main Idea: Use JNI-like way Java CPP JNI
  33. 33. Main Idea: Use JNI-like way Java and C language connected via JNI C language and Golang connected via cgo Java CPP GO JNI CGO
  34. 34. Main Idea: Use JNI-like way Java and C language connected via JNI C language and Golang connected via cgo ...But, JNI and then cgo looks tedious Java CPP GO JNI CGO
  35. 35. Main Idea: Use JNI-like way Java and C language connected via JNI C language and Golang connected via cgo ...But, JNI and then cgo looks tedious Golang supports Java-Golang bind Java CPP GO JNI CGO bind/seq
  36. 36. Example Code https://github. com/golang/mobile/tree/ master/example/libhello
  37. 37. 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
  38. 38. 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)
  39. 39. 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)
  40. 40. 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 gobind -lang=java github.com/libhello/hi > src/go/hi/Hi.java
  41. 41. 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)
  42. 42. 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)
  43. 43. Golang as a Library Pros: JAVA for UI, Golang for background (Looks efficient enough)
  44. 44. Golang as a Library Pros: JAVA for UI, Golang for background (Looks efficient enough) Cons: bind/seq is not so efficient, yet
  45. 45. Inter-Process Communication Philosophy of Unix
  46. 46. 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 ARM x86 Androids exist, though...
  47. 47. 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 ARM x86 Androids exist, though... Go supports ARM & Linux Officially with static-linking
  48. 48. 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 ARM x86 Androids exist, though... Go supports ARM & Linux Officially with static-linking Remember the philosophy of Unix
  49. 49. Example Code https://github.com/sjp38/goOnAndroid https://github.com/sjp38/goOnAndroidFA
  50. 50. Example Code https://github.com/sjp38/goOnAndroid https://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/)
  51. 51. Golang Process on Android: Plan 1. Cross compile Go program as ARM / Linux
  52. 52. Golang Process on Android: Plan 1. Cross compile Go program as ARM / Linux 2. Include the binary in assets/ of Android app
  53. 53. Golang Process on Android: Plan 1. Cross compile Go program as ARM / Linux 2. Include the binary in assets/ of Android app 3. Copy the binary in private space of the app
  54. 54. Golang Process on Android: Plan 1. Cross compile Go program as ARM / Linux 2. Include the binary in assets/ of Android app 3. Copy the binary in private space of the app 4. 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
  55. 55. Golang Process on Android: Plan 1. Cross compile Go program as ARM / Linux 2. Include the binary in assets/ of Android app 3. Copy the binary in private space of the app 4. Give execute permission to the binary 5. Execute it /data/data/com.example.goRunner/files # ls -al -rwxrwxrwx u0_a55 u0_a55 4512840 2014-11-28 17:45 gobin
  56. 56. 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) { } }
  57. 57. 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();
  58. 58. Inter Process Communication Pros: Just normal unix way
  59. 59. Inter Process Communication Pros: Just normal unix way Golang team is using this for Camlistore (https://github.com/camlistore/camlistore)
  60. 60. Inter Process Communication Pros: Just normal unix way Golang team using this for Camlistore (https://github.com/camlistore/camlistore) Cons: Hacky, a little
  61. 61. Summary You can use Golang for Android though it’s not stable yet Just for fun
  62. 62. 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/.

×