android is going to go! - android and goland - almog baku

29
Android is going to Go! Android and golang

Upload: droidcontlv

Post on 21-Jan-2018

126 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Android is going to Go! - Android and goland - Almog Baku

Android is going to Go!Android and golang

Page 2: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

Disclaimer

You wanna know more? Google it!

Google tip: use the keyword “golang”

Page 6: Android is going to Go! - Android and goland - Almog Baku

Who heard about Go?

Page 7: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

Hello worldpackage main

import "fmt"

func main() { fmt.Println("Hello DroidCon!")}

RUN

Page 9: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

Go Mobile

A tool for using Golang for native mobile apps easily!

Page 15: Android is going to Go! - Android and goland - Almog Baku

Caution

The Go Mobile project is experimental. Use this at your own risk.

Page 16: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

Native Go Apps

• 100% Go app

• Multi platform: Android, iOS and Desktop

• GUI with OpenGL

Page 19: Android is going to Go! - Android and goland - Almog Baku

Behind the scenes

NativeActivityAndroid

App

Page 20: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

DEMO

Page 22: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

Behind the scenes

JNIAndroid

App

Go shared binary

rpc

Page 24: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

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 goland - Almog Baku

DEMO

Page 27: Android is going to Go! - Android and goland - Almog Baku

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 goland - Almog Baku

Questions?

Page 29: Android is going to Go! - Android and goland - Almog Baku

Thanks.@AlmogBaku