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.

Android is going to Go! Android and Golang

3,534 views

Published on

I recently talked in Droidcon about building using Go with Android
We talked about what is Go? And why it's so awesome? And how can we use it with Android?
On this session we've learnt a little bit about Go, and how can we use it with Android- about the use cases, and the secret
tricks to use it with mobile applications.

Published in: Technology
  • Be the first to comment

Android is going to Go! Android and Golang

  1. 1. Android is going to Go! Android and golang
  2. 2. Who are you? @AlmogBaku on github 1. A serial entrepreneur 2. Co-Founder & CTO @ Rimoto 3. Developer for 12 years 4. GitHub addicted. 5. Blog about entrepreneurship and development: www.AlmogBaku.com
  3. 3. What is Rimoto? Rimoto enable apps to sponsor their user’s mobile-data, and to became accessible for international travellers, regardless their data-plan, boosting their engagement.
  4. 4. What are we going to talk about? 1. What is Go? 2. How can we use Go with Android? 3. When is it useful?
  5. 5. Disclaimer You wanna know more? Google it! Google tip: use the keyword “golang”
  6. 6. Who heard about Go?
  7. 7. Why use Go, and what is it?! • New modern language (since 2009) • Super fast (native) compiling • Concurrent • Performant • Garbage collected • Standard libraries
  8. 8. Hello world package main import "fmt" func main() { fmt.Println("Hello DroidCon!") } RUN
  9. 9. Goroutines func boring() { for i := 0; i < 10; i++ { sayIt(i) } } func sayIt(i int) { time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) fmt.Println("I'm saying ", i) } RUN
  10. 10. Goroutines func not_boring() { for i := 0; i < 10; i++ { go sayIt(i) } time.Sleep(2 * time.Second) } func sayIt(i int) { time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) fmt.Println("I'm saying ", i) } RUN
  11. 11. Goroutines syncing / WaitGroup func not_boring_at_all() { wg := sync.WaitGroup{} wg.Add(10) for i := 0; i < 10; i++ { go sayIt(i, &wg) } wg.Wait() } func sayIt(i int, wg *sync.WaitGroup) { defer wg.Done() time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) fmt.Println("I'm saying ", i) } RUN
  12. 12. Goroutines communications / Channels package main import "fmt" func main() { c := make(chan string) for i := 0; i < 5; i++ { go func() { c <- "ping" }() } for i := 0; i < 5; i++ { go func() { c <- "pong" }() } for i := 0; i < 10; i++ { fmt.Println(<-c) } } RUN
  13. 13. Standard libraries package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there!") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
  14. 14. Go Mobile A tool for using Golang for native mobile apps easily!
  15. 15. Caution The Go Mobile project is experimental. Use this at your own risk.
  16. 16. Why? 1. Full-stack development 2. Write a single cross-platform Go library 3. Bring a simple and modern language and development tooling to mobile 4. Enjoy Go benefits of native, faster, and much more concurrent code
  17. 17. How? Native Apps ● Write the whole app in Go (with OpenGL) ● Use Go packages for graphics, event handling, audio, etc. ● When Native App UI is not required (i.e. games) SDK Apps ● Write common functionality in Go, as a library
  18. 18. Native Go Apps • 100% Go app • Multi platform: Android, iOS and Desktop • GUI with OpenGL
  19. 19. Behind the scenes NativeActivity Android App
  20. 20. Gomobile A tool that automate this process – Toolchain installation – Creating NativeActivity – Attach the Go runtime to the app – Bind the App binary to the app – Multi Architecture build – Cross platform(Android/iOS) build $ gomobile build golang.org/x/mobile/example/basic $ gomobile install golang.org/x/mobile/example/basic
  21. 21. DEMO
  22. 22. SDKs • Build the app natively with Java/Swift/Obj. C • Write a simple regular Go library • Reuse libraries across platforms and projects Common library iOS Android Backend service A Backend service B 3rd party
  23. 23. Behind the scenes JNI Android App Go shared binary rpc
  24. 24. Behind the scenes package mypkg func Hello() (string, error) { return "Gopher", nil } public abstract class Mypkg { public static String hello() throws Exception { ... } } Go Library: Java API:
  25. 25. Gomobile A tool that automate this process – Multi Architecture build – Build as shared library – Automatically generate the binding for Java/Swift/Obj. C – Bundle everything to an .aar – Cross platform(Android/iOS) build $ gomobile bind -target=android golang.org/x/mobile/example/bind/hello
  26. 26. DEMO
  27. 27. Disadvantages Nothing is perfect.. • .aar includes all architecture binaries (increase size) • go binaries are currently statically linked • Go as a native app is lack of many sensors and integrations with the Android/iOS APIs • The communication between the Platform and the go binary is not free
  28. 28. Questions?
  29. 29. Thanks. @AlmogBaku

×