dpranke | c641a534 | 2017-04-04 02:35:01 | [diff] [blame^] | 1 | # Checking out and building Cast for Android |
| 2 | |
| 3 | **Note**: it is **not possible** to build a binary functionally |
| 4 | equivalent to a Chromecast. This is to build a single-page content |
| 5 | embedder with similar functionality to Cast products. |
| 6 | |
| 7 | ## Instructions for Google Employees |
| 8 | |
| 9 | Are you a Google employee? See |
| 10 | [go/building-android-cast](https://goto.google.com/building-android-cast) instead. |
| 11 | |
| 12 | [TOC] |
| 13 | |
| 14 | ## System requirements |
| 15 | |
| 16 | * A 64-bit Intel machine running Linux with at least 8GB of RAM. More |
| 17 | than 16GB is highly recommended. |
| 18 | * At least 100GB of free disk space. |
| 19 | * You must have Git and Python installed already. |
| 20 | |
| 21 | Most development is done on Ubuntu. Other distros may or may not work; |
| 22 | see the [Linux instructions](linux_build_instructions.md) for some suggestions. |
| 23 | |
| 24 | Building the Android client on Windows or Mac is not supported and doesn't work. |
| 25 | |
| 26 | ## Install `depot_tools` |
| 27 | |
| 28 | Clone the `depot_tools` repository: |
| 29 | |
| 30 | ```shell |
| 31 | $ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git |
| 32 | ``` |
| 33 | |
| 34 | Add `depot_tools` to the end of your PATH (you will probably want to put this |
| 35 | in your `~/.bashrc` or `~/.zshrc`). Assuming you cloned `depot_tools` |
| 36 | to `/path/to/depot_tools`: |
| 37 | |
| 38 | ```shell |
| 39 | $ export PATH="$PATH:/path/to/depot_tools" |
| 40 | ``` |
| 41 | |
| 42 | ## Get the code |
| 43 | |
| 44 | Create a `chromium` directory for the checkout and change to it (you can call |
| 45 | this whatever you like and put it wherever you like, as |
| 46 | long as the full path has no spaces): |
| 47 | |
| 48 | ```shell |
| 49 | $ mkdir ~/chromium && cd ~/chromium |
| 50 | $ fetch --nohooks android |
| 51 | ``` |
| 52 | |
| 53 | If you don't want the full repo history, you can save a lot of time by |
| 54 | adding the `--no-history` flag to `fetch`. |
| 55 | |
| 56 | Expect the command to take 30 minutes on even a fast connection, and many |
| 57 | hours on slower ones. |
| 58 | |
| 59 | If you've already installed the build dependencies on the machine (from another |
| 60 | checkout, for example), you can omit the `--nohooks` flag and `fetch` |
| 61 | will automatically execute `gclient runhooks` at the end. |
| 62 | |
| 63 | When `fetch` completes, it will have created a hidden `.gclient` file and a |
| 64 | directory called `src` in the working directory. The remaining instructions |
| 65 | assume you have switched to the `src` directory: |
| 66 | |
| 67 | ```shell |
| 68 | $ cd src |
| 69 | ``` |
| 70 | |
| 71 | ### Converting an existing Linux checkout |
| 72 | |
| 73 | If you have an existing Linux checkout, you can add Android support by |
| 74 | appending `target_os = ['android']` to your `.gclient` file (in the |
| 75 | directory above `src`): |
| 76 | |
| 77 | ```shell |
| 78 | $ echo "target_os = [ 'android' ]" >> ../.gclient |
| 79 | ``` |
| 80 | |
| 81 | Then run `gclient sync` to pull the new Android dependencies: |
| 82 | |
| 83 | ```shell |
| 84 | $ gclient sync |
| 85 | ``` |
| 86 | |
| 87 | (This is the only difference between `fetch android` and `fetch chromium`.) |
| 88 | |
| 89 | ### Install additional build dependencies |
| 90 | |
| 91 | Once you have checked out the code, run |
| 92 | |
| 93 | ```shell |
| 94 | $ build/install-build-deps-android.sh |
| 95 | ``` |
| 96 | |
| 97 | to get all of the dependencies you need to build on Linux, *plus* all of the |
| 98 | Android-specific dependencies (you need some of the regular Linux dependencies |
| 99 | because an Android build includes a bunch of the Linux tools and utilities). |
| 100 | |
| 101 | ### Run the hooks |
| 102 | |
| 103 | Once you've run `install-build-deps` at least once, you can now run the |
| 104 | Chromium-specific hooks, which will download additional binaries and other |
| 105 | things you might need: |
| 106 | |
| 107 | ```shell |
| 108 | $ gclient runhooks |
| 109 | ``` |
| 110 | |
| 111 | *Optional*: You can also [install API |
| 112 | keys](https://www.chromium.org/developers/how-tos/api-keys) if you want your |
| 113 | build to talk to some Google services, but this is not necessary for most |
| 114 | development and testing purposes. |
| 115 | |
| 116 | ## Setting up the build |
| 117 | |
| 118 | Chromium uses [Ninja](https://ninja-build.org) as its main build tool along |
| 119 | with a tool called [GN](../tools/gn/docs/quick_start.md) to generate `.ninja` |
| 120 | files. You can create any number of *build directories* with different |
| 121 | configurations. To create a build directory which builds Chrome for Android, |
| 122 | run: |
| 123 | |
| 124 | ```shell |
| 125 | $ gn gen --args='target_os="android" is_chromecast=true' out/Default |
| 126 | ``` |
| 127 | |
| 128 | * You only have to run this once for each new build directory, Ninja will |
| 129 | update the build files as needed. |
| 130 | * You can replace `Default` with another name, but |
| 131 | it should be a subdirectory of `out`. |
| 132 | * For other build arguments, including release settings, see [GN build |
| 133 | configuration](https://www.chromium.org/developers/gn-build-configuration). |
| 134 | The default will be a debug component build matching the current host |
| 135 | operating system and CPU. |
| 136 | * For more info on GN, run `gn help` on the command line or read the |
| 137 | [quick start guide](../tools/gn/docs/quick_start.md). |
| 138 | |
| 139 | Also be aware that some scripts (e.g. `tombstones.py`, `adb_gdb.py`) |
| 140 | require you to set `CHROMIUM_OUTPUT_DIR=out/Default`. |
| 141 | |
| 142 | ## Build cast\_shell\_apk |
| 143 | |
| 144 | Build cast\_shell\_apk with Ninja using the command: |
| 145 | |
| 146 | ```shell |
| 147 | $ ninja -C out/Default cast_shell_apk |
| 148 | ``` |
| 149 | |
| 150 | ## Installing and Running cast\_shell\_apk on a device |
| 151 | |
| 152 | If the `adb_install_apk.py` script below fails, make sure `aapt` is in your |
| 153 | PATH. If not, add `aapt`'s parent directory to your `PATH` environment variable |
| 154 | (it should be |
| 155 | `/path/to/src/third_party/android_tools/sdk/build-tools/{latest_version}/`). |
| 156 | |
| 157 | Prepare the environment: |
| 158 | |
| 159 | ```shell |
| 160 | $ . build/android/envsetup.sh |
| 161 | ``` |
| 162 | |
| 163 | ### Plug in your Android device |
| 164 | |
| 165 | Make sure your Android device is plugged in via USB, and USB Debugging |
| 166 | is enabled. |
| 167 | |
| 168 | To enable USB Debugging: |
| 169 | |
| 170 | * Navigate to Settings \> About Phone \> Build number |
| 171 | * Click 'Build number' 7 times |
| 172 | * Now navigate back to Settings \> Developer Options |
| 173 | * Enable 'USB Debugging' and follow the prompts |
| 174 | |
| 175 | You may also be prompted to allow access to your PC once your device is |
| 176 | plugged in. |
| 177 | |
| 178 | You can check if the device is connected by running: |
| 179 | |
| 180 | ```shell |
| 181 | third_party/android_tools/sdk/platform-tools/adb devices |
| 182 | ``` |
| 183 | |
| 184 | Which prints a list of connected devices. If not connected, try |
| 185 | unplugging and reattaching your device. |
| 186 | |
| 187 | ### Build the APK |
| 188 | |
| 189 | ```shell |
| 190 | ninja -C out/Release cast_shell_apk |
| 191 | ``` |
| 192 | |
| 193 | And deploy it to your Android device: |
| 194 | |
| 195 | ```shell |
| 196 | build/android/adb_install_apk.py out/Default/apks/CastShell.apk |
| 197 | adb shell am start -d "http://google.com" org.chromium.chromecast.shell/.CastShellActivity |
| 198 | |
| 199 | ``` |
| 200 | |
| 201 | The app will appear on the device as "Chromium". |
| 202 | |
| 203 | ### Build Content shell |
| 204 | |
| 205 | Wraps the content module (but not the /chrome embedder). See |
| 206 | [https://www.chromium.org/developers/content-module](https://www.chromium.org/developers/content-module) |
| 207 | for details on the content module and content shell. |
| 208 | |
| 209 | ```shell |
| 210 | ninja -C out/Release content_shell_apk |
| 211 | build/android/adb_install_apk.py out/Release/apks/ContentShell.apk |
| 212 | ``` |
| 213 | |
| 214 | this will build and install an Android apk under |
| 215 | `out/Release/apks/ContentShell.apk`. (Where `Release` is the name of your build |
| 216 | directory.) |
| 217 | |
| 218 | If you use custom out dir instead of standard out/ dir, use |
| 219 | CHROMIUM_OUT_DIR env. |
| 220 | |
| 221 | ```shell |
| 222 | export CHROMIUM_OUT_DIR=out_android |
| 223 | ``` |
| 224 | |
| 225 | ### Build WebView shell |
| 226 | |
| 227 | [Android WebView](https://developer.android.com/reference/android/webkit/WebView.html) |
| 228 | is a system framework component. Since Android KitKat, it is implemented using |
| 229 | Chromium code (based off the [content module](https://dev.chromium.org/developers/content-module)). |
| 230 | It is possible to test modifications to WebView using a simple test shell. The |
| 231 | WebView shell is a view with a URL bar at the top (see [code](https://code.google.com/p/chromium/codesearch#chromium/src/android_webview/test/shell/src/org/chromium/android_webview/test/AwTestContainerView.java)) |
| 232 | and is **independent** of the WebView **implementation in the Android system** ( |
| 233 | the WebView shell is essentially a standalone unbundled app). |
| 234 | As drawback, the shell runs in non-production rendering mode only. |
| 235 | |
| 236 | ```shell |
| 237 | ninja -C out/Release android_webview_apk |
| 238 | build/android/adb_install_apk.py out/Release/apks/AndroidWebView.apk |
| 239 | ``` |
| 240 | |
| 241 | If, instead, you want to build the complete Android WebView framework component and test the effect of your chromium changes in other Android app using the WebView, you should follow the [Android AOSP + chromium WebView instructions](https://www.chromium.org/developers/how-tos/build-instructions-android-webview) |
| 242 | |
| 243 | ### Running |
| 244 | |
| 245 | Set [command line flags](https://www.chromium.org/developers/how-tos/run-chromium-with-flags) if necessary. |
| 246 | |
| 247 | For Content shell: |
| 248 | |
| 249 | ```shell |
| 250 | build/android/adb_run_content_shell http://example.com |
| 251 | ``` |
| 252 | |
| 253 | For Chrome public: |
| 254 | |
| 255 | ```shell |
| 256 | build/android/adb_run_chrome_public http://example.com |
| 257 | ``` |
| 258 | |
| 259 | For Android WebView shell: |
| 260 | |
| 261 | ```shell |
| 262 | build/android/adb_run_android_webview_shell http://example.com |
| 263 | ``` |
| 264 | |
| 265 | ### Logging and debugging |
| 266 | |
| 267 | Logging is often the easiest way to understand code flow. In C++ you can print |
| 268 | log statements using the LOG macro or printf(). In Java, you can print log |
| 269 | statements using [android.util.Log](https://developer.android.com/reference/android/util/Log.html): |
| 270 | |
| 271 | `Log.d("sometag", "Reticulating splines progress = " + progress);` |
| 272 | |
| 273 | You can see these log statements using adb logcat: |
| 274 | |
| 275 | ```shell |
| 276 | adb logcat...01-14 11:08:53.373 22693 23070 D sometag: Reticulating splines progress = 0.99 |
| 277 | ``` |
| 278 | |
| 279 | You can debug Java or C++ code. To debug C++ code, use one of the |
| 280 | following commands: |
| 281 | |
| 282 | ```shell |
| 283 | build/android/adb_gdb_content_shell |
| 284 | build/android/adb_gdb_chrome_public |
| 285 | build/android/adb_gdb_android_webview_shell http://example.com |
| 286 | ``` |
| 287 | |
| 288 | See [Debugging Chromium on Android](https://www.chromium.org/developers/how-tos/debugging-on-android) |
| 289 | for more on debugging, including how to debug Java code. |
| 290 | |
| 291 | ### Testing |
| 292 | |
| 293 | For information on running tests, see [android\_test\_instructions.md](https://chromium.googlesource.com/chromium/src/+/master/docs/android_test_instructions.md). |
| 294 | |
| 295 | ### Faster Edit/Deploy (GN only) |
| 296 | |
| 297 | GN's "incremental install" uses reflection and side-loading to speed up the edit |
| 298 | & deploy cycle (normally < 10 seconds). The initial launch of the apk will be |
| 299 | a little slower since updated dex files are installed manually. |
| 300 | |
| 301 | * Make sure to set` is_component_build = true `in your GN args |
| 302 | * All apk targets have \*`_incremental` targets defined (e.g. |
| 303 | `chrome_public_apk_incremental`) except for Webview and Monochrome |
| 304 | |
| 305 | Here's an example: |
| 306 | |
| 307 | ```shell |
| 308 | ninja -C out/Default chrome_public_apk_incremental |
| 309 | out/Default/bin/install_chrome_public_apk_incremental -v |
| 310 | ``` |
| 311 | |
| 312 | For gunit tests (note that run_*_incremental automatically add |
| 313 | --fast-local-dev when calling test\_runner.py): |
| 314 | |
| 315 | ```shell |
| 316 | ninja -C out/Default base_unittests_incremental |
| 317 | out/Default/bin/run_base_unittests_incremental |
| 318 | ``` |
| 319 | |
| 320 | For instrumentation tests: |
| 321 | |
| 322 | ```shell |
| 323 | ninja -C out/Default chrome_public_test_apk_incremental |
| 324 | out/Default/bin/run_chrome_public_test_apk_incremental |
| 325 | ``` |
| 326 | |
| 327 | To uninstall: |
| 328 | |
| 329 | ```shell |
| 330 | out/Default/bin/install_chrome_public_apk_incremental -v --uninstall |
| 331 | ``` |
| 332 | |
| 333 | A subtly erroneous flow arises when you build a regular apk but install an |
| 334 | incremental apk (e.g. |
| 335 | `ninja -C out/Default foo_apk && out/Default/bin/install_foo_apk_incremental`). |
| 336 | Setting `incremental_apk_by_default = true` in your GN args aliases regular |
| 337 | targets as their incremental counterparts. With this arg set, the commands |
| 338 | above become: |
| 339 | |
| 340 | ```shell |
| 341 | ninja -C out/Default chrome_public_apk |
| 342 | out/Default/bin/install_chrome_public_apk |
| 343 | |
| 344 | ninja -C out/Default base_unittests |
| 345 | out/Default/bin/run_base_unittests |
| 346 | |
| 347 | ninja -C out/Default chrome_public_test_apk |
| 348 | out/Default/bin/run_chrome_public_test_apk |
| 349 | ``` |
| 350 | |
| 351 | If you want to build a non-incremental apk you'll need to remove |
| 352 | `incremental_apk_by_default` from your GN args. |
| 353 | |
| 354 | ## Tips, tricks, and troubleshooting |
| 355 | |
| 356 | ### Rebuilding libchrome.so for a particular release |
| 357 | |
| 358 | These instructions are only necessary for Chrome 51 and earlier. |
| 359 | |
| 360 | In the case where you want to modify the native code for an existing |
| 361 | release of Chrome for Android (v25+) you can do the following steps. |
| 362 | Note that in order to get your changes into the official release, you'll |
| 363 | need to send your change for a codereview using the regular process for |
| 364 | committing code to chromium. |
| 365 | |
| 366 | 1. Open Chrome on your Android device and visit chrome://version |
| 367 | 2. Copy down the id listed next to "Build ID:" |
| 368 | 3. Go to |
| 369 | [http://storage.googleapis.com/chrome-browser-components/BUILD\_ID\_FROM\_STEP\_2/index.html](http://storage.googleapis.com/chrome-browser-components/BUILD_ID_FROM_STEP_2/index.html) |
| 370 | 4. Download the listed files and follow the steps in the README. |