Android is going to Go!Android and golang
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
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.
What are we going to talk about?
1. What is Go?
2. How can we use Go with Android?
3. When is it useful?
Disclaimer
You wanna know more? Google it!
Google tip: use the keyword “golang”
Who heard about Go?
Why use Go, and what is it?!
• New modern language (since 2009)
• Super fast (native) compiling
• Concurrent
• Performant
• Garbage collected
• Standard libraries
Hello worldpackage main
import "fmt"
func main() { fmt.Println("Hello DroidCon!")}
RUN
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
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
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
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
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)}
Go Mobile
A tool for using Golang for native mobile apps easily!
Caution
The Go Mobile project is experimental. Use this at your own risk.
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
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
Native Go Apps
• 100% Go app
• Multi platform: Android, iOS and Desktop
• GUI with OpenGL
Behind the scenes
NativeActivityAndroid
App
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
DEMO
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
Behind the scenes
JNIAndroid
App
Go shared binary
rpc
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:
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
DEMO
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
Questions?
Thanks.@AlmogBaku