tfarina | 2c77322 | 2016-04-05 18:43:35 | [diff] [blame] | 1 | # Android Build Instructions |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## Prerequisites |
| 6 | |
| 7 | A Linux build machine capable of building [Chrome for |
| 8 | Linux](https://chromium.googlesource.com/chromium/src/+/master/docs/linux_build_instructions_prerequisites.md). |
| 9 | Other (Mac/Windows) platforms are not supported for Android. |
| 10 | |
| 11 | ## Getting the code |
| 12 | |
| 13 | First, check out and install the [depot\_tools |
| 14 | package](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_tutorial.html#_setting_up). |
| 15 | |
| 16 | Then, if you have no existing checkout, create your source directory and |
| 17 | get the code: |
| 18 | |
| 19 | ```shell |
| 20 | mkdir ~/chromium && cd ~/chromium |
| 21 | fetch --nohooks android # This will take 30 minutes on a fast connection |
| 22 | ``` |
| 23 | |
| 24 | If you have an existing Linux checkout, you can add Android support by |
| 25 | appending `target_os = ['android']` to your .gclient file (in the |
| 26 | directory above src): |
| 27 | |
| 28 | ```shell |
| 29 | cat > .gclient <<EOF |
| 30 | solutions = [ ...existing stuff in here... ] |
| 31 | target_os = [ 'android' ] # Add this to get Android stuff checked out. |
| 32 | EOF |
| 33 | ``` |
| 34 | |
| 35 | Then run gclient sync to get the Android stuff checked out: |
| 36 | |
| 37 | ```shell |
| 38 | gclient sync |
| 39 | ``` |
| 40 | |
| 41 | ## (Optional) Check out LKGR |
| 42 | |
| 43 | If you want a single build of Chromium in a known good state, sync to |
| 44 | the LKGR ("last known good revision"). You can find it |
| 45 | [here](http://chromium-status.appspot.com/lkgr), and the last 100 |
| 46 | [here](http://chromium-status.appspot.com/revisions). Run: |
| 47 | |
| 48 | ```shell |
| 49 | gclient sync --nohooks -r <lkgr-sha1> |
| 50 | ``` |
| 51 | |
| 52 | This is not needed for a typical developer workflow; only for one-time |
| 53 | builds of Chromium. |
| 54 | |
| 55 | ## Configure your build |
| 56 | |
| 57 | Android builds can be run with GN or GYP, though GN incremental builds |
| 58 | are the fastest option and GN will soon be the only supported option. |
| 59 | They are both meta-build systems that generate nina files for the |
| 60 | Android build. Both builds are regularly tested on the build waterfall. |
| 61 | |
| 62 | ### Configure GYP (deprecated -- use GN instead) |
| 63 | |
| 64 | If you are using GYP, next to the .gclient file, create a a file called |
| 65 | 'chromium.gyp_env' with the following contents: |
| 66 | |
| 67 | ```shell |
| 68 | echo "{ 'GYP_DEFINES': 'OS=android target_arch=arm', }" > chromium.gyp_env |
| 69 | ``` |
| 70 | |
| 71 | Note that "arm" is the default architecture and can be omitted. If |
| 72 | building for x86 or MIPS devices, change `target_arch` to "ia32" or |
| 73 | "mipsel". |
| 74 | |
| 75 | **NOTE:** If you are using the `GYP_DEFINES` environment variable, it |
| 76 | will override any settings in this file. Either clear it or set it to |
| 77 | the values above before running `gclient runhooks`. |
tfarina | ba2792fa | 2016-04-07 15:50:42 | [diff] [blame] | 78 | |
| 79 | See |
| 80 | [build/android/developer\_recommended\_flags.gypi](https://code.google.com/p/chromium/codesearch#chromium/src/build/android/developer_recommended_flags.gypi&sq=package:chromium&type=cs&q=file:android/developer_recommended_flags.gypi&l=1) |
| 81 | for other recommended GYP settings. |
| 82 | Once chromium.gyp_env is ready, you need to run the following command |
| 83 | to update projects from gyp files. You may need to run this again when |
| 84 | you have added new files, updated gyp files, or sync'ed your |
| 85 | repository. |
| 86 | |
| 87 | ```shell |
| 88 | gclient runhooks |
| 89 | ``` |
| 90 | |
| 91 | #### This will download more things and prompt you to accept Terms of Service for Android SDK packages. |
| 92 | |
| 93 | ## Configure GN (recommended) |
| 94 | |
| 95 | If you are using GN, create a build directory and set the build flags |
| 96 | with: |
| 97 | |
| 98 | ```shell |
| 99 | gn args out/Default |
| 100 | ``` |
| 101 | |
| 102 | You can replace out/Default with another name you choose inside the out |
| 103 | directory. Do not use GYP's out/Debug or out/Release directories, as |
| 104 | they may conflict with GYP builds. |
| 105 | |
| 106 | Also be aware that some scripts (e.g. tombstones.py, adb_gdb.py) |
| 107 | require you to set `CHROMIUM_OUTPUT_DIR=out/Default`. |
| 108 | |
| 109 | This command will bring up your editor with the GN build args. In this |
| 110 | file add: |
| 111 | |
| 112 | ``` |
| 113 | target_os = "android" |
| 114 | target_cpu = "arm" # (default) |
| 115 | is_debug = true # (default) |
| 116 | |
| 117 | # Other args you may want to set: |
| 118 | is_component_build = true |
| 119 | is_clang = true |
| 120 | symbol_level = 1 # Faster build with fewer symbols. -g1 rather than -g2 |
| 121 | enable_incremental_javac = true # Much faster; experimental |
| 122 | symbol_level = 1 # Faster build with fewer symbols. -g1 rather than -g2 |
| 123 | enable_incremental_javac = true # Much faster; experimental |
| 124 | ``` |
| 125 | |
| 126 | You can also specify `target_cpu` values of "x86" and "mipsel". Re-run |
| 127 | gn args on that directory to edit the flags in the future. See the [GN |
| 128 | build |
| 129 | configuration](https://www.chromium.org/developers/gn-build-configuration) |
| 130 | page for other flags you may want to set. |
| 131 | |
| 132 | ### Install build dependencies |
| 133 | |
| 134 | Update the system packages required to build by running: |
| 135 | |
| 136 | ```shell |
| 137 | ./build/install-build-deps-android.sh |
| 138 | ``` |
| 139 | |
| 140 | Make also sure that OpenJDK 1.7 is selected as default: |
| 141 | |
| 142 | `sudo update-alternatives --config javac` |
| 143 | `sudo update-alternatives --config java` |
| 144 | `sudo update-alternatives --config javaws` |
| 145 | `sudo update-alternatives --config javap` |
| 146 | `sudo update-alternatives --config jar` |
| 147 | `sudo update-alternatives --config jarsigner` |
| 148 | |
| 149 | ### Synchronize sub-directories. |
| 150 | |
| 151 | ```shell |
| 152 | gclient sync |
| 153 | ``` |
| 154 | |
| 155 | ## Build and install the APKs |
| 156 | |
| 157 | If the `adb_install_apk.py` script below fails, make sure aapt is in |
| 158 | your PATH. If not, add aapt's path to your PATH environment variable (it |
| 159 | should be |
| 160 | `/path/to/src/third_party/android_tools/sdk/build-tools/{latest_version}/`). |
| 161 | |
| 162 | Prepare the environment: |
| 163 | |
| 164 | ```shell |
| 165 | . build/android/envsetup.sh |
| 166 | ``` |
| 167 | |
| 168 | ### Plug in your Android device |
| 169 | |
| 170 | Make sure your Android device is plugged in via USB, and USB Debugging |
| 171 | is enabled. |
| 172 | |
| 173 | To enable USB Debugging: |
| 174 | |
| 175 | * Navigate to Settings \> About Phone \> Build number |
| 176 | * Click 'Build number' 7 times |
| 177 | * Now navigate back to Settings \> Developer Options |
| 178 | * Enable 'USB Debugging' and follow the prompts |
| 179 | |
| 180 | You may also be prompted to allow access to your PC once your device is |
| 181 | plugged in. |
| 182 | |
| 183 | You can check if the device is connected by running: |
| 184 | |
| 185 | ```shell |
| 186 | third_party/android_tools/sdk/platform-tools/adb devices |
| 187 | ``` |
| 188 | |
| 189 | Which prints a list of connected devices. If not connected, try |
| 190 | unplugging and reattaching your device. |
tfarina | a68eb90 | 2016-04-12 19:43:05 | [diff] [blame] | 191 | |
| 192 | ### Build the full browser |
| 193 | |
| 194 | **Note: When adding new resource files or java files in gyp builds, you |
| 195 | need to run 'gclient runhooks' again to get them in the build.** |
| 196 | |
| 197 | ```shell |
| 198 | ninja -C out/Release chrome_public_apk |
| 199 | ``` |
| 200 | |
| 201 | And deploy it to your Android device: |
| 202 | |
| 203 | ```shell |
| 204 | build/android/adb_install_apk.py out/Release/apks/ChromePublic.apk # For gyp. |
| 205 | CHROMIUM_OUTPUT_DIR=$gndir build/android/adb_install_apk.py $gndir/apks/ChromePublic.apk # for gn. |
| 206 | ``` |
| 207 | |
| 208 | The app will appear on the device as "Chromium". |
| 209 | |
| 210 | ### Build Content shell |
| 211 | |
| 212 | Wraps the content module (but not the /chrome embedder). See |
| 213 | [http://www.chromium.org/developers/content-module](http://www.chromium.org/developers/content-module) |
| 214 | for details on the content module and content shell. |
| 215 | |
| 216 | ```shell |
| 217 | ninja -C out/Release content_shell_apk |
| 218 | build/android/adb_install_apk.py out/Release/apks/ContentShell.apk |
| 219 | ``` |
| 220 | |
| 221 | this will build and install an Android apk under |
| 222 | `out/Release/apks/ContentShell.apk`. For GYP, replace `Release` with `Debug` |
| 223 | above if you want to generate a Debug app. If you are using GN, substitute the |
| 224 | name you initially gave to your build directory. |
| 225 | |
| 226 | If you use custom out dir instead of standard out/ dir, use |
| 227 | CHROMIUM_OUT_DIR env. |
| 228 | |
| 229 | ```shell |
| 230 | export CHROMIUM_OUT_DIR=out_android |
| 231 | ``` |
| 232 | |
| 233 | ### Build WebView shell |
| 234 | |
| 235 | [Android WebView](http://developer.android.com/reference/android/webkit/WebView.html) |
| 236 | is a system framework component. Since Android KitKat, it is implemented using |
| 237 | Chromium code (based off the [content module](http://dev.chromium.org/developers/content-module)). |
| 238 | It is possible to test modifications to WebView using a simple test shell. The |
| 239 | 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)) |
| 240 | and is **independent** of the WebView **implementation in the Android system** ( |
| 241 | the WebView shell is essentially a standalone unbundled app). |
| 242 | As drawback, the shell runs in non-production rendering mode only. |
| 243 | |
| 244 | ```shell |
| 245 | ninja -C out/Release android_webview_apk |
| 246 | build/android/adb_install_apk.py out/Release/apks/AndroidWebView.apk |
| 247 | ``` |
| 248 | |
| 249 | 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](http://www.chromium.org/developers/how-tos/build-instructions-android-webview) |
| 250 | |
| 251 | ### Running |
| 252 | |
| 253 | Set [command line flags](https://www.chromium.org/developers/how-tos/run-chromium-with-flags) if necessary. |
| 254 | |
| 255 | For Content shell: |
| 256 | |
| 257 | ```shell |
| 258 | build/android/adb_run_content_shell http://example.com |
| 259 | ``` |
| 260 | |
| 261 | For Chrome public: |
| 262 | |
| 263 | ```shell |
| 264 | build/android/adb_run_chrome_public http://example.com |
| 265 | ``` |
| 266 | |
| 267 | For Android WebView shell: |
| 268 | |
| 269 | ```shell |
| 270 | build/android/adb_run_android_webview_shell http://example.com |
| 271 | ``` |
| 272 | |
| 273 | ### Logging and debugging |
| 274 | |
| 275 | Logging is often the easiest way to understand code flow. In C++ you can print |
| 276 | log statements using the LOG macro or printf(). In Java, you can print log |
| 277 | statements using [android.util.Log](http://developer.android.com/reference/android/util/Log.html): |
| 278 | |
| 279 | `Log.d("sometag", "Reticulating splines progress = " + progress);` |
| 280 | |
| 281 | You can see these log statements using adb logcat: |
| 282 | |
| 283 | ```shell |
| 284 | adb logcat...01-14 11:08:53.373 22693 23070 D sometag: Reticulating splines progress = 0.99 |
| 285 | ``` |
| 286 | |
| 287 | You can debug Java or C++ code. To debug C++ code, use one of the |
| 288 | following commands: |
| 289 | |
| 290 | ```shell |
| 291 | build/android/adb_gdb_content_shell |
| 292 | build/android/adb_gdb_chrome_public |
| 293 | build/android/adb_gdb_android_webview_shell http://example.com |
| 294 | ``` |
| 295 | |
| 296 | See [Debugging Chromium on Android](https://www.chromium.org/developers/how-tos/debugging-on-android) |
| 297 | for more on debugging, including how to debug Java code. |
| 298 | |
| 299 | ### Testing |
| 300 | |
| 301 | For information on running tests, see [android\_test\_instructions.md](https://chromium.googlesource.com/chromium/src/+/master/docs/android_test_instructions.md). |
| 302 | |
| 303 | ### Faster Edit/Deploy (GN only) |
| 304 | |
| 305 | GN's "incremental install" uses reflection and side-loading to speed up the edit |
| 306 | & deploy cycle (normally < 10 seconds). |
| 307 | |
| 308 | * Make sure to set` is_component_build = true `in your GN args |
| 309 | * All apk targets have \*`_incremental` targets defined (e.g. |
| 310 | `chrome_public_apk_incremental`) |
| 311 | |
| 312 | Here's an example: |
| 313 | |
| 314 | ```shell |
| 315 | ninja -C out/Default chrome_public_apk_incremental |
| 316 | out/Default/bin/install_chrome_public_apk_incremental -v |
| 317 | ``` |
| 318 | |
| 319 | For gunit tests (note that run_*_incremental automatically add |
| 320 | --fast-local-dev when calling test\_runner.py): |
| 321 | |
| 322 | ```shell |
| 323 | ninja -C out/Default base_unittests_incremental |
| 324 | out/Default/bin/run_base_unittests_incremental |
| 325 | ``` |
| 326 | |
| 327 | For instrumentation tests: |
| 328 | |
| 329 | ```shell |
| 330 | ninja -C out/Default chrome_public_test_apk_incremental |
| 331 | out/Default/bin/run_chrome_public_test_apk_incremental |
| 332 | ``` |
| 333 | |
| 334 | To uninstall: |
| 335 | |
| 336 | ```shell |
| 337 | out/Default/bin/install_chrome_public_apk_incremental -v --uninstall |
| 338 | ``` |
| 339 | |
| 340 | ### Miscellaneous |
| 341 | |
| 342 | #### Rebuilding libchrome.so for a particular release |
| 343 | |
yfriedman | 9b4327b | 2016-05-04 16:36:24 | [diff] [blame^] | 344 | These instructions are only necessary for Chrome 51 and earlier. |
| 345 | |
tfarina | a68eb90 | 2016-04-12 19:43:05 | [diff] [blame] | 346 | In the case where you want to modify the native code for an existing |
| 347 | release of Chrome for Android (v25+) you can do the following steps. |
| 348 | Note that in order to get your changes into the official release, you'll |
| 349 | need to send your change for a codereview using the regular process for |
| 350 | committing code to chromium. |
| 351 | |
| 352 | 1. Open Chrome on your Android device and visit chrome://version |
| 353 | 2. Copy down the id listed next to "Build ID:" |
| 354 | 3. Go to |
| 355 | [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) |
| 356 | 4. Download the listed files and follow the steps in the README. |