David Chen | c4af828 | 2016-01-20 11:06:35 +0000 | [diff] [blame] | 1 | --- |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 2 | layout: documentation |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 3 | title: Build Tutorial - iOS |
daroberts | 223aebd | 2021-02-18 17:53:30 -0800 | [diff] [blame] | 4 | category: getting-started |
David Chen | c4af828 | 2016-01-20 11:06:35 +0000 | [diff] [blame] | 5 | --- |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 6 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 7 | # Bazel Tutorial: Build an iOS App |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 8 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 9 | This tutorial covers how to build a simple iOS app using Bazel. |
| 10 | |
| 11 | ## What you'll learn |
| 12 | |
| 13 | In this tutorial, you learn how to: |
| 14 | |
| 15 | * Set up the environment by installing Bazel and Xcode, and downloading the |
| 16 | sample project |
| 17 | * Set up a Bazel [workspace](workspace.md) that contained the source code |
| 18 | for the app and a `WORKSPACE` file that identifies the top level of the |
| 19 | workspace directory |
| 20 | * Update the `WORKSPACE` file to contain references to the required |
| 21 | external dependencies |
| 22 | * Create a `BUILD` file |
| 23 | * Run Bazel to build the app for the simulator and an iOS device |
| 24 | * Run the app in the simulator and on an iOS device |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 25 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 26 | ## Set up your environment |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 27 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 28 | To get started, install Bazel and Xcode, and get the sample project. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 29 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 30 | ### Install Bazel |
| 31 | |
| 32 | Follow the [installation instructions](../install.md) to install Bazel and |
| 33 | its dependencies. |
| 34 | |
| 35 | ### Install Xcode |
| 36 | |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 37 | Download and install [Xcode](https://developer.apple.com/xcode/downloads/). |
| 38 | Xcode contains the compilers, SDKs, and other tools required by Bazel to build |
| 39 | Apple applications. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 40 | |
| 41 | ### Get the sample project |
| 42 | |
| 43 | You also need to get the sample project for the tutorial from GitHub. The GitHub |
aiuto | 9afd33d | 2021-07-12 11:33:09 -0700 | [diff] [blame] | 44 | repo has two branches: `source-only` and `main`. The `source-only` branch |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 45 | contains the source files for the project only. You'll use the files in this |
aiuto | 9afd33d | 2021-07-12 11:33:09 -0700 | [diff] [blame] | 46 | branch in this tutorial. The `main` branch contains both the source files |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 47 | and completed Bazel `WORKSPACE` and `BUILD` files. You can use the files in this |
| 48 | branch to check your work when you've completed the tutorial steps. |
| 49 | |
| 50 | Enter the following at the command line to get the files in the `source-only` |
| 51 | branch: |
| 52 | |
| 53 | ```bash |
| 54 | cd $HOME |
| 55 | git clone -b source-only https://github.com/bazelbuild/examples |
| 56 | ``` |
| 57 | |
| 58 | The `git clone` command creates a directory named `$HOME/examples/`. This |
| 59 | directory contains several sample projects for Bazel. The project files for this |
| 60 | tutorial are in `$HOME/examples/tutorial/ios-app`. |
| 61 | |
| 62 | ## Set up a workspace |
| 63 | |
| 64 | A [workspace](../build-ref.html#workspaces) is a directory that contains the |
| 65 | source files for one or more software projects, as well as a `WORKSPACE` file |
| 66 | and `BUILD` files that contain the instructions that Bazel uses to build |
| 67 | the software. The workspace may also contain symbolic links to output |
| 68 | directories. |
| 69 | |
| 70 | A workspace directory can be located anywhere on your filesystem and is denoted |
| 71 | by the presence of the `WORKSPACE` file at its root. In this tutorial, your |
| 72 | workspace directory is `$HOME/examples/tutorial/`, which contains the sample |
| 73 | project files you cloned from the GitHub repo in the previous step. |
| 74 | |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 75 | Note that Bazel itself doesn't impose any requirements for organizing source |
| 76 | files in your workspace. The sample source files in this tutorial are organized |
| 77 | according to conventions for the target platform. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 78 | |
| 79 | For your convenience, set the `$WORKSPACE` environment variable now to refer to |
| 80 | your workspace directory. At the command line, enter: |
| 81 | |
| 82 | ```bash |
| 83 | export WORKSPACE=$HOME/examples/tutorial |
| 84 | ``` |
| 85 | |
| 86 | ### Create a WORKSPACE file |
| 87 | |
| 88 | Every workspace must have a text file named `WORKSPACE` located in the top-level |
| 89 | workspace directory. This file may be empty or it may contain references |
| 90 | to [external dependencies](../external.html) required to build the |
| 91 | software. |
| 92 | |
| 93 | For now, you'll create an empty `WORKSPACE` file, which simply serves to |
| 94 | identify the workspace directory. In later steps, you'll update the file to add |
| 95 | external dependency information. |
| 96 | |
| 97 | Enter the following at the command line: |
| 98 | |
| 99 | ```bash |
| 100 | touch $WORKSPACE/WORKSPACE |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 101 | open -a Xcode $WORKSPACE/WORKSPACE |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 102 | ``` |
| 103 | |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 104 | This creates and opens the empty `WORKSPACE` file. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 105 | |
| 106 | ### Update the WORKSPACE file |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 107 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 108 | To build applications for Apple devices, Bazel needs to pull the latest |
| 109 | [Apple build rules](https://github.com/bazelbuild/rules_apple) from its GitHub |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 110 | repository. To enable this, add the following [`git_repository`](../be/workspace.html#git_repository) |
| 111 | rules to your `WORKSPACE` file: |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 112 | |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 113 | ```python |
Yuta Saito | c3ff213 | 2019-09-03 10:45:43 -0700 | [diff] [blame] | 114 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 115 | |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 116 | git_repository( |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 117 | name = "build_bazel_rules_apple", |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 118 | remote = "https://github.com/bazelbuild/rules_apple.git", |
Shin Yamamoto | 257d093 | 2019-12-05 16:31:00 -0800 | [diff] [blame] | 119 | tag = "0.19.0", |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 120 | ) |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 121 | |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 122 | git_repository( |
Shin Yamamoto | 257d093 | 2019-12-05 16:31:00 -0800 | [diff] [blame] | 123 | name = "build_bazel_rules_swift", |
| 124 | remote = "https://github.com/bazelbuild/rules_swift.git", |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 125 | tag = "0.13.0", |
Shin Yamamoto | 257d093 | 2019-12-05 16:31:00 -0800 | [diff] [blame] | 126 | ) |
| 127 | |
| 128 | git_repository( |
| 129 | name = "build_bazel_apple_support", |
| 130 | remote = "https://github.com/bazelbuild/apple_support.git", |
vladmos | 4e9208a | 2020-03-11 12:16:42 -0700 | [diff] [blame] | 131 | tag = "0.7.2", |
Shin Yamamoto | 257d093 | 2019-12-05 16:31:00 -0800 | [diff] [blame] | 132 | ) |
| 133 | |
| 134 | git_repository( |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 135 | name = "bazel_skylib", |
| 136 | remote = "https://github.com/bazelbuild/bazel-skylib.git", |
Shin Yamamoto | 257d093 | 2019-12-05 16:31:00 -0800 | [diff] [blame] | 137 | tag = "0.9.0", |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 138 | ) |
| 139 | ``` |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 140 | |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 141 | **NOTE:** "Always use the [latest version of the build_apple rules](https://github.com/bazelbuild/rules_apple/releases) |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 142 | in the `tag` attribute. Make sure to check the latest dependencies required in |
| 143 | `rules_apple`'s [project](https://github.com/bazelbuild/rules_apple)." |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 144 | |
| 145 | **NOTE:** You **must** set the value of the `name` attribute in the |
Sergio Campama | 2705e8c | 2018-03-01 09:07:52 -0800 | [diff] [blame] | 146 | `git_repository` rule to `build_bazel_rules_apple` or the build will fail. |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 147 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 148 | ## Review the source files |
| 149 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 150 | Take a look at the source files for the app located in |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 151 | `$WORKSPACE/ios-app/UrlGet`. Again, you're just looking at these files now to |
| 152 | become familiar with the structure of the app. You don't have to edit any of the |
| 153 | source files to complete this tutorial. |
| 154 | |
| 155 | ## Create a BUILD file |
| 156 | |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 157 | At a command-line prompt, open a new `BUILD` file for editing: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 158 | |
| 159 | ```bash |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 160 | touch $WORKSPACE/ios-app/BUILD |
| 161 | open -a Xcode $WORKSPACE/ios-app/BUILD |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 162 | ``` |
| 163 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 164 | ### Add the rule load statement |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 165 | |
| 166 | To build iOS targets, Bazel needs to load build rules from its GitHub repository |
| 167 | whenever the build runs. To make these rules available to your project, add the |
| 168 | following load statement to the beginning of your `BUILD` file: |
| 169 | |
| 170 | ``` |
| 171 | load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application") |
| 172 | ``` |
| 173 | |
Googler | ec7ecd7 | 2021-03-19 16:20:52 -0700 | [diff] [blame] | 174 | You only need to load the `ios_application` rule because the `objc_library` |
| 175 | rule is built into the Bazel package. |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 176 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 177 | ### Add an objc_library rule |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 178 | |
| 179 | Bazel provides several build rules that you can use to build an app for the |
| 180 | iOS platform. For this tutorial, you'll first use the |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 181 | [`objc_library`](../be/objective-c.html#objc_library) rule to tell Bazel |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 182 | how to build a static library from the app source code and Xib files. Then |
Googler | 916f703 | 2019-04-24 16:24:12 -0700 | [diff] [blame] | 183 | you'll use the |
| 184 | [`ios_application`](https://github.com/bazelbuild/rules_apple/tree/master/doc) |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 185 | rule to tell it how to build the application binary and the `.ipa` bundle. |
| 186 | |
| 187 | **NOTE:** This tutorial presents a minimal use case of the Objective-C rules in |
| 188 | Bazel. For example, you have to use the `ios_application` rule to build |
| 189 | multi-architecture iOS apps. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 190 | |
| 191 | Add the following to your `BUILD` file: |
| 192 | |
| 193 | ```python |
| 194 | objc_library( |
| 195 | name = "UrlGetClasses", |
| 196 | srcs = [ |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 197 | "UrlGet/AppDelegate.m", |
| 198 | "UrlGet/UrlGetViewController.m", |
| 199 | "UrlGet/main.m", |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 200 | ], |
| 201 | hdrs = glob(["UrlGet/*.h"]), |
Shin Yamamoto | 257d093 | 2019-12-05 16:31:00 -0800 | [diff] [blame] | 202 | data = ["UrlGet/UrlGetViewController.xib"], |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 203 | ) |
| 204 | ``` |
| 205 | |
| 206 | Note the name of the rule, `UrlGetClasses`. |
| 207 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 208 | ### Add an ios_application rule |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 209 | |
Googler | 916f703 | 2019-04-24 16:24:12 -0700 | [diff] [blame] | 210 | The |
| 211 | [`ios_application`](https://github.com/bazelbuild/rules_apple/tree/master/doc) |
| 212 | rule builds the application binary and creates the `.ipa` bundle file. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 213 | |
| 214 | Add the following to your `BUILD` file: |
| 215 | |
| 216 | ```python |
| 217 | ios_application( |
| 218 | name = "ios-app", |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 219 | bundle_id = "Google.UrlGet", |
| 220 | families = [ |
| 221 | "iphone", |
| 222 | "ipad", |
| 223 | ], |
Zain Asgar | 74f86dc | 2017-11-28 07:57:45 -0800 | [diff] [blame] | 224 | minimum_os_version = "9.0", |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 225 | infoplists = [":UrlGet/UrlGet-Info.plist"], |
| 226 | visibility = ["//visibility:public"], |
| 227 | deps = [":UrlGetClasses"], |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 228 | ) |
| 229 | ``` |
| 230 | |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 231 | **NOTE:** Please update the `minimum_os_version` attribute to the minimum |
| 232 | version of iOS that you plan to support. |
| 233 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 234 | Note how the `deps` attribute references the output of the `UrlGetClasses` rule |
| 235 | you added to the `BUILD` file above. |
| 236 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 237 | Now, save and close the file. You can compare your `BUILD` file to the |
aiuto | 9afd33d | 2021-07-12 11:33:09 -0700 | [diff] [blame] | 238 | [completed example](https://github.com/bazelbuild/examples/blob/main/tutorial/ios-app/BUILD) |
| 239 | in the `main` branch of the `GitHub repo. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 240 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 241 | ## Build and deploy the app |
| 242 | |
| 243 | You are now ready to build your app and deploy it to a simulator and onto an |
| 244 | iOS device. |
| 245 | |
| 246 | **NOTE:** The app launches standalone but requires a backend server in order to |
| 247 | produce output. See the README file in the sample project directory to find out |
| 248 | how to build the backend server. |
| 249 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 250 | The built app is located in the `$WORKSPACE/bazel-bin` directory. |
| 251 | |
| 252 | Completed `WORKSPACE` and `BUILD` files for this tutorial are located in the |
aiuto | 9afd33d | 2021-07-12 11:33:09 -0700 | [diff] [blame] | 253 | [main branch](https://github.com/bazelbuild/examples/tree/HEAD/tutorial) |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 254 | of the GitHub repo. You can compare your work to the completed files for |
| 255 | additional help or troubleshooting. |
| 256 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 257 | ### Build the app for the simulator |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 258 | |
| 259 | Make sure that your current working directory is inside your Bazel workspace: |
| 260 | |
| 261 | ```bash |
| 262 | cd $WORKSPACE |
| 263 | ``` |
| 264 | |
| 265 | Now, enter the following to build the sample app: |
| 266 | |
| 267 | ```bash |
| 268 | bazel build //ios-app:ios-app |
| 269 | ``` |
| 270 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 271 | Bazel launches and builds the sample app. During the build process, its |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 272 | output will appear similar to the following: |
| 273 | |
| 274 | ```bash |
| 275 | INFO: Found 1 target... |
| 276 | Target //ios-app:ios-app up-to-date: |
| 277 | bazel-bin/ios-app/ios-app.ipa |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 278 | INFO: Elapsed time: 0.565s, Critical Path: 0.44s |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 279 | ``` |
| 280 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 281 | ### Find the build outputs |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 282 | |
| 283 | The `.ipa` file and other outputs are located in the |
| 284 | `$WORKSPACE/bazel-bin/ios-app` directory. |
| 285 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 286 | ### Run and debug the app in the simulator |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 287 | |
philwo | 9cd168a | 2021-06-08 13:22:14 -0700 | [diff] [blame] | 288 | You can now run the app from Xcode using the iOS Simulator. First, [generate an Xcode project using Tulsi](http://tulsi.bazel.build/). |
Dave MacLachlan | dc2c735 | 2017-11-17 08:50:48 -0800 | [diff] [blame] | 289 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 290 | Then, open the project in Xcode, choose an iOS Simulator as the runtime scheme, |
| 291 | and click **Run**. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 292 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 293 | **Note:** If you modify any project files in Xcode (for example, if you add or |
| 294 | remove a file, or add or change a dependency), you must rebuild the app using |
| 295 | Bazel, re-generate the Xcode project in Tulsi, and then re-open the project in |
| 296 | Xcode. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 297 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 298 | ### Build the app for a device |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 299 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 300 | To build your app so that it installs and launches on an iOS device, Bazel needs |
| 301 | the appropriate provisioning profile for that device model. Do the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 302 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 303 | 1. Go to your [Apple Developer Account](https://developer.apple.com/account) and |
| 304 | download the appropriate provisioning profile for your device. See |
| 305 | [Apple's documentation](https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/MaintainingProfiles/MaintainingProfiles.html) |
| 306 | for more information. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 307 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 308 | 2. Move your profile into `$WORKSPACE`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 309 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 310 | 3. (Optional) Add your profile to your `.gitignore` file. |
| 311 | |
| 312 | 4. Add the following line to the `ios_application` target in your `BUILD` file: |
| 313 | |
| 314 | ```python |
| 315 | provisioning_profile = "<your_profile_name>.mobileprovision", |
| 316 | ``` |
| 317 | |
| 318 | **NOTE:** Ensure the profile is correct so that the app can be installed on |
| 319 | a device. |
| 320 | |
| 321 | Now build the app for your device: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 322 | |
| 323 | ```bash |
| 324 | bazel build //ios-app:ios-app --ios_multi_cpus=armv7,arm64 |
| 325 | ``` |
| 326 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 327 | This builds the app as a fat binary. To build for a specific device |
| 328 | architecture, designate it in the build options. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 329 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 330 | To build for a specific Xcode version, use the `--xcode_version` option. To |
| 331 | build for a specific SDK version, use the `--ios_sdk_version` option. The |
| 332 | `--xcode_version` option is sufficient in most scenarios. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 333 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 334 | To specify a minimum required iOS version, add the `minimum_os_version` |
| 335 | parameter to the `ios_application` build rule in your `BUILD` file. |
| 336 | |
philwo | 9cd168a | 2021-06-08 13:22:14 -0700 | [diff] [blame] | 337 | You can also use [Tulsi](http://tulsi.bazel.build/docs/gettingstarted.html) to |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 338 | build your app using a GUI rather than the command line. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 339 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 340 | ### Install the app on a device |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 341 | |
| 342 | The easiest way to install the app on the device is to launch Xcode and use the |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 343 | `Windows > Devices` command. Select your plugged-in device from the list on the |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 344 | left, then add the app by clicking the **Add** (plus sign) button under |
| 345 | "Installed Apps" and selecting the `.ipa` file that you built. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 346 | |
Googler | b343cda | 2017-06-07 14:45:17 -0400 | [diff] [blame] | 347 | If your app fails to install on your device, ensure that you are specifying the |
| 348 | correct provisioning profile in your `BUILD` file (step 4 in the previous |
| 349 | section). |
| 350 | |
| 351 | If your app fails to launch, make sure that your device is part of your |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 352 | provisioning profile. The `View Device Logs` button on the `Devices` screen in |
| 353 | Xcode may provide other information as to what has gone wrong. |
| 354 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 355 | ## Further reading |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 356 | |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 357 | For more details, see |
aiuto | 9afd33d | 2021-07-12 11:33:09 -0700 | [diff] [blame] | 358 | [main branch](https://github.com/bazelbuild/examples/tree/HEAD/tutorial) |
Googler | 22230e4 | 2020-12-02 15:18:28 -0800 | [diff] [blame] | 359 | of the GitHub repo. |
| 360 | |