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 - Android |
David Chen | c4af828 | 2016-01-20 11:06:35 +0000 | [diff] [blame] | 4 | --- |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 5 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 6 | # Introduction to Bazel: Building an Android App |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 7 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 8 | In this tutorial, you will learn how to build a simple Android app using Bazel. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 9 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 10 | Bazel supports building Android apps using the [Android |
| 11 | rules](https://docs.bazel.build/versions/master/be/android.html). |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 12 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 13 | This tutorial is intended for Windows, macOS and Linux users and does not |
| 14 | require experience with Bazel or Android app development. You do not need to |
| 15 | write any Android code in this tutorial. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 16 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 17 | ## Prerequisites |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 18 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 19 | You will need to install the following software: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 20 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 21 | * **Bazel.** To install, follow the [installation instructions](../install.md). |
| 22 | * **Android Studio.** To install, follow the steps to [download Android |
| 23 | Studio](https://developer.android.com/sdk/index.html). |
| 24 | * (Optional) **Git.** We will use `git` to download the Android app project. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 25 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 26 | ## Getting started |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 27 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 28 | We will be using a basic Android app project in [Bazel's examples |
| 29 | repository](https://github.com/bazelbuild/examples). |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 30 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 31 | This app has a single button that prints a greeting when clicked. |
| 32 | |
| 33 | <img src="/assets/android_tutorial_app.png" alt="screenshot of tutorial app" |
| 34 | width="700"> |
| 35 | |
| 36 | Clone the repository with `git` (or [download the ZIP file |
| 37 | directly](https://github.com/bazelbuild/examples/archive/master.zip)): |
| 38 | |
| 39 | ``` bash |
| 40 | git clone git@github.com:bazelbuild/examples.git bazel-examples |
| 41 | cd bazel-examples/android/tutorial |
| 42 | ``` |
| 43 | |
| 44 | For the rest of the tutorial, you will be executing commands in this directory. |
| 45 | |
| 46 | ## Review the source files |
| 47 | |
| 48 | Let's take a look at the source files for the app. |
| 49 | |
| 50 | ``` |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 51 | . |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 52 | ├── README.md |
| 53 | └── src |
| 54 | └── main |
| 55 | ├── AndroidManifest.xml |
| 56 | └── java |
| 57 | └── com |
| 58 | └── example |
| 59 | └── bazel |
| 60 | ├── AndroidManifest.xml |
| 61 | ├── Greeter.java |
| 62 | ├── MainActivity.java |
| 63 | └── res |
| 64 | ├── layout |
| 65 | │ └── activity_main.xml |
| 66 | └── values |
| 67 | ├── colors.xml |
| 68 | └── strings.xml |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 69 | ``` |
| 70 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 71 | The key files and directories are: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 72 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 73 | | Name | Location | |
| 74 | | Android manifest files | `src/main/AndroidManifest.xml` and `src/main/java/com/example/bazel/AndroidManifest.xml` | |
| 75 | | Android source files | `src/main/java/com/example/bazel/MainActivity.java` and `Greeter.java` | |
| 76 | | Resource file directory | `src/main/java/com/example/bazel/res/` | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 77 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 78 | ## Initialize the project's workspace |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 79 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 80 | A [workspace](../build-ref.html#workspace) is a directory that contains the |
| 81 | source files for one or more software projects, and has a `WORKSPACE` file at |
| 82 | its root. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 83 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 84 | The `WORKSPACE` file may be empty or may contain references to [external |
| 85 | dependencies](../external.html) required to build your project. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 86 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 87 | First, run the following command to create an empty `WORKSPACE` file: |
| 88 | |
| 89 | | Linux, macOS | `touch WORKSPACE` | |
| 90 | | Windows (Command Prompt) | `type nul > WORKSPACE` | |
| 91 | | Windows (PowerShell) | `New-Item WORKSPACE -ItemType file` | |
| 92 | |
| 93 | ### Running Bazel |
| 94 | |
| 95 | You can now check if Bazel is running correctly with the command: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 96 | |
| 97 | ```bash |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 98 | bazel info workspace |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 99 | ``` |
| 100 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 101 | If Bazel prints the path of the current directory, you're good to go! If the |
| 102 | `WORKSPACE` file does not exist, you may see an error message like: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 103 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 104 | ``` |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 105 | ERROR: The 'info' command is only supported from within a workspace. |
| 106 | ``` |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 107 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 108 | ## Integrate with the Android SDK |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 109 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 110 | Bazel needs to run the Android SDK [build |
| 111 | tools](https://developer.android.com/tools/revisions/build-tools.html) to build |
| 112 | the app. This means that you need to add some information to your `WORKSPACE` |
| 113 | file so that Bazel knows where to find them. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 114 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 115 | Add the following line to your `WORKSPACE` file: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 116 | |
| 117 | ```python |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 118 | android_sdk_repository(name = "androidsdk") |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 119 | ``` |
| 120 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 121 | This will use the Android SDK at the path referenced by the `ANDROID_HOME` |
| 122 | environment variable, and automatically detect the highest API level and the |
| 123 | latest version of build tools installed within that location. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 124 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 125 | You can set the `ANDROID_HOME` variable to the location of the Android SDK. Find |
| 126 | the path to the installed SDK using Android Studio's [SDK |
| 127 | Manager](https://developer.android.com/studio/intro/update#sdk-manager). |
| 128 | |
| 129 | For example, as the default SDK path is in your home directory for Linux and |
| 130 | macOS, and `LOCALAPPDATA` for Windows, you can use the following commands to set |
| 131 | the `ANDROID_HOME` variable: |
| 132 | |
| 133 | | Linux, macOS | `export ANDROID_HOME=$HOME/Android/Sdk/` | |
| 134 | | Windows (Command Prompt) | `set ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk` | |
| 135 | | Windows (PowerShell) | `$env:ANDROID_HOME="$env:LOCALAPPDATA\Android\Sdk"` | |
| 136 | |
| 137 | The above commands set the variable only for the current shell session. To make |
| 138 | them permanent, run the following commands: |
| 139 | |
| 140 | | Linux, macOS | `echo "export ANDROID_HOME=$HOME/Android/Sdk/" >> ~/.bashrc` | |
| 141 | | Windows (Command Prompt) | `setx ANDROID_HOME "%LOCALAPPDATA%\Android\Sdk"` | |
| 142 | | Windows (PowerShell) | `[System.Environment]::SetEnvironmentVariable('ANDROID_HOME', "$env:LOCALAPPDATA\Android\Sdk", [System.EnvironmentVariableTarget]::User)` | |
| 143 | |
| 144 | You can also explicitly specify the absolute path of the Android SDK, |
| 145 | the API level, and the version of build tools to use by including the `path`, |
| 146 | `api_level`, and `build_tools_version` attributes. If `api_level` and |
| 147 | `build_tools_version` are not specified, the `android_sdk_repository` rule will |
| 148 | use the respective latest version available in the SDK. You can specify any |
| 149 | combination of these attributes, as long as they are present in the SDK, for |
| 150 | example: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 151 | |
| 152 | ```python |
| 153 | android_sdk_repository( |
| 154 | name = "androidsdk", |
| 155 | path = "/path/to/Android/sdk", |
| 156 | api_level = 25, |
dannark | 5eb684f | 2017-09-06 02:29:30 +0200 | [diff] [blame] | 157 | build_tools_version = "26.0.1" |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 158 | ) |
| 159 | ``` |
| 160 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 161 | On Windows, note that the `path` attribute must use the mixed-style path, that |
| 162 | is, a Windows path with forward slashes: |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 163 | |
| 164 | ```python |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 165 | android_sdk_repository( |
| 166 | name = "androidsdk", |
| 167 | path = "c:/path/to/Android/sdk", |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 168 | ) |
| 169 | ``` |
| 170 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 171 | **Optional:** If you want to compile native code into your Android app, you |
jingwen | 5d99e3f | 2018-11-12 13:25:13 -0800 | [diff] [blame] | 172 | also need to download the [Android |
| 173 | NDK](https://developer.android.com/ndk/downloads/index.html) |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 174 | and tell Bazel where to find it by adding the following line to your `WORKSPACE` file: |
| 175 | |
| 176 | ```python |
| 177 | android_ndk_repository(name = "androidndk") |
| 178 | ``` |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 179 | |
| 180 | Similar to `android_sdk_repository`, the path to the Android NDK is inferred |
| 181 | from the `ANDROID_NDK_HOME` environment variable by default. The path can also |
| 182 | be explicitly specified with a `path` attribute on `android_ndk_repository`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 183 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 184 | For more information, read [Using the Android Native Development Kit with |
| 185 | Bazel](https://docs.bazel.build/versions/master/android-ndk.html). |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 186 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 187 | `api_level` is the version of the Android API that the SDK and NDK |
| 188 | target - for example, 23 for Android 6.0 and 25 for Android 7.1. If not |
| 189 | explicitly set, `api_level` defaults to the highest available API level for |
| 190 | `android_sdk_repository` and `android_ndk_repository`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 191 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 192 | It's not necessary to set the API levels to the same value for the SDK and NDK. |
| 193 | [This page](https://developer.android.com/ndk/guides/stable_apis.html) contains |
| 194 | a map from Android releases to NDK-supported API levels. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 195 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 196 | ## Create a BUILD file |
| 197 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 198 | A [`BUILD` file](../build-ref.html#BUILD_files) describes the relationship |
| 199 | between a set of build outputs, like compiled Android resources from `aapt` or |
| 200 | class files from `javac`, and their dependencies. These dependencies may be |
| 201 | source files (Java, C++) in your workspace or other build outputs. `BUILD` files |
| 202 | are written in a language called **Starlark**. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 203 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 204 | `BUILD` files are part of a concept in Bazel known as the *package hierarchy*. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 205 | The package hierarchy is a logical structure that overlays the directory |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 206 | structure in your workspace. Each [package](../build-ref.html#packages) is a |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 207 | directory (and its subdirectories) that contains a related set of source files |
| 208 | and a `BUILD` file. The package also includes any subdirectories, excluding |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 209 | those that contain their own `BUILD` file. The *package name* is the path to the |
| 210 | `BUILD` file relative to the `WORKSPACE`. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 211 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 212 | Note that Bazel's package hierarchy is conceptually different from the Java |
| 213 | package hierarchy of your Android App directory where the `BUILD` file is |
| 214 | located. , although the directories may be organized identically. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 215 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 216 | For the simple Android app in this tutorial, the source files in `src/main/` |
| 217 | comprise a single Bazel package. A more complex project may have many nested |
| 218 | packages. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 219 | |
| 220 | ### Add an android_library rule |
| 221 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 222 | A `BUILD` file contains several different types of declarations for Bazel. The |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 223 | most important type is the [build rule](../build-ref.html#funcs), which tells |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 224 | Bazel how to build an intermediate or final software output from a set of source |
| 225 | files or other dependencies. |
| 226 | |
| 227 | Bazel provides two build rules, `android_library` and `android_binary`, that you |
| 228 | can use to build an Android app. For this tutorial, you'll first use the |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 229 | [`android_library`](../be/android.html#android_library) rule to tell Bazel to |
| 230 | build an [Android library |
| 231 | module](http://developer.android.com/tools/projects/index.html#LibraryProjects) |
| 232 | from the app source code and resource files. You'll then use the |
| 233 | `android_binary` rule to tell Bazel how to build the Android application package. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 234 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 235 | Create a new `BUILD` file in the `src/main/java/com/example/bazel` directory, |
| 236 | and declare a new `android_library` target: |
| 237 | |
| 238 | `src/main/java/com/example/bazel/BUILD`: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 239 | |
| 240 | ```python |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 241 | package( |
| 242 | default_visibility = ["//src:__subpackages__"], |
| 243 | ) |
| 244 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 245 | android_library( |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 246 | name = "greeter_activity", |
| 247 | srcs = [ |
| 248 | "Greeter.java", |
| 249 | "MainActivity.java", |
| 250 | ], |
| 251 | manifest = "AndroidManifest.xml", |
| 252 | resource_files = glob(["res/**"]), |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 253 | ) |
| 254 | ``` |
| 255 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 256 | The `android_library` build rule contains a set of attributes that specify the |
| 257 | information that Bazel needs to build a library module from the source files. |
| 258 | Note also that the name of the rule is `greeter_activity`. You'll reference the |
| 259 | rule using this name as a dependency in the `android_binary` rule. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 260 | |
| 261 | ### Add an android_binary rule |
| 262 | |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 263 | The [`android_binary`](../be/android.html#android_binary) rule builds |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 264 | the Android application package (`.apk` file) for your app. |
| 265 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 266 | Create a new `BUILD` file in the `src/main/` directory, |
| 267 | and declare a new `android_binary` target: |
| 268 | |
| 269 | `src/main/BUILD`: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 270 | |
| 271 | ```python |
| 272 | android_binary( |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 273 | name = "app", |
| 274 | manifest = "AndroidManifest.xml", |
| 275 | deps = ["//src/main/java/com/example/bazel:greeter_activity"], |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 276 | ) |
| 277 | ``` |
| 278 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 279 | Here, the `deps` attribute references the output of the `greeter_activity` rule |
| 280 | you added to the `BUILD` file above. This means that when Bazel builds the |
| 281 | output of this rule it checks first to see if the output of the |
| 282 | `greeter_activity` library rule has been built and is up-to-date. If not, Bazel |
| 283 | builds it and then uses that output to build the application package file. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 284 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 285 | Now, save and close the file. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 286 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 287 | ## Build the app |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 288 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 289 | Let's try building the app! Run the following command to build the |
| 290 | `android_binary` target: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 291 | |
| 292 | ```bash |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 293 | bazel build //src/main:app |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 294 | ``` |
| 295 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 296 | The [`build`](../user-manual.html#build) subcommand instructs Bazel to build the |
| 297 | target that follows. The target is specified as the name of a build rule inside |
| 298 | a `BUILD` file, with along with the package path relative to your workspace |
| 299 | directory. For this example, the target is `app` and the package path is |
| 300 | `//src/main/`. |
| 301 | |
| 302 | Note that you can sometimes omit the package path or target name, depending on |
| 303 | your current working directory at the command line and the name of the target. |
| 304 | See [Labels](../build-ref.html#labels) in the *Bazel Concepts and Terminology* |
| 305 | page for more information about target labels and paths. |
| 306 | |
| 307 | Bazel will start to build the sample app. During the build process, its output |
| 308 | will appear similar to the following: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 309 | |
| 310 | ```bash |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 311 | INFO: Analysed target //src/main:app (0 packages loaded, 0 targets configured). |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 312 | INFO: Found 1 target... |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 313 | Target //src/main:app up-to-date: |
| 314 | bazel-bin/src/main/app_deploy.jar |
| 315 | bazel-bin/src/main/app_unsigned.apk |
| 316 | bazel-bin/src/main/app.apk |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 317 | ``` |
| 318 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 319 | ## Locate the build outputs |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 320 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 321 | Bazel puts the outputs of both intermediate and final build operations in a set |
| 322 | of per-user, per-workspace output directories. These directories are symlinked |
| 323 | from the following locations at the top-level of the project directory, where |
| 324 | the `WORKSPACE` is: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 325 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 326 | * `bazel-bin` stores binary executables and other runnable build outputs |
| 327 | * `bazel-genfiles` stores intermediary source files that are generated by |
| 328 | Bazel rules |
| 329 | * `bazel-out` stores other types of build outputs |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 330 | |
| 331 | Bazel stores the Android `.apk` file generated using the `android_binary` rule |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 332 | in the `bazel-bin/src/main` directory, where the subdirectory name `src/main` is |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 333 | derived from the name of the Bazel package. |
| 334 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 335 | At a command prompt, list the contents of this directory and find the `app.apk` |
| 336 | file: |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 337 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 338 | | Linux, macOS | `ls bazel-bin/src/main` | |
| 339 | | Windows (Command Prompt) | `dir bazel-bin\src\main` | |
| 340 | | Windows (PowerShell) | `ls bazel-bin\src\main` | |
| 341 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 342 | |
| 343 | ## Run the app |
| 344 | |
| 345 | You can now deploy the app to a connected Android device or emulator from the |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 346 | command line using the [`bazel |
| 347 | mobile-install`](../user-manual.html#mobile-install) command. This command uses |
| 348 | the Android Debug Bridge (`adb`) to communicate with the device. You must set up |
| 349 | your device to use `adb` following the instructions in [Android Debug |
| 350 | Bridge](http://developer.android.com/tools/help/adb.html) before deployment. You |
| 351 | can also choose to install the app on the Android emulator included in Android |
| 352 | Studio. Make sure the emulator is running before executing the command below. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 353 | |
| 354 | Enter the following: |
| 355 | |
| 356 | ```bash |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 357 | bazel mobile-install //src/main:app |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 358 | ``` |
| 359 | |
jingwen | ac56739 | 2018-11-26 12:04:47 -0800 | [diff] [blame] | 360 | > **Note**: `mobile-install` may not work if your device is running Android 9 |
| 361 | > (Pie). This issue is being tracked |
| 362 | > [here](https://github.com/bazelbuild/examples/issues/77). |
| 363 | > As a workaround, use `adb install bazel-bin/src/main/app.apk` to install the |
| 364 | > APK on the device. |
| 365 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 366 | Next, find and launch the "Bazel Tutorial App", which looks as follows: |
| 367 | |
| 368 | <img src="/assets/android_tutorial_before.png" alt="screenshot of tutorial app" width="500"> |
| 369 | |
| 370 | **Congratulations! You have just installed your first Bazel-built Android app.** |
| 371 | |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 372 | Note that the `mobile-install` subcommand also supports the |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 373 | [`--incremental`](../user-manual.html#mobile-install) flag that can be used to |
| 374 | deploy only those parts of the app that have changed since the last deployment. |
| 375 | |
| 376 | It also supports the `--start_app` flag to start the app immediately upon |
| 377 | installing it. |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 378 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 379 | ## Review your work |
dzc | 22b85a2 | 2017-05-31 20:37:50 +0200 | [diff] [blame] | 380 | |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 381 | In this tutorial, you used Bazel to build an Android app. To accomplish that, |
| 382 | you: |
| 383 | |
| 384 | * Set up your environment by installing Bazel and Android Studio, and |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 385 | downloading the sample project. |
dannark | dc71908d | 2018-12-04 08:24:11 -0800 | [diff] [blame^] | 386 | * Set up a Bazel [workspace](../be/workspace.md) that contains the source code |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 387 | for the app and a `WORKSPACE` file that identifies the top level of the |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 388 | workspace directory. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 389 | * Updated the `WORKSPACE` file to contain references to the required |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 390 | external dependencies, like the Android SDK. |
| 391 | * Created a `BUILD` file. |
| 392 | * Built the app with Bazel. |
| 393 | * Deployed and ran the app on an Android emulator or physical device. |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 394 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 395 | ## Further reading |
Googler | 3d70c31 | 2017-08-09 00:23:38 +0200 | [diff] [blame] | 396 | |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 397 | You now know the basics of building an Android project with Bazel. Here are some |
| 398 | other pages to check out: |
| 399 | |
dannark | dc71908d | 2018-12-04 08:24:11 -0800 | [diff] [blame^] | 400 | * More information on [mobile-install](../mobile-install) |
| 401 | * Testing your app with [Android instrumentation tests](../android-instrumentation-test.md) |
| 402 | * Integrating C and C++ code into your Android app with the [NDK](../android-ndk.md) |
Jingwen Chen | 92b14d3 | 2018-11-12 09:19:16 -0800 | [diff] [blame] | 403 | |
| 404 | Happy building! |