android is going to go! android and golang
TRANSCRIPT
![Page 1: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/1.jpg)
Android is going to Go!Android and golang
![Page 2: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/2.jpg)
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
![Page 3: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/3.jpg)
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.
![Page 4: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/4.jpg)
What are we going to talk about?
1. What is Go?
2. How can we use Go with Android?
3. When is it useful?
![Page 5: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/5.jpg)
Disclaimer
You wanna know more? Google it!
Google tip: use the keyword “golang”
![Page 6: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/6.jpg)
Who heard about Go?
![Page 7: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/7.jpg)
Why use Go, and what is it?!
• New modern language (since 2009)
• Super fast (native) compiling
• Concurrent
• Performant
• Garbage collected
• Standard libraries
![Page 8: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/8.jpg)
Hello worldpackage main
import "fmt"
func main() { fmt.Println("Hello DroidCon!")}
RUN
![Page 9: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/9.jpg)
Goroutinesfunc 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
![Page 10: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/10.jpg)
Goroutinesfunc 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
![Page 11: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/11.jpg)
Goroutines syncing / WaitGroupfunc 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
![Page 12: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/12.jpg)
Goroutines communications / Channelspackage 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
![Page 13: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/13.jpg)
Standard librariespackage 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)}
![Page 14: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/14.jpg)
Go Mobile
A tool for using Golang for native mobile apps easily!
![Page 15: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/15.jpg)
Caution
The Go Mobile project is experimental. Use this at your own risk.
![Page 16: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/16.jpg)
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
![Page 17: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/17.jpg)
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
![Page 18: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/18.jpg)
Native Go Apps
• 100% Go app
• Multi platform: Android, iOS and Desktop
• GUI with OpenGL
![Page 19: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/19.jpg)
Behind the scenes
NativeActivityAndroid
App
![Page 20: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/20.jpg)
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
![Page 21: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/21.jpg)
DEMO
![Page 22: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/22.jpg)
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
![Page 23: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/23.jpg)
Behind the scenes
JNIAndroid
App
Go shared binary
rpc
![Page 24: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/24.jpg)
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:
![Page 25: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/25.jpg)
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
![Page 26: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/26.jpg)
DEMO
![Page 27: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/27.jpg)
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
![Page 28: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/28.jpg)
Questions?
![Page 29: Android is going to Go! Android and Golang](https://reader034.vdocuments.mx/reader034/viewer/2022051101/58ee78891a28ab44568b45d7/html5/thumbnails/29.jpg)
Thanks.@AlmogBaku