jingwen | 1d897e2 | 2019-07-26 07:20:10 -0700 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
| 3 | title: Android Build Performance |
| 4 | --- |
| 5 | |
| 6 | # Android Build Performance |
| 7 | |
| 8 | This document contains information on optimizing build performance for Android |
| 9 | apps specifically. For general build performance optimization with Bazel, see |
| 10 | [Optimizing Performance](skylark/performance.html). |
| 11 | |
| 12 | ## Recommended flags |
| 13 | |
| 14 | The flags are in the |
| 15 | [`bazelrc` configuration syntax](guide.html#bazelrc-syntax-and-semantics), so |
| 16 | they can be pasted directly into a `bazelrc` file and invoked with |
| 17 | `--config=<configuration_name>` on the command line. |
| 18 | |
| 19 | **Profiling performance** |
| 20 | |
twerth | 56fd358 | 2020-08-11 05:57:37 -0700 | [diff] [blame] | 21 | Bazel writes a JSON trace profile by default to a file called |
| 22 | `command.profile.gz` in Bazel's output base. |
| 23 | See the [JSON Profile documentation](skylark/performance.html#json-profile) for |
jingwen | 1d897e2 | 2019-07-26 07:20:10 -0700 | [diff] [blame] | 24 | how to read and interact with the profile. |
| 25 | |
jingwen | 1d897e2 | 2019-07-26 07:20:10 -0700 | [diff] [blame] | 26 | **Persistent workers for Android build actions**. |
| 27 | |
| 28 | A subset of Android build actions has support for |
| 29 | [persistent workers](https://blog.bazel.build/2015/12/10/java-workers.html). |
| 30 | |
| 31 | These actions' mnemonics are: |
| 32 | |
| 33 | * DexBuilder |
| 34 | * Javac |
| 35 | * Desugar |
| 36 | * AaptPackage |
| 37 | * AndroidResourceParser |
| 38 | * AndroidResourceValidator |
| 39 | * AndroidResourceCompiler |
| 40 | * RClassGenerator |
| 41 | * AndroidResourceLink |
| 42 | * AndroidAapt2 |
| 43 | * AndroidAssetMerger |
| 44 | * AndroidResourceMerger |
| 45 | * AndroidCompiledResourceMerger |
| 46 | |
| 47 | Enabling workers can result in better build performance by saving on JVM |
| 48 | startup costs from invoking each of these tools, but at the cost of increased |
| 49 | memory usage on the system by persisting them. |
| 50 | |
| 51 | To enable workers for these actions, apply these flags with |
| 52 | `--config=android_workers` on the command line: |
| 53 | |
| 54 | ``` |
| 55 | build:android_workers --strategy=DexBuilder=worker |
| 56 | build:android_workers --strategy=Javac=worker |
| 57 | build:android_workers --strategy=Desugar=worker |
| 58 | |
| 59 | # A wrapper flag for these resource processing actions: |
| 60 | # - AndroidResourceParser |
| 61 | # - AndroidResourceValidator |
| 62 | # - AndroidResourceCompiler |
| 63 | # - RClassGenerator |
| 64 | # - AndroidResourceLink |
| 65 | # - AndroidAapt2 |
| 66 | # - AndroidAssetMerger |
| 67 | # - AndroidResourceMerger |
| 68 | # - AndroidCompiledResourceMerger |
| 69 | build:android_workers --persistent_android_resource_processor |
| 70 | ``` |
| 71 | |
| 72 | The default number of persistent workers created per action is `4`. We have |
| 73 | [measured improved build performance](https://github.com/bazelbuild/bazel/issues/8586#issuecomment-500070549) |
| 74 | by capping the number of instances for each action to `1` or `2`, although this |
| 75 | may vary depending on the system Bazel is running on, and the project being |
| 76 | built. |
| 77 | |
| 78 | To cap the number of instances for an action, apply these flags: |
| 79 | |
| 80 | ``` |
| 81 | build:android_workers --worker_max_instances=DexBuilder=2 |
| 82 | build:android_workers --worker_max_instances=Javac=2 |
| 83 | build:android_workers --worker_max_instances=Desugar=2 |
| 84 | build:android_workers --worker_max_instances=AaptPackage=2 |
| 85 | # .. and so on for each action you're interested in. |
| 86 | ``` |
| 87 | |
| 88 | **Using AAPT2** |
| 89 | |
| 90 | [`aapt2`](https://developer.android.com/studio/command-line/aapt2) has improved |
| 91 | performance over `aapt` and also creates smaller APKs. To use `aapt2`, use the |
| 92 | `--android_aapt=aapt2` flag or set `aapt2` on the `aapt_version` on |
| 93 | `android_binary` and `android_local_test`. |
| 94 | |
| 95 | **SSD optimizations** |
| 96 | |
| 97 | The `--experimental_multi_threaded_digest` flag is useful for optimizing digest |
| 98 | computation on SSDs. |