forked from eliasnaur/golab-2017-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golab-2017-training.slide
185 lines (112 loc) · 4.48 KB
/
golab-2017-training.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Write your next Android app in Go
GoLab, Florence 2017
Elias Naur
@elias_naur
: Hi everyone. This training session is about at least some of the nitty-gritty details of using Go in your Android apps.
: Since this is going to be a hands-on session, I'd like to start by getting the boring details done first.
* Prerequisites
- Laptop running a 64 bit OS; Linux, macOS or Windows.
- Go 1.7 (64 bit).
- Android SDK/Android studio.
- Android NDK (64 bit), downloaded through the Android SDK manager. Command line:
sdkmanager ndk-bundle
- A working and recent Android device or emulator.
- On a device, enable developer mode and USB debugging.
: I'd like you to go through these requirements, and make sure you've got the Android SDK or Android Studio installed in a recent version.
: Then, I'd like you to download the Android NDK for your platform, preferably through the Android SDK manager. You can access the manager inside Android Studio, or you can run the sdkmanager tool with the argument ndk-bundle. I prefer the command line tool because I don't have much experience with Android Studio.
: Let's take a few minutes for everyone to get ready.
: Please do speak up if you're stuck. The rest of this session won't be that interesting if you can't actually run your Go Android apps.
* Setup
- Environment
- ANDROID_HOME should point to your Android SDK
- gomobile
$ go get -u golang.org/x/mobile/cmd/...
$ gomobile init
: Now, with everything installed, go get the gomobile and gobind tools through the go command.
: Then, initialize the gomobile environment with the gomobile init command.
* Training files
Slides
https://talks.godoc.org/github.com/eliasnaur/golab-2017-training/golab-2017-training-nocomments.slide#13
Example code
$ git clone https://github.com/eliasnaur/golab-2017-training.git
Add the golab-2017-training directory to your GOPATH.
: To make it easier to start, I've added a very basic Android app you can use as a starting point for your own apps.
: The project, basic, is set up to use the support library, the databinding library and finally, to use gomobile to bind your Go packages.
* The first run
$ cd golab-2017-basic/basic
Linux/macOS
$ ./gradlew installDebug
Windows
$ gradlew.bat installDebug
: If everything went well, you should now have a "Basic" app on your emulator or device.
: Now, let's take 15-20 minutes for you to get everything working and to play around with the project. Try to edit the goapp.go source file, the layout file activity_main.xml or the MainActivity.java file.
* Logging
$ abd log cat -s Go:* GoLog:*
Java
android.util.Log.i("Go", ...);
* Structs
Go
type AGoStruct struct {
...
}
func NewAGoStruct() *AGoStruct {
...
}
func (s *AGoStruct) AGoMethod() {
...
}
Java
goapp.AGoStruct s = new goapp.AGoStruct();
s.aGoMethod()
: Now that you can call a simple Go function from Java, try to add a Go struct, with a constructor and a method.
: Remember to lower case the first letter of the method when calling it from Java.
: Then, instantiate it from Java and call the method.
: Try to add more methods, taking arguments or returning values.
* Structs (cont.)
You can use all the basic types (string, int, float32 etc.), other structs and interfaces.
Go
func F(str *GoStruct, interf GoInterface, i int, f float32) string
* Go interfaces
Go
type SomeGoInterface interface {
F(i int) string
}
Java
class ImplementsGo implements goapp.SomeGoInterface {
@Override public String f(i long, s goapp.AGoStruct) {
...
}
}
: Let's try adding a Go interface to the goapp package.
* Go interfaces (cont.)
Go
func TakesAnInterface(i SomeGoInterface) string {
return i.F(10)
}
Java
String res = goapp.takesAnInterface(new ImplementsGo());
android.util.Log.i("Go", "The result: " + res);
: Then, implement that interface in Java, and use it from Go.
* Reverse bindings
Go
package gorev
import (
"Java/android/os"
"Java/android/support/v7/app"
"Java/android/databinding/DataBindingUtil"
"Java/gorev/databinding/ActivityMainBinding"
rlayout "Java/gorev/R/layout"
// The Java package for "gorev".
gopkg "Java/gorev"
)
type GoActivity struct {
app.AppCompatActivity
}
* Reverse bindings (cont.)
func (a *GoActivity) OnCreate(this gopkg.GoActivity, b os.Bundle) {
this.Super().OnCreate(b)
db := DataBindingUtil.SetContentView(this, rlayout.Activity_main)
binding := ActivityMainBinding.Cast(db)
binding.SetText("Hello from the reverse")
}