1*8975f5c5SAndroid Build Coastguard Worker# Chromium's Java Toolchain 2*8975f5c5SAndroid Build Coastguard Worker 3*8975f5c5SAndroid Build Coastguard WorkerThis doc aims to describe the Chrome build process that takes a set of `.java` 4*8975f5c5SAndroid Build Coastguard Workerfiles and turns them into a `classes.dex` file. 5*8975f5c5SAndroid Build Coastguard Worker 6*8975f5c5SAndroid Build Coastguard Worker[TOC] 7*8975f5c5SAndroid Build Coastguard Worker 8*8975f5c5SAndroid Build Coastguard Worker## Core GN Target Types 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard WorkerThe following have `supports_android` and `requires_android` set to false by 11*8975f5c5SAndroid Build Coastguard Workerdefault: 12*8975f5c5SAndroid Build Coastguard Worker* `java_library()`: Compiles `.java` -> `.jar` 13*8975f5c5SAndroid Build Coastguard Worker* `java_prebuilt()`: Imports a prebuilt `.jar` file. 14*8975f5c5SAndroid Build Coastguard Worker 15*8975f5c5SAndroid Build Coastguard WorkerThe following have `supports_android` and `requires_android` set to true. They 16*8975f5c5SAndroid Build Coastguard Workeralso have a default `jar_excluded_patterns` set (more on that later): 17*8975f5c5SAndroid Build Coastguard Worker* `android_library()` 18*8975f5c5SAndroid Build Coastguard Worker* `android_java_prebuilt()` 19*8975f5c5SAndroid Build Coastguard Worker 20*8975f5c5SAndroid Build Coastguard WorkerAll target names must end with "_java" so that the build system can distinguish 21*8975f5c5SAndroid Build Coastguard Workerthem from non-java targets (or [other variations](https://cs.chromium.org/chromium/src/build/config/android/internal_rules.gni?rcl=ec2c17d7b4e424e060c3c7972842af87343526a1&l=20)). 22*8975f5c5SAndroid Build Coastguard Worker 23*8975f5c5SAndroid Build Coastguard WorkerMost targets produce two separate `.jar` files: 24*8975f5c5SAndroid Build Coastguard Worker* Device `.jar`: Used to produce `.dex.jar`, which is used on-device. 25*8975f5c5SAndroid Build Coastguard Worker* Host `.jar`: For use on the host machine (`junit_binary` / `java_binary`). 26*8975f5c5SAndroid Build Coastguard Worker * Host `.jar` files live in `lib.java/` so that they are archived in 27*8975f5c5SAndroid Build Coastguard Worker builder/tester bots (which do not archive `obj/`). 28*8975f5c5SAndroid Build Coastguard Worker 29*8975f5c5SAndroid Build Coastguard Worker## From Source to Final Dex 30*8975f5c5SAndroid Build Coastguard Worker 31*8975f5c5SAndroid Build Coastguard Worker### Step 1: Create interface .jar with turbine or ijar 32*8975f5c5SAndroid Build Coastguard Worker 33*8975f5c5SAndroid Build Coastguard WorkerWhat are interface jars?: 34*8975f5c5SAndroid Build Coastguard Worker 35*8975f5c5SAndroid Build Coastguard Worker* They contain `.class` files with all private symbols and all method bodies 36*8975f5c5SAndroid Build Coastguard Worker removed. 37*8975f5c5SAndroid Build Coastguard Worker* Dependant targets use interface `.jar` files to skip having to be rebuilt 38*8975f5c5SAndroid Build Coastguard Worker when only private implementation details change. 39*8975f5c5SAndroid Build Coastguard Worker 40*8975f5c5SAndroid Build Coastguard WorkerFor prebuilt `.jar` files: we use [//third_party/ijar] to create interface 41*8975f5c5SAndroid Build Coastguard Worker`.jar` files from the prebuilt ones. 42*8975f5c5SAndroid Build Coastguard Worker 43*8975f5c5SAndroid Build Coastguard WorkerFor non-prebuilt `.jar` files`: we use [//third_party/turbine] to create 44*8975f5c5SAndroid Build Coastguard Workerinterface `.jar` files directly from `.java` source files. Turbine is faster 45*8975f5c5SAndroid Build Coastguard Workerthan javac because it does not compile method bodies. Although Turbine causes 46*8975f5c5SAndroid Build Coastguard Workerus to compile files twice, it speeds up builds by allowing `javac` compilation 47*8975f5c5SAndroid Build Coastguard Workerof targets to happen concurrently with their dependencies. We also use Turbine 48*8975f5c5SAndroid Build Coastguard Workerto run our annotation processors. 49*8975f5c5SAndroid Build Coastguard Worker 50*8975f5c5SAndroid Build Coastguard Worker[//third_party/ijar]: /third_party/ijar/README.chromium 51*8975f5c5SAndroid Build Coastguard Worker[//third_party/turbine]: /third_party/turbine/README.chromium 52*8975f5c5SAndroid Build Coastguard Worker 53*8975f5c5SAndroid Build Coastguard Worker### Step 2a: Compile with javac 54*8975f5c5SAndroid Build Coastguard Worker 55*8975f5c5SAndroid Build Coastguard WorkerThis step is the only step that does not apply to prebuilt targets. 56*8975f5c5SAndroid Build Coastguard Worker 57*8975f5c5SAndroid Build Coastguard Worker* All `.java` files in a target are compiled by `javac` into `.class` files. 58*8975f5c5SAndroid Build Coastguard Worker * This includes `.java` files that live within `.srcjar` files, referenced 59*8975f5c5SAndroid Build Coastguard Worker through `srcjar_deps`. 60*8975f5c5SAndroid Build Coastguard Worker* The `classpath` used when compiling a target is comprised of `.jar` files of 61*8975f5c5SAndroid Build Coastguard Worker its deps. 62*8975f5c5SAndroid Build Coastguard Worker * When deps are library targets, the Step 1 `.jar` file is used. 63*8975f5c5SAndroid Build Coastguard Worker * When deps are prebuilt targets, the original `.jar` file is used. 64*8975f5c5SAndroid Build Coastguard Worker * All `.jar` processing done in subsequent steps does not impact compilation 65*8975f5c5SAndroid Build Coastguard Worker classpath. 66*8975f5c5SAndroid Build Coastguard Worker* `.class` files are zipped into an output `.jar` file. 67*8975f5c5SAndroid Build Coastguard Worker* There is **no support** for incremental compilation at this level. 68*8975f5c5SAndroid Build Coastguard Worker * If one source file changes within a library, then the entire library is 69*8975f5c5SAndroid Build Coastguard Worker recompiled. 70*8975f5c5SAndroid Build Coastguard Worker * Prefer smaller targets to avoid slow compiles. 71*8975f5c5SAndroid Build Coastguard Worker 72*8975f5c5SAndroid Build Coastguard Worker### Step 2b: Compile with ErrorProne 73*8975f5c5SAndroid Build Coastguard Worker 74*8975f5c5SAndroid Build Coastguard WorkerThis step can be disabled via GN arg: `use_errorprone_java_compiler = false` 75*8975f5c5SAndroid Build Coastguard Worker 76*8975f5c5SAndroid Build Coastguard Worker* Concurrently with step 1a: [ErrorProne] compiles java files and checks for bug 77*8975f5c5SAndroid Build Coastguard Worker patterns, including some [custom to Chromium][ep_plugins]. 78*8975f5c5SAndroid Build Coastguard Worker* ErrorProne used to replace step 1a, but was changed to a concurrent step after 79*8975f5c5SAndroid Build Coastguard Worker being identified as being slower. 80*8975f5c5SAndroid Build Coastguard Worker 81*8975f5c5SAndroid Build Coastguard Worker[ErrorProne]: https://errorprone.info/ 82*8975f5c5SAndroid Build Coastguard Worker[ep_plugins]: /tools/android/errorprone_plugin/ 83*8975f5c5SAndroid Build Coastguard Worker 84*8975f5c5SAndroid Build Coastguard Worker### Step 3: Desugaring (Device .jar Only) 85*8975f5c5SAndroid Build Coastguard Worker 86*8975f5c5SAndroid Build Coastguard WorkerThis step happens only when targets have `supports_android = true`. It is not 87*8975f5c5SAndroid Build Coastguard Workerapplied to `.jar` files used by `junit_binary`. 88*8975f5c5SAndroid Build Coastguard Worker 89*8975f5c5SAndroid Build Coastguard Worker* `//third_party/bazel/desugar` converts certain Java 8 constructs, such as 90*8975f5c5SAndroid Build Coastguard Worker lambdas and default interface methods, into constructs that are compatible 91*8975f5c5SAndroid Build Coastguard Worker with Java 7. 92*8975f5c5SAndroid Build Coastguard Worker 93*8975f5c5SAndroid Build Coastguard Worker### Step 4: Instrumenting (Device .jar Only) 94*8975f5c5SAndroid Build Coastguard Worker 95*8975f5c5SAndroid Build Coastguard WorkerThis step happens only when this GN arg is set: `use_jacoco_coverage = true` 96*8975f5c5SAndroid Build Coastguard Worker 97*8975f5c5SAndroid Build Coastguard Worker* [Jacoco] adds instrumentation hooks to methods. 98*8975f5c5SAndroid Build Coastguard Worker 99*8975f5c5SAndroid Build Coastguard Worker[Jacoco]: https://www.eclemma.org/jacoco/ 100*8975f5c5SAndroid Build Coastguard Worker 101*8975f5c5SAndroid Build Coastguard Worker### Step 5: Filtering 102*8975f5c5SAndroid Build Coastguard Worker 103*8975f5c5SAndroid Build Coastguard WorkerThis step happens only when targets that have `jar_excluded_patterns` or 104*8975f5c5SAndroid Build Coastguard Worker`jar_included_patterns` set (e.g. all `android_` targets). 105*8975f5c5SAndroid Build Coastguard Worker 106*8975f5c5SAndroid Build Coastguard Worker* Remove `.class` files that match the filters from the `.jar`. These `.class` 107*8975f5c5SAndroid Build Coastguard Worker files are generally those that are re-created with different implementations 108*8975f5c5SAndroid Build Coastguard Worker further on in the build process. 109*8975f5c5SAndroid Build Coastguard Worker * E.g.: `R.class` files - a part of [Android Resources]. 110*8975f5c5SAndroid Build Coastguard Worker * E.g.: `GEN_JNI.class` - a part of our [JNI] glue. 111*8975f5c5SAndroid Build Coastguard Worker 112*8975f5c5SAndroid Build Coastguard Worker[JNI]: /third_party/jni_zero/README.md 113*8975f5c5SAndroid Build Coastguard Worker[Android Resources]: life_of_a_resource.md 114*8975f5c5SAndroid Build Coastguard Worker 115*8975f5c5SAndroid Build Coastguard Worker### Step 6: Per-Library Dexing 116*8975f5c5SAndroid Build Coastguard Worker 117*8975f5c5SAndroid Build Coastguard WorkerThis step happens only when targets have `supports_android = true`. 118*8975f5c5SAndroid Build Coastguard Worker 119*8975f5c5SAndroid Build Coastguard Worker* [d8] converts `.jar` files containing `.class` files into `.dex.jar` files 120*8975f5c5SAndroid Build Coastguard Worker containing `classes.dex` files. 121*8975f5c5SAndroid Build Coastguard Worker* Dexing is incremental - it will reuse dex'ed classes from a previous build if 122*8975f5c5SAndroid Build Coastguard Worker the corresponding `.class` file is unchanged. 123*8975f5c5SAndroid Build Coastguard Worker* These per-library `.dex.jar` files are used directly by [incremental install], 124*8975f5c5SAndroid Build Coastguard Worker and are inputs to the Apk step when `enable_proguard = false`. 125*8975f5c5SAndroid Build Coastguard Worker * Even when `is_java_debug = false`, many apk targets do not enable ProGuard 126*8975f5c5SAndroid Build Coastguard Worker (e.g. unit tests). 127*8975f5c5SAndroid Build Coastguard Worker 128*8975f5c5SAndroid Build Coastguard Worker[d8]: https://developer.android.com/studio/command-line/d8 129*8975f5c5SAndroid Build Coastguard Worker[incremental install]: /build/android/incremental_install/README.md 130*8975f5c5SAndroid Build Coastguard Worker 131*8975f5c5SAndroid Build Coastguard Worker### Step 7: Apk / Bundle Module Compile 132*8975f5c5SAndroid Build Coastguard Worker 133*8975f5c5SAndroid Build Coastguard Worker* Each `android_apk` and `android_bundle_module` template has a nested 134*8975f5c5SAndroid Build Coastguard Worker `java_library` target. The nested library includes final copies of files 135*8975f5c5SAndroid Build Coastguard Worker stripped out by prior filtering steps. These files include: 136*8975f5c5SAndroid Build Coastguard Worker * Final `R.java` files, created by `compile_resources.py`. 137*8975f5c5SAndroid Build Coastguard Worker * Final `GEN_JNI.java` for [JNI glue]. 138*8975f5c5SAndroid Build Coastguard Worker * `BuildConfig.java` and `NativeLibraries.java` (//base dependencies). 139*8975f5c5SAndroid Build Coastguard Worker 140*8975f5c5SAndroid Build Coastguard Worker[JNI glue]: /third_party/jni_zero/README.md 141*8975f5c5SAndroid Build Coastguard Worker 142*8975f5c5SAndroid Build Coastguard Worker### Step 8: Final Dexing 143*8975f5c5SAndroid Build Coastguard Worker 144*8975f5c5SAndroid Build Coastguard WorkerThis step is skipped when building using [Incremental Install]. 145*8975f5c5SAndroid Build Coastguard Worker 146*8975f5c5SAndroid Build Coastguard WorkerWhen `is_java_debug = true`: 147*8975f5c5SAndroid Build Coastguard Worker* [d8] merges all library `.dex.jar` files into a final `.mergeddex.jar`. 148*8975f5c5SAndroid Build Coastguard Worker 149*8975f5c5SAndroid Build Coastguard WorkerWhen `is_java_debug = false`: 150*8975f5c5SAndroid Build Coastguard Worker* [R8] performs whole-program optimization on all library `lib.java` `.jar` 151*8975f5c5SAndroid Build Coastguard Worker files and outputs a final `.r8dex.jar`. 152*8975f5c5SAndroid Build Coastguard Worker * For App Bundles, R8 creates a `.r8dex.jar` for each module. 153*8975f5c5SAndroid Build Coastguard Worker 154*8975f5c5SAndroid Build Coastguard Worker[Incremental Install]: /build/android/incremental_install/README.md 155*8975f5c5SAndroid Build Coastguard Worker[R8]: https://r8.googlesource.com/r8 156*8975f5c5SAndroid Build Coastguard Worker 157*8975f5c5SAndroid Build Coastguard Worker## Test APKs with apk_under_test 158*8975f5c5SAndroid Build Coastguard Worker 159*8975f5c5SAndroid Build Coastguard WorkerTest APKs are normal APKs that contain an `<instrumentation>` tag within their 160*8975f5c5SAndroid Build Coastguard Worker`AndroidManifest.xml`. If this tag specifies an `android:targetPackage` 161*8975f5c5SAndroid Build Coastguard Workerdifferent from itself, then Android will add that package's `classes.dex` to the 162*8975f5c5SAndroid Build Coastguard Workertest APK's Java classpath when run. In GN, you can enable this behavior using 163*8975f5c5SAndroid Build Coastguard Workerthe `apk_under_test` parameter on `instrumentation_test_apk` targets. Using it 164*8975f5c5SAndroid Build Coastguard Workeris discouraged if APKs have `proguard_enabled=true`. 165*8975f5c5SAndroid Build Coastguard Worker 166*8975f5c5SAndroid Build Coastguard Worker### Difference in Final Dex 167*8975f5c5SAndroid Build Coastguard Worker 168*8975f5c5SAndroid Build Coastguard WorkerWhen `enable_proguard=false`: 169*8975f5c5SAndroid Build Coastguard Worker* Any library depended on by the test APK that is also depended on by the 170*8975f5c5SAndroid Build Coastguard Worker apk-under-test is excluded from the test APK's final dex step. 171*8975f5c5SAndroid Build Coastguard Worker 172*8975f5c5SAndroid Build Coastguard WorkerWhen `enable_proguard=true`: 173*8975f5c5SAndroid Build Coastguard Worker* Test APKs cannot make use of the apk-under-test's dex because only symbols 174*8975f5c5SAndroid Build Coastguard Worker explicitly kept by `-keep` directives are guaranteed to exist after 175*8975f5c5SAndroid Build Coastguard Worker ProGuarding. As a work-around, test APKs include all of the apk-under-test's 176*8975f5c5SAndroid Build Coastguard Worker libraries directly in its own final dex such that the under-test apk's Java 177*8975f5c5SAndroid Build Coastguard Worker code is never used (because it is entirely shadowed by the test apk's dex). 178*8975f5c5SAndroid Build Coastguard Worker * We've found this configuration to be fragile, and are trying to [move away 179*8975f5c5SAndroid Build Coastguard Worker from it](https://bugs.chromium.org/p/chromium/issues/detail?id=890452). 180*8975f5c5SAndroid Build Coastguard Worker 181*8975f5c5SAndroid Build Coastguard Worker### Difference in GEN_JNI.java 182*8975f5c5SAndroid Build Coastguard Worker* Calling native methods using [JNI glue] requires that a `GEN_JNI.java` class 183*8975f5c5SAndroid Build Coastguard Worker be generated that contains all native methods for an APK. There cannot be 184*8975f5c5SAndroid Build Coastguard Worker conflicting `GEN_JNI` classes in both the test apk and the apk-under-test, so 185*8975f5c5SAndroid Build Coastguard Worker only the apk-under-test has one generated for it. As a result this, 186*8975f5c5SAndroid Build Coastguard Worker instrumentation test APKs that use apk-under-test cannot use native methods 187*8975f5c5SAndroid Build Coastguard Worker that aren't already part of the apk-under-test. 188*8975f5c5SAndroid Build Coastguard Worker 189*8975f5c5SAndroid Build Coastguard Worker## How to Generate Java Source Code 190*8975f5c5SAndroid Build Coastguard WorkerThere are two ways to go about generating source files: Annotation Processors 191*8975f5c5SAndroid Build Coastguard Workerand custom build steps. 192*8975f5c5SAndroid Build Coastguard Worker 193*8975f5c5SAndroid Build Coastguard Worker### Annotation Processors 194*8975f5c5SAndroid Build Coastguard Worker* These are run by `javac` as part of the compile step. 195*8975f5c5SAndroid Build Coastguard Worker* They **cannot** modify the source files that they apply to. They can only 196*8975f5c5SAndroid Build Coastguard Worker generate new sources. 197*8975f5c5SAndroid Build Coastguard Worker* Use these when: 198*8975f5c5SAndroid Build Coastguard Worker * an existing Annotation Processor does what you want 199*8975f5c5SAndroid Build Coastguard Worker (E.g. Dagger, AutoService, etc.), or 200*8975f5c5SAndroid Build Coastguard Worker * you need to understand Java types to do generation. 201*8975f5c5SAndroid Build Coastguard Worker 202*8975f5c5SAndroid Build Coastguard Worker### Custom Build Steps 203*8975f5c5SAndroid Build Coastguard Worker* These use discrete build actions to generate source files. 204*8975f5c5SAndroid Build Coastguard Worker * Some generate `.java` directly, but most generate a zip file of sources 205*8975f5c5SAndroid Build Coastguard Worker (called a `.srcjar`) to simplify the number of inputs / outputs. 206*8975f5c5SAndroid Build Coastguard Worker* Examples of existing templates: 207*8975f5c5SAndroid Build Coastguard Worker * `jinja_template`: Generates source files using [Jinja]. 208*8975f5c5SAndroid Build Coastguard Worker * `java_cpp_template`: Generates source files using the C preprocessor. 209*8975f5c5SAndroid Build Coastguard Worker * `java_cpp_enum`: Generates `@IntDef`s based on enums within `.h` files. 210*8975f5c5SAndroid Build Coastguard Worker * `java_cpp_strings`: Generates String constants based on strings defined in 211*8975f5c5SAndroid Build Coastguard Worker `.cc` files. 212*8975f5c5SAndroid Build Coastguard Worker* Custom build steps are preferred over Annotation Processors because they are 213*8975f5c5SAndroid Build Coastguard Worker generally easier to understand, and can run in parallel with other steps 214*8975f5c5SAndroid Build Coastguard Worker (rather than being tied to compiles). 215*8975f5c5SAndroid Build Coastguard Worker 216*8975f5c5SAndroid Build Coastguard Worker[Jinja]: https://palletsprojects.com/p/jinja/ 217*8975f5c5SAndroid Build Coastguard Worker 218*8975f5c5SAndroid Build Coastguard Worker## Static Analysis & Code Checks 219*8975f5c5SAndroid Build Coastguard Worker 220*8975f5c5SAndroid Build Coastguard WorkerSee [static_analysis.md](static_analysis.md) 221