blob: 4324621a086b15bf090a40e916cd1e2abac5f715 [file] [log] [blame] [view]
johnl029e2282017-02-04 19:34:50 +01001The Go mobile subrepository adds support for mobile platforms (Android and iOS) and provides tools to build mobile applications.
Burcu Dogan57900762015-07-29 12:06:54 -07002
Burcu Doganab4a4812015-08-01 16:51:41 -07003There are two strategies you can follow to include Go into your mobile stack:
Burcu Dogan57900762015-07-29 12:06:54 -07004
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
Burcu Doganf42faee2015-08-23 12:35:11 -070011- [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)
Danieliue3e45952015-12-01 21:10:11 +080017 - [Building and deploying to iOS](#building-and-deploying-to-ios)
Burcu Doganf42faee2015-08-23 12:35:11 -070018 - [iOS Simulator](#ios-simulator)
19
Burcu Dogan327fedf2015-08-10 14:46:18 -070020## Tools
Burcu Dogan57900762015-07-29 12:06:54 -070021
Brad Fitzpatrick98a66182017-01-06 11:30:57 -080022Note: 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)
Burcu Dogan22f3af22015-08-19 10:38:44 -070023
24Go Mobile introduces a new tool, [gomobile](https://golang.org/x/mobile/cmd/gomobile),
25to help you with the build and the binding process.
Hyang-Ah Hana Kimd39b4c02015-08-05 11:28:27 -040026Go get gomobile and initialize it to install the required toolchain.
Burcu Dogan57900762015-07-29 12:06:54 -070027
Dmitri Shuralyov281571f2015-10-06 14:59:02 -070028On OS X, you will need to have
Burcu Dogan57900762015-07-29 12:06:54 -070029[Xcode Command Line Tools](https://developer.apple.com/downloads/)
30installed.
31
32```
33$ go get golang.org/x/mobile/cmd/gomobile
34$ gomobile init # it might take a few minutes
35```
36
37(The following sections will help you how to use the gomobile tool.)
38
39## Native applications
40
johnl029e2282017-02-04 19:34:50 +010041The native category includes applications entirely written in Go. Currently, the
Burcu Dogane8d5ad52015-07-29 14:47:41 -070042[golang.org/x/mobile](https://godoc.org/golang.org/x/mobile)
43contains only a small set of packages that focus on:
Burcu Dogan01c67ea2015-07-29 13:07:32 -070044
45* App control and configuration
46* OpenGL ES 2 bindings
47* Asset management
48* Event management
Burcu Dogane8d5ad52015-07-29 14:47:41 -070049* Experimental packages include OpenAL bindings, audio, font, sprite and motion sensors
Burcu Dogan01c67ea2015-07-29 13:07:32 -070050
Burcu Doganab4a4812015-08-01 16:51:41 -070051There 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.
52
53Grab the application.
54
55```
56$ go get -d golang.org/x/mobile/example/basic
Burcu Dogan7c3f3512015-08-03 16:24:11 -070057```
Burcu Doganab4a4812015-08-01 16:51:41 -070058
Burcu Dogand4d44802015-08-01 17:19:44 -070059### Building and deploying to Android
Burcu Doganab4a4812015-08-01 16:51:41 -070060
61Run `gomobile build` to build an Android APK.
62
63```
64$ gomobile build -target=android golang.org/x/mobile/example/basic
65```
66
67Build command will build an APK named basic.apk.
BeeAdminf01830e2015-11-15 09:38:56 -050068
Jason Buberelcba0d0f2015-11-22 12:24:10 -080069If an AndroidManifest.xml is defined in the package directory, it is added to the APK output. Otherwise, a default manifest is generated.
Burcu Doganab4a4812015-08-01 16:51:41 -070070
johnl029e2282017-02-04 19:34:50 +010071If 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.
Burcu Doganab4a4812015-08-01 16:51:41 -070072
73```
74$ gomobile install golang.org/x/mobile/example/basic
75```
76
Burcu Dogand4d44802015-08-01 17:19:44 -070077### Building and deploying to iOS
Burcu Doganab4a4812015-08-01 16:51:41 -070078Run `gomobile build` to build the package as an iOS application.
79
Burcu Doganf1a2e582015-11-23 21:29:13 -080080Note: 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/ios/recipes/xcode_help-accounts_preferences/articles/obtain_certificates_and_provisioning_profiles.html) in order to continue.
Burcu Doganab4a4812015-08-01 16:51:41 -070081
82```
Burcu Dogan7c3f3512015-08-03 16:24:11 -070083$ gomobile build -target=ios golang.org/x/mobile/example/basic
Burcu Doganab4a4812015-08-01 16:51:41 -070084```
85
johnl029e2282017-02-04 19:34:50 +010086The build command will build an application bundle, named `basic.app`.
Burcu Doganab4a4812015-08-01 16:51:41 -070087
Burcu Dogan989a68f2015-08-11 14:32:36 -070088You can deploy .app files by dragging and dropping them to the device.
89
90* In Xcode, open Window > Devices.
91* Select the physical device from the left pane.
92* Drag and drop the .app file to "Installed Apps" section.
Burcu Dogan63235d52016-02-12 17:28:36 -080093* Check the "Copy items if needed" option
Burcu Dogan989a68f2015-08-11 14:32:36 -070094
Jaana B. Dogan56661482017-01-27 12:08:15 -080095![Deploying app bundle](http://i.imgur.com/fRbQ0EQ.png)
Burcu Dogan989a68f2015-08-11 14:32:36 -070096
97Alternatively, 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.
Burcu Doganab4a4812015-08-01 16:51:41 -070098
99```
Burcu Dogan7f6d39b2015-08-04 09:54:40 -0700100$ ios-deploy -b basic.app
Burcu Doganab4a4812015-08-01 16:51:41 -0700101```
Burcu Dogan57900762015-07-29 12:06:54 -0700102
103## SDK applications and generating bindings
104
Burcu Dogana17da172015-07-29 12:24:07 -0700105In this category, we will show you how you can use a Go package in
106your existing Android or iOS application.
107
johnl029e2282017-02-04 19:34:50 +0100108The advantages to following this strategy:
Burcu Dogana17da172015-07-29 12:24:07 -0700109
110* You can reuse a Go package from a mobile app without making significant changes to your existing application.
Hyang-Ah Hana Kimd39b4c02015-08-05 11:28:27 -0400111* 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.
Burcu Dogana17da172015-07-29 12:24:07 -0700112
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700113Current limitations are listed below.
Burcu Dogana17da172015-07-29 12:24:07 -0700114
Hyang-Ah Hana Kimd39b4c02015-08-05 11:28:27 -0400115* Only a [subset of Go types](https://godoc.org/golang.org/x/mobile/cmd/gobind) are currently supported.
Burcu Dogan01c67ea2015-07-29 13:07:32 -0700116* Language bindings have a performance overhead.
meirfd052d522015-11-02 22:06:17 -0500117* There are a few limitations on how the exported APIs should look due to the limitations of the target language.
Burcu Dogan6fb9a7c2015-07-29 12:25:26 -0700118
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700119We 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.
120
121Grab the example by running the command below.
122
123```
Burcu Dogan2dd17a82015-08-14 13:59:12 -0700124$ go get -d golang.org/x/mobile/example/bind/...
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700125```
126
127### Building and deploying to Android
128
Burcu Dogan683bb2b2015-08-10 16:46:51 -0700129Note: Currently only ARM devices and ARM emulating AVDs are supported.
130
Hyang-Ah Hana Kimd39b4c02015-08-05 11:28:27 -0400131If you are using Android Studio, you can use the [Gradle plugin](https://plugins.gradle.org/plugin/org.golang.mobile.bind) to automate this process.
Burcu Dogand5b916f2015-08-03 16:24:29 -0700132
Burcu Dogan4ef1ac42015-08-04 10:37:32 -0700133* Launch Android Studio.
134* File > Import Project... to import the reference project from $GOPATH/src/golang.org/x/mobile/example/bind/android.
Burcu Dogan8cd96c62015-08-04 10:35:35 -0700135
Jaana B. Dogan56661482017-01-27 12:08:15 -0800136![Android Studio](http://i.imgur.com/RhNCnnH.png)
Burcu Dogan8cd96c62015-08-04 10:35:35 -0700137
Burcu Dogan4ef1ac42015-08-04 10:37:32 -0700138* Open hello/build.gradle to edit the absolute path to GOPATH and GO.
139* Build and deploy the application to the device.
Burcu Dogand5b916f2015-08-03 16:24:29 -0700140
Burcu Doganeddb1f12015-08-10 16:47:04 -0700141The app module contains the main application that invokes the `hello.Greetings`. When application is launched the text view is updated with the string returned value.
142
Burcu Dogan8cd96c62015-08-04 10:35:35 -0700143If 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.
Burcu Dogand5b916f2015-08-03 16:24:29 -0700144
145```
146$ gomobile bind -target=android golang.org/x/mobile/example/bind/hello
147```
148
Hyang-Ah Hana Kimd39b4c02015-08-05 11:28:27 -0400149The command above will generate an [aar](http://tools.android.com/tech-docs/new-build-system/aar-format) that can be importable by your IDE.
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700150
pyros20979e729472015-09-07 10:33:49 +0530151### Building and deploying to iOS
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700152
johnl029e2282017-02-04 19:34:50 +0100153Note: target=ios requires the host machine to be running OS X.
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700154
155```
156$ cd $GOPATH/src/golang.org/x/mobile/example/bind
157$ gomobile bind -target=ios golang.org/x/mobile/example/bind/hello
158```
159
Burcu Doganee074282015-10-02 23:48:24 -0400160Gomobile bind will generate a framework bundle called `Hello.framework`. Open the sample XCode project by running the command below.
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700161
162```
163$ open ios/bind.xcodeproj
164```
meirfcbc79b92015-11-02 22:28:57 -0500165Drag 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.
Burcu Dogan08d8e522015-08-10 16:20:10 -0700166
Jaana B. Dogan56661482017-01-27 12:08:15 -0800167
168
169![Drag and drop Hello.framework](http://i.imgur.com/u88CxN9.png)
Burcu Dogan08d8e522015-08-10 16:20:10 -0700170
171Your project layout should look like what's shown below.
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700172
Jaana B. Dogan56661482017-01-27 12:08:15 -0800173![Xcode project layout with Hello.framework](http://i.imgur.com/JhcSKwC.png)
Burcu Dogan3a1b1b02015-08-02 23:26:43 -0700174
Burcu Dogan0885a792015-08-04 09:57:48 -0700175Build 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.
David Crawshaw787cea12015-08-17 08:56:55 -0400176
Burcu Dogand2ad3c92015-10-02 23:51:57 -0400177Note that you can also invoke `GoHelloGreetings` from Swift by importing Hello.
178
179```
180@import Hello
181// ...
182let msg = Hello.GoHelloGreetings("gopher")
183```
184
David Crawshaw787cea12015-08-17 08:56:55 -0400185#### iOS Simulator
186
187As 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.
188
Burcu Doganee074282015-10-02 23:48:24 -0400189Xcode 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.