blob: 02ec3594b9b9cd5c2aaadd77d2031d60ed29bfe8 [file] [log] [blame] [view]
Ian Lance Taylorb2def952019-07-19 05:51:43 -07001The Go mobile subrepository adds support for mobile platforms (Android and iOS) and provides tools to build mobile applications.
2
3There are two strategies you can follow to include Go into your mobile stack:
4
5- Writing all-Go native mobile applications.
6- Writing SDK applications by generating bindings from a Go package and invoke them from Java (on Android) and Objective-C (on iOS).
7
8This article will contain step-by-step guides to explain how to achieve
9these strategies.
10
11- [Tools](#tools)
12- [Native applications](#native-applications)
13 - [Building and deploying to Android](#building-and-deploying-to-android)
14 - [Building and deploying to iOS](#building-and-deploying-to-ios)
15- [SDK applications](#sdk-applications-and-generating-bindings)
16 - [Building and deploying to Android](#building-and-deploying-to-android-1)
17 - [Building and deploying to iOS](#building-and-deploying-to-ios-1)
18 - [iOS Simulator](#ios-simulator)
19
20## Tools
21
22Note: You need to have [Go 1.5 or above](https://golang.org/dl/) to install mobile tools. (Or at least Go 1.7.4 if using macOS Sierra)
23
24Go Mobile introduces a new tool, [gomobile](https://golang.org/x/mobile/cmd/gomobile),
25to help you with the build and the binding process.
26
27On OS X, you will need to have
28[Xcode Command Line Tools](https://developer.apple.com/downloads/)
29installed.
30
31```
32$ go get golang.org/x/mobile/cmd/gomobile
33```
34
35(The following sections will help you how to use the gomobile tool.)
36
37## Native applications
38
39The native category includes applications entirely written in Go. Currently, the
40[golang.org/x/mobile](https://godoc.org/golang.org/x/mobile)
41contains only a small set of packages that focus on:
42
43* App control and configuration
44* OpenGL ES 2 bindings
45* Asset management
46* Event management
47* Experimental packages include OpenAL bindings, audio, font, sprite and motion sensors
48
49There are various example native applications under [golang.org/x/mobile/example](https://golang.org/x/mobile/example). We will build and deploy the basic example both to an Android and iOS device.
50
51Grab the application.
52
53```
54$ go get -d golang.org/x/mobile/example/basic
55```
56
57### Building and deploying to Android
58
59Run `gomobile build` to build an Android APK.
60
61```
62$ gomobile build -target=android golang.org/x/mobile/example/basic
63```
64
65Build command will build an APK named basic.apk.
66
67If an AndroidManifest.xml is defined in the package directory, it is added to the APK output. Otherwise, a default manifest is generated.
68
69If you have the [adb](http://developer.android.com/tools/help/adb.html) command installed on your machine, you can use `gomobile install` to build and push the APK to your mobile device.
70
71```
72$ gomobile install golang.org/x/mobile/example/basic
73```
74
75### Building and deploying to iOS
76Run `gomobile build` to build the package as an iOS application.
77
78Note: target=ios requires the host machine running OS X. You need to obtain a [signing identity and download provisioning profiles](https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/MaintainingCertificates/MaintainingCertificates.html) in order to continue.
79
80```
81$ gomobile build -target=ios golang.org/x/mobile/example/basic
82```
83
84The build command will build an application bundle, named `basic.app`.
85
86You can deploy .app files by dragging and dropping them to the device.
87
88* In Xcode, open Window > Devices.
89* Select the physical device from the left pane.
90* Drag and drop the .app file to "Installed Apps" section.
91* Check the "Copy items if needed" option
92
93![Deploying app bundle](http://i.imgur.com/fRbQ0EQ.png)
94
95Alternatively, you can deploy application bundles to your iOS device by using the [ios-deploy](https://github.com/phonegap/ios-deploy) utility command line tool. Use ios-deploy to push the application to your device.
96
97```
98$ ios-deploy -b basic.app
99```
100
101## SDK applications and generating bindings
102
103In this category, we will show you how you can use a Go package in
104your existing Android or iOS application.
105
106The advantages to following this strategy:
107
108* You can reuse a Go package from a mobile app without making significant changes to your existing application.
109* In cases where you want to share a common code base between your Android and iOS application, you can write the common functionality once in Go and glue them to the platform-specific code by invoking the Go package through bindings.
110
111Current limitations are listed below.
112
113* Only a [subset of Go types](https://godoc.org/golang.org/x/mobile/cmd/gobind) are currently supported.
114* Language bindings have a performance overhead.
115* There are a few limitations on how the exported APIs should look due to the limitations of the target language.
116
117We will use the example package under [golang.org/x/mobile/example/bind/hello](https://golang.org/x/mobile/example/bind/hello) to generate bindings and invoke Greetings function from Java and Objective-C.
118
119Grab the example by running the command below.
120
121```
122$ go get -d golang.org/x/mobile/example/bind/...
123```
124
125### Building and deploying to Android
126
127Note: Go Mobile runs on the same architectures as Go, which currently means ARM, ARM64, 386 and amd64 devices and emulators. Notably, Android on MIPS devices is not yet supported.
128
129* Launch Android Studio.
130* File > Import Project... to import the reference project from $GOPATH/src/golang.org/x/mobile/example/bind/android.
131
132![Android Studio](http://i.imgur.com/RhNCnnH.png)
133
134* Run the following command to generate the [aar](https://developer.android.com/studio/projects/android-library.html) file that is suitable for importing into Android projects:
135
136```
137$ gomobile bind -o app/hello.aar -target=android golang.org/x/mobile/example/bind/hello
138```
139
140* Build and deploy the application to the device.
141
142The app module contains the main application that invokes the `hello.Greetings`. When the application is launched the text view is updated with the string returned value.
143
144If you are not using Android Studio, in order to work with bindings for Android, you need to have [Android SDK](https://developer.android.com/sdk/index.html#Other) installed and ANDROID_HOME environment variable set to the SDK path. You also need the [NDK](https://developer.android.com/ndk/) installed; the easiest way is to run the SDK command `sdkmanager ndk-bundle`.
145
146Alternatively, if you are not familiar with android development, and you do not wish to set up all the required environment (Android SDK, Gradle, etc), you can use this [Dockerfile](https://github.com/mpl/go4droid/blob/master/Dockerfile) to build the application in [docker](https://www.docker.com/) instead.
147
148### Building and deploying to iOS
149
150Note: target=ios requires the host machine to be running OS X.
151
152```
153$ cd $GOPATH/src/golang.org/x/mobile/example/bind
154$ gomobile bind -target=ios golang.org/x/mobile/example/bind/hello
155```
156
157Gomobile bind will generate a framework bundle called `Hello.framework`. Open the sample XCode project by running the command below.
158
159```
160$ open ios/bind.xcodeproj
161```
162Drag and drop the `Hello.framework` bundle to the Xcode project. Check "Copy items if needed" if you need a different copy of the framework bundle within the Xcode otherwise. Otherwise, modifying the Go package source code and rerunning `gomobile bind` will update the hello.framework.
163
164
165
166![Drag and drop Hello.framework](http://i.imgur.com/u88CxN9.png)
167
168Your project layout should look like what's shown below.
169
170![Xcode project layout with Hello.framework](http://i.imgur.com/JhcSKwC.png)
171
172Build and run it on the simulator or an actual device (Cmd+R). When the application launches, the label on the main view will be modified with the string returned from `GoHelloGreetings` which invokes the `hello.Greetings` function.
173
174Note that you can also invoke `GoHelloGreetings` from Swift by importing Hello.
175
176```swift
177@import Hello
178// ...
179let msg = Hello.GoHelloGreetings("gopher")
180```
181
182#### iOS Simulator
183
184As of Go 1.5, only darwin/amd64 works on the iOS simulator. To use the simulator, you need to configure Xcode to only try to run 64-bit binaries.
185
186Xcode matches the bit width of the ARM binaries when running on the X86 simulator. That is, if you configure Xcode to build both 32-bit and 64-bit ARM binaries (the default), it will attempt to run 32-bit X86 binaries on the simulator, which will not work with Go today. Modify the Xcode build settings to only build 64-bit ARM binaries, and the simulator will run the amd64 binary.