Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 1 | 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) |
| 17 | - [Building an deploying to iOS](#building-an-deploying-to-ios) |
| 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 | |
Burcu Dogan | 22f3af2 | 2015-08-19 10:38:44 -0700 | [diff] [blame] | 22 | Note: You need to have [Go 1.5 or above](https://golang.org/dl/) to install mobile tools. |
| 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 | |
Burcu Dogan | e8d5ad5 | 2015-07-29 14:47:41 -0700 | [diff] [blame] | 41 | Native category includes applications entirely written in Go. Currently, the |
| 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. |
| 68 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 69 | If you have [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] | 70 | |
| 71 | ``` |
| 72 | $ gomobile install golang.org/x/mobile/example/basic |
| 73 | ``` |
| 74 | |
Burcu Dogan | d4d4480 | 2015-08-01 17:19:44 -0700 | [diff] [blame] | 75 | ### Building and deploying to iOS |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 76 | Run `gomobile build` to build the package as an iOS application. |
| 77 | |
Dmitri Shuralyov | 281571f | 2015-10-06 14:59:02 -0700 | [diff] [blame] | 78 | Note: target=ios requires the host machine running OS X. Prior to Xcode 7, 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] | 79 | |
| 80 | ``` |
Burcu Dogan | 7c3f351 | 2015-08-03 16:24:11 -0700 | [diff] [blame] | 81 | $ gomobile build -target=ios golang.org/x/mobile/example/basic |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 82 | ``` |
| 83 | |
Burcu Dogan | 989a68f | 2015-08-11 14:32:36 -0700 | [diff] [blame] | 84 | The build command will build an application bundle, named basic.app. |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 85 | |
Burcu Dogan | 989a68f | 2015-08-11 14:32:36 -0700 | [diff] [blame] | 86 | You 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 | |
| 92 |  |
| 93 | |
| 94 | 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] | 95 | |
| 96 | ``` |
Burcu Dogan | 7f6d39b | 2015-08-04 09:54:40 -0700 | [diff] [blame] | 97 | $ ios-deploy -b basic.app |
Burcu Dogan | ab4a481 | 2015-08-01 16:51:41 -0700 | [diff] [blame] | 98 | ``` |
Burcu Dogan | 5790076 | 2015-07-29 12:06:54 -0700 | [diff] [blame] | 99 | |
| 100 | ## SDK applications and generating bindings |
| 101 | |
Burcu Dogan | a17da17 | 2015-07-29 12:24:07 -0700 | [diff] [blame] | 102 | In this category, we will show you how you can use a Go package in |
| 103 | your existing Android or iOS application. |
| 104 | |
| 105 | The advantages to follow this strategy: |
| 106 | |
| 107 | * 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] | 108 | * 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] | 109 | |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 110 | Current limitations are listed below. |
Burcu Dogan | a17da17 | 2015-07-29 12:24:07 -0700 | [diff] [blame] | 111 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 112 | * 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] | 113 | * Language bindings have a performance overhead. |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 114 | * There are a few limitations on how the exported APIs should look like due to the limitations of the target language. |
Burcu Dogan | 6fb9a7c | 2015-07-29 12:25:26 -0700 | [diff] [blame] | 115 | |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 116 | 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. |
| 117 | |
| 118 | Grab the example by running the command below. |
| 119 | |
| 120 | ``` |
Burcu Dogan | 2dd17a8 | 2015-08-14 13:59:12 -0700 | [diff] [blame] | 121 | $ go get -d golang.org/x/mobile/example/bind/... |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 122 | ``` |
| 123 | |
| 124 | ### Building and deploying to Android |
| 125 | |
Burcu Dogan | 683bb2b | 2015-08-10 16:46:51 -0700 | [diff] [blame] | 126 | Note: Currently only ARM devices and ARM emulating AVDs are supported. |
| 127 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 128 | 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] | 129 | |
Burcu Dogan | 4ef1ac4 | 2015-08-04 10:37:32 -0700 | [diff] [blame] | 130 | * Launch Android Studio. |
| 131 | * 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] | 132 | |
| 133 |  |
| 134 | |
Burcu Dogan | 4ef1ac4 | 2015-08-04 10:37:32 -0700 | [diff] [blame] | 135 | * Open hello/build.gradle to edit the absolute path to GOPATH and GO. |
| 136 | * Build and deploy the application to the device. |
Burcu Dogan | d5b916f | 2015-08-03 16:24:29 -0700 | [diff] [blame] | 137 | |
Burcu Dogan | eddb1f1 | 2015-08-10 16:47:04 -0700 | [diff] [blame] | 138 | 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. |
| 139 | |
Burcu Dogan | 8cd96c6 | 2015-08-04 10:35:35 -0700 | [diff] [blame] | 140 | 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] | 141 | |
| 142 | ``` |
| 143 | $ gomobile bind -target=android golang.org/x/mobile/example/bind/hello |
| 144 | ``` |
| 145 | |
Hyang-Ah Hana Kim | d39b4c0 | 2015-08-05 11:28:27 -0400 | [diff] [blame] | 146 | 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] | 147 | |
pyros2097 | 9e72947 | 2015-09-07 10:33:49 +0530 | [diff] [blame] | 148 | ### Building and deploying to iOS |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 149 | |
Dmitri Shuralyov | 281571f | 2015-10-06 14:59:02 -0700 | [diff] [blame] | 150 | Note: target=ios requires the host machine running OS X. |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 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 | |
Burcu Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 157 | 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] | 158 | |
| 159 | ``` |
| 160 | $ open ios/bind.xcodeproj |
| 161 | ``` |
Burcu Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 162 | Drag and drop the `Hello.framework` bundle to the Xcode project. Check "Copy items in needed" if you need a different copy of the framework bundle within the Xcode otherwise. Otherwise, modifying the Go package source code and reruning `gomobile bind` will update the hello.framework. |
Burcu Dogan | 08d8e52 | 2015-08-10 16:20:10 -0700 | [diff] [blame] | 163 | |
Burcu Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 164 |  |
Burcu Dogan | 08d8e52 | 2015-08-10 16:20:10 -0700 | [diff] [blame] | 165 | |
| 166 | Your project layout should look like what's shown below. |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 167 | |
Burcu Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 168 |  |
Burcu Dogan | 3a1b1b0 | 2015-08-02 23:26:43 -0700 | [diff] [blame] | 169 | |
Burcu Dogan | 0885a79 | 2015-08-04 09:57:48 -0700 | [diff] [blame] | 170 | 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] | 171 | |
Burcu Dogan | d2ad3c9 | 2015-10-02 23:51:57 -0400 | [diff] [blame] | 172 | Note that you can also invoke `GoHelloGreetings` from Swift by importing Hello. |
| 173 | |
| 174 | ``` |
| 175 | @import Hello |
| 176 | // ... |
| 177 | let msg = Hello.GoHelloGreetings("gopher") |
| 178 | ``` |
| 179 | |
David Crawshaw | 787cea1 | 2015-08-17 08:56:55 -0400 | [diff] [blame] | 180 | #### iOS Simulator |
| 181 | |
| 182 | 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. |
| 183 | |
Burcu Dogan | ee07428 | 2015-10-02 23:48:24 -0400 | [diff] [blame] | 184 | 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. |