johnl | 029e228 | 2017-02-04 19:34:50 +0100 | [diff] [blame] | 1 | The Go mobile subrepository adds support for mobile platforms (Android and iOS) and provides tools to build mobile applications. |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 2 | |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 3 | There are two strategies you can follow to include Go into your mobile stack: |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 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 | |
| 8 | This article will contain step-by-step guides to explain how to achieve |
| 9 | these strategies. |
| 10 | |
Burcu Dogan | f42faee | 2015-08-23 12:35:11 -0700 | [diff] [blame] | 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) |
Danieliu | e3e4595 | 2015-12-01 21:10:11 +0800 | [diff] [blame] | 17 | - [Building and deploying to iOS](#building-and-deploying-to-ios) |
Burcu Dogan | f42faee | 2015-08-23 12:35:11 -0700 | [diff] [blame] | 18 | - [iOS Simulator](#ios-simulator) |
| 19 | |
Burcu Dogan | 327fedf | 2015-08-10 14:46:18 -0700 | [diff] [blame] | 20 | ## Tools |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 21 | |
Brad Fitzpatrick | 98a6618 | 2017-01-06 11:30:57 -0800 | [diff] [blame] | 22 | Note: 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 Dogan | 22f3af2 | 2015-08-19 10:38:44 -0700 | [diff] [blame] | 23 | |
| 24 | Go Mobile introduces a new tool, [gomobile](https://golang.org/x/mobile/cmd/gomobile), |
| 25 | to help you with the build and the binding process. |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 26 | Go get gomobile and initialize it to install the required toolchain. |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 27 | |
Dmitri Shuralyov | 281571f | 2015-10-06 14:59:02 -0700 | [diff] [blame] | 28 | On OS X, you will need to have |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 29 | [Xcode Command Line Tools](https://developer.apple.com/downloads/) |
| 30 | installed. |
| 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 | |
johnl | 029e228 | 2017-02-04 19:34:50 +0100 | [diff] [blame] | 41 | The native category includes applications entirely written in Go. Currently, the |
Burcu Dogan | e8d5ad5 | 2015-07-29 14:47:41 -0700 | [diff] [blame] | 42 | [golang.org/x/mobile](https://godoc.org/golang.org/x/mobile) |
| 43 | contains only a small set of packages that focus on: |
Burcu Dogan | 01c67ea | 2015-07-29 13:07:32 -0700 | [diff] [blame] | 44 | |
| 45 | * App control and configuration |
| 46 | * OpenGL ES 2 bindings |
| 47 | * Asset management |
| 48 | * Event management |
Burcu Dogan | e8d5ad5 | 2015-07-29 14:47:41 -0700 | [diff] [blame] | 49 | * Experimental packages include OpenAL bindings, audio, font, sprite and motion sensors |
Burcu Dogan | 01c67ea | 2015-07-29 13:07:32 -0700 | [diff] [blame] | 50 | |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 51 | There 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 | |
| 53 | Grab the application. |
| 54 | |
| 55 | ``` |
| 56 | $ go get -d golang.org/x/mobile/example/basic |
Burcu Dogan | 7c3f351 | 2015-08-03 16:24:11 -0700 | [diff] [blame] | 57 | ``` |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 58 | |
Burcu Dogan | d4d4480 | 2015-08-01 17:19:44 -0700 | [diff] [blame] | 59 | ### Building and deploying to Android |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 60 | |
| 61 | Run `gomobile build` to build an Android APK. |
| 62 | |
| 63 | ``` |
| 64 | $ gomobile build -target=android golang.org/x/mobile/example/basic |
| 65 | ``` |
| 66 | |
| 67 | Build command will build an APK named basic.apk. |
BeeAdmin | f01830e | 2015-11-15 09:38:56 -0500 | [diff] [blame] | 68 | |
Jason Buberel | cba0d0f | 2015-11-22 12:24:10 -0800 | [diff] [blame] | 69 | If an AndroidManifest.xml is defined in the package directory, it is added to the APK output. Otherwise, a default manifest is generated. |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 70 | |
johnl | 029e228 | 2017-02-04 19:34:50 +0100 | [diff] [blame] | 71 | If 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 Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 72 | |
| 73 | ``` |
| 74 | $ gomobile install golang.org/x/mobile/example/basic |
| 75 | ``` |
| 76 | |
Burcu Dogan | d4d4480 | 2015-08-01 17:19:44 -0700 | [diff] [blame] | 77 | ### Building and deploying to iOS |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 78 | Run `gomobile build` to build the package as an iOS application. |
| 79 | |
Burcu Dogan | f1a2e58 | 2015-11-23 21:29:13 -0800 | [diff] [blame] | 80 | Note: 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 Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 81 | |
| 82 | ``` |
Burcu Dogan | 7c3f351 | 2015-08-03 16:24:11 -0700 | [diff] [blame] | 83 | $ gomobile build -target=ios golang.org/x/mobile/example/basic |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 84 | ``` |
| 85 | |
johnl | 029e228 | 2017-02-04 19:34:50 +0100 | [diff] [blame] | 86 | The build command will build an application bundle, named `basic.app`. |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 87 | |
Burcu Dogan | 989a68f | 2015-08-11 14:32:36 -0700 | [diff] [blame] | 88 | You 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 Dogan | 63235d5 | 2016-02-12 17:28:36 -0800 | [diff] [blame] | 93 | * Check the "Copy items if needed" option |
Burcu Dogan | 989a68f | 2015-08-11 14:32:36 -0700 | [diff] [blame] | 94 | |
Jaana B. Dogan | 5666148 | 2017-01-27 12:08:15 -0800 | [diff] [blame] | 95 |  |
Burcu Dogan | 989a68f | 2015-08-11 14:32:36 -0700 | [diff] [blame] | 96 | |
| 97 | Alternatively, 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 Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 98 | |
| 99 | ``` |
Burcu Dogan | 7f6d39b | 2015-08-04 09:54:40 -0700 | [diff] [blame] | 100 | $ ios-deploy -b basic.app |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 101 | ``` |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 102 | |
| 103 | ## SDK applications and generating bindings |
| 104 | |
Burcu Dogan | a17da17 | 2015-07-29 12:24:07 -0700 | [diff] [blame] | 105 | In this category, we will show you how you can use a Go package in |
| 106 | your existing Android or iOS application. |
| 107 | |
johnl | 029e228 | 2017-02-04 19:34:50 +0100 | [diff] [blame] | 108 | The advantages to following this strategy: |
Burcu Dogan | a17da17 | 2015-07-29 12:24:07 -0700 | [diff] [blame] | 109 | |
| 110 | * You can reuse a Go package from a mobile app without making significant changes to your existing application. |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 111 | * 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 Dogan | a17da17 | 2015-07-29 12:24:07 -0700 | [diff] [blame] | 112 | |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 113 | Current limitations are listed below. |
Burcu Dogan | a17da17 | 2015-07-29 12:24:07 -0700 | [diff] [blame] | 114 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 115 | * Only a [subset of Go types](https://godoc.org/golang.org/x/mobile/cmd/gobind) are currently supported. |
Burcu Dogan | 01c67ea | 2015-07-29 13:07:32 -0700 | [diff] [blame] | 116 | * Language bindings have a performance overhead. |
meirf | d052d52 | 2015-11-02 22:06:17 -0500 | [diff] [blame] | 117 | * There are a few limitations on how the exported APIs should look due to the limitations of the target language. |
Burcu Dogan | 6fb9a7c | 2015-07-29 12:25:26 -0700 | [diff] [blame] | 118 | |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 119 | We 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 | |
| 121 | Grab the example by running the command below. |
| 122 | |
| 123 | ``` |
Burcu Dogan | 2dd17a8 | 2015-08-14 13:59:12 -0700 | [diff] [blame] | 124 | $ go get -d golang.org/x/mobile/example/bind/... |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 125 | ``` |
| 126 | |
| 127 | ### Building and deploying to Android |
| 128 | |
Burcu Dogan | 683bb2b | 2015-08-10 16:46:51 -0700 | [diff] [blame] | 129 | Note: Currently only ARM devices and ARM emulating AVDs are supported. |
| 130 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 131 | If 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 Dogan | d5b916f | 2015-08-03 16:24:29 -0700 | [diff] [blame] | 132 | |
Burcu Dogan | 4ef1ac4 | 2015-08-04 10:37:32 -0700 | [diff] [blame] | 133 | * Launch Android Studio. |
| 134 | * File > Import Project... to import the reference project from $GOPATH/src/golang.org/x/mobile/example/bind/android. |
Burcu Dogan | 8cd96c6 | 2015-08-04 10:35:35 -0700 | [diff] [blame] | 135 | |
Jaana B. Dogan | 5666148 | 2017-01-27 12:08:15 -0800 | [diff] [blame] | 136 |  |
Burcu Dogan | 8cd96c6 | 2015-08-04 10:35:35 -0700 | [diff] [blame] | 137 | |
Burcu Dogan | 4ef1ac4 | 2015-08-04 10:37:32 -0700 | [diff] [blame] | 138 | * Open hello/build.gradle to edit the absolute path to GOPATH and GO. |
| 139 | * Build and deploy the application to the device. |
Burcu Dogan | d5b916f | 2015-08-03 16:24:29 -0700 | [diff] [blame] | 140 | |
Burcu Dogan | eddb1f1 | 2015-08-10 16:47:04 -0700 | [diff] [blame] | 141 | The 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 Dogan | 8cd96c6 | 2015-08-04 10:35:35 -0700 | [diff] [blame] | 143 | If 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 Dogan | d5b916f | 2015-08-03 16:24:29 -0700 | [diff] [blame] | 144 | |
| 145 | ``` |
| 146 | $ gomobile bind -target=android golang.org/x/mobile/example/bind/hello |
| 147 | ``` |
| 148 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 149 | The 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 Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 150 | |
pyros2097 | 9e72947 | 2015-09-07 10:33:49 +0530 | [diff] [blame] | 151 | ### Building and deploying to iOS |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 152 | |
johnl | 029e228 | 2017-02-04 19:34:50 +0100 | [diff] [blame] | 153 | Note: target=ios requires the host machine to be running OS X. |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 154 | |
| 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 Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 160 | Gomobile bind will generate a framework bundle called `Hello.framework`. Open the sample XCode project by running the command below. |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 161 | |
| 162 | ``` |
| 163 | $ open ios/bind.xcodeproj |
| 164 | ``` |
meirf | cbc79b9 | 2015-11-02 22:28:57 -0500 | [diff] [blame] | 165 | Drag 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 Dogan | 08d8e52 | 2015-08-10 16:20:10 -0700 | [diff] [blame] | 166 | |
Jaana B. Dogan | 5666148 | 2017-01-27 12:08:15 -0800 | [diff] [blame] | 167 | |
| 168 | |
| 169 |  |
Burcu Dogan | 08d8e52 | 2015-08-10 16:20:10 -0700 | [diff] [blame] | 170 | |
| 171 | Your project layout should look like what's shown below. |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 172 | |
Jaana B. Dogan | 5666148 | 2017-01-27 12:08:15 -0800 | [diff] [blame] | 173 |  |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 174 | |
Burcu Dogan | 0885a79 | 2015-08-04 09:57:48 -0700 | [diff] [blame] | 175 | Build 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 Crawshaw | 787cea1 | 2015-08-17 08:56:55 -0400 | [diff] [blame] | 176 | |
Burcu Dogan | d2ad3c9 | 2015-10-02 23:51:57 -0400 | [diff] [blame] | 177 | Note that you can also invoke `GoHelloGreetings` from Swift by importing Hello. |
| 178 | |
| 179 | ``` |
| 180 | @import Hello |
| 181 | // ... |
| 182 | let msg = Hello.GoHelloGreetings("gopher") |
| 183 | ``` |
| 184 | |
David Crawshaw | 787cea1 | 2015-08-17 08:56:55 -0400 | [diff] [blame] | 185 | #### iOS Simulator |
| 186 | |
| 187 | As 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 Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 189 | Xcode 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. |