Develop Android/iOS app using golang

download Develop Android/iOS app using golang

of 65

  • date post

    07-Jan-2017
  • Category

    Software

  • view

    4.914
  • download

    4

Embed Size (px)

Transcript of Develop Android/iOS app using golang

  • HelloMobile.goSeongJae Park

  • 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/.

    http://creativecommons.org/licenses/by-sa/3.0/http://creativecommons.org/licenses/by-sa/3.0/http://creativecommons.org/licenses/by-sa/3.0/

  • This slides were presented duringGopherCon Korea 2015

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

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

  • Nice To Meet You

    SeongJae Park

    sj38.park@gmail.com

    golang newbie programmer

  • 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

  • Warning

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

    Dont try this at office carelessly Its an experiment yet

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

  • 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

  • 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, efficient,

    and free mobile application?

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

  • Golang on Mobile

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

    Though its 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

    https://github.com/golang/mobilehttps://github.com/golang/mobile

  • 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

  • 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

    https://github.com/golang/mobile/tree/40f92f7d9f9092072ea68570fd6f5725a5cbcd6bhttps://github.com/golang/mobile/tree/25faf494e186f45f8d9704fb53a6c6c555366d20

  • 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

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

  • 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

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

  • 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

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

  • Pure Golang Mobile AppNO JAVA!

  • 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

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

  • 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

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

  • Example Code

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

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

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

  • 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

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

  • 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)

  • 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)

  • NativeActivity

    NativeActivity only application doesnt need JAVA

    (mobile/example/basic/AndroidManifest.xml)

  • NativeActivity

    NativeActivity only application doesnt need JAVA

    (mobile/example/basic/AndroidManifest.xml)

  • NativeActivity

    NativeActivity only application doesnt need JAVA

    (mobile/example/basic/AndroidManifest.xml)

  • 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)

  • 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)

  • 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)

  • Example Code (1.5 based)

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

    gomobile command do build

    $ tree. main.go

    0 directories, 1 file

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

  • 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

    $ tree. main.go

    0 directories, 1 file

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

  • 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

    https://github.co