1*6777b538SAndroid Build Coastguard Worker# Incremental Install 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard WorkerIncremental Install is a way of building & deploying an APK that tries to 4*6777b538SAndroid Build Coastguard Workerminimize the time it takes to make a change and see that change running on 5*6777b538SAndroid Build Coastguard Workerdevice. They work best with `is_component_build=true`, and do *not* require a 6*6777b538SAndroid Build Coastguard Workerrooted device. 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker## Building 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard WorkerAdd the gn arg: 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Worker incremental_install = true 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard WorkerThis causes all apks to be built as incremental except for denylisted ones. 15*6777b538SAndroid Build Coastguard Worker 16*6777b538SAndroid Build Coastguard Worker## Running 17*6777b538SAndroid Build Coastguard Worker 18*6777b538SAndroid Build Coastguard WorkerIt is not enough to `adb install` them. You must use the generated wrapper 19*6777b538SAndroid Build Coastguard Workerscript: 20*6777b538SAndroid Build Coastguard Worker 21*6777b538SAndroid Build Coastguard Worker out/Debug/bin/your_apk run 22*6777b538SAndroid Build Coastguard Worker out/Debug/bin/run_chrome_public_test_apk # Automatically sets --fast-local-dev 23*6777b538SAndroid Build Coastguard Worker 24*6777b538SAndroid Build Coastguard Worker# How it Works 25*6777b538SAndroid Build Coastguard Worker 26*6777b538SAndroid Build Coastguard Worker## Overview 27*6777b538SAndroid Build Coastguard Worker 28*6777b538SAndroid Build Coastguard WorkerThe basic idea is to sideload .dex and .so files to `/data/local/tmp` rather 29*6777b538SAndroid Build Coastguard Workerthan bundling them in the .apk. Then, when making a change, only the changed 30*6777b538SAndroid Build Coastguard Worker.dex / .so needs to be pushed to the device. 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard WorkerFaster Builds: 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker * No `final_dex` step (where all .dex files are merged into one) 35*6777b538SAndroid Build Coastguard Worker * No need to rebuild .apk for code-only changes (but required for resources) 36*6777b538SAndroid Build Coastguard Worker * Apks sign faster because they are smaller. 37*6777b538SAndroid Build Coastguard Worker 38*6777b538SAndroid Build Coastguard WorkerFaster Installs: 39*6777b538SAndroid Build Coastguard Worker 40*6777b538SAndroid Build Coastguard Worker * The .apk is smaller, and so faster to verify. 41*6777b538SAndroid Build Coastguard Worker * No need to run `adb install` for code-only changes. 42*6777b538SAndroid Build Coastguard Worker * Only changed .so / .dex files are pushed. MD5s of existing on-device files 43*6777b538SAndroid Build Coastguard Worker are cached on host computer. 44*6777b538SAndroid Build Coastguard Worker 45*6777b538SAndroid Build Coastguard WorkerSlower Initial Runs: 46*6777b538SAndroid Build Coastguard Worker 47*6777b538SAndroid Build Coastguard Worker * The first time you run an incremental .apk, the `DexOpt` needs to run on all 48*6777b538SAndroid Build Coastguard Worker .dex files. This step is normally done during `adb install`, but is done on 49*6777b538SAndroid Build Coastguard Worker start-up for incremental apks. 50*6777b538SAndroid Build Coastguard Worker * DexOpt results are cached, so subsequent runs are faster. 51*6777b538SAndroid Build Coastguard Worker * The slowdown varies significantly based on the Android version. Android O+ 52*6777b538SAndroid Build Coastguard Worker has almost no visible slow-down. 53*6777b538SAndroid Build Coastguard Worker 54*6777b538SAndroid Build Coastguard WorkerCaveats: 55*6777b538SAndroid Build Coastguard Worker * Isolated processes (on L+) are incompatible with incremental install. As a 56*6777b538SAndroid Build Coastguard Worker work-around, isolated processes are disabled when building incremental apks. 57*6777b538SAndroid Build Coastguard Worker * Android resources, assets, and `loadable_modules` are not sideloaded (they 58*6777b538SAndroid Build Coastguard Worker remain in the apk), so builds & installs that modify any of these are not as 59*6777b538SAndroid Build Coastguard Worker fast as those that modify only .java / .cc. 60*6777b538SAndroid Build Coastguard Worker * Since files are sideloaded to `/data/local/tmp`, you need to use the wrapper 61*6777b538SAndroid Build Coastguard Worker scripts to uninstall them fully. E.g.: 62*6777b538SAndroid Build Coastguard Worker ```shell 63*6777b538SAndroid Build Coastguard Worker out/Default/bin/chrome_public_apk uninstall 64*6777b538SAndroid Build Coastguard Worker ``` 65*6777b538SAndroid Build Coastguard Worker * `AppComponentFactory.instantiateClassLoader()` is not supported. 66*6777b538SAndroid Build Coastguard Worker 67*6777b538SAndroid Build Coastguard Worker## The Code 68*6777b538SAndroid Build Coastguard Worker 69*6777b538SAndroid Build Coastguard WorkerAll incremental apks have the same classes.dex, which is built from: 70*6777b538SAndroid Build Coastguard Worker 71*6777b538SAndroid Build Coastguard Worker //build/android/incremental_install:bootstrap_java 72*6777b538SAndroid Build Coastguard Worker 73*6777b538SAndroid Build Coastguard WorkerThey also have a transformed `AndroidManifest.xml`, which overrides the the 74*6777b538SAndroid Build Coastguard Workermain application class and any instrumentation classes so that they instead 75*6777b538SAndroid Build Coastguard Workerpoint to `BootstrapApplication`. This is built by: 76*6777b538SAndroid Build Coastguard Worker 77*6777b538SAndroid Build Coastguard Worker //build/android/incremental_install/generate_android_manifest.py 78*6777b538SAndroid Build Coastguard Worker 79*6777b538SAndroid Build Coastguard WorkerWrapper scripts and install logic is contained in: 80*6777b538SAndroid Build Coastguard Worker 81*6777b538SAndroid Build Coastguard Worker //build/android/incremental_install/create_install_script.py 82*6777b538SAndroid Build Coastguard Worker //build/android/incremental_install/installer.py 83*6777b538SAndroid Build Coastguard Worker 84*6777b538SAndroid Build Coastguard WorkerFinally, GN logic for incremental apks is sprinkled throughout. 85