blob: f5887b4580bcb86d02cb93ff1aae2db79c7508b0 [file] [log] [blame] [view]
jingwen1d897e22019-07-26 07:20:10 -07001---
2layout: documentation
3title: Android Build Performance
4---
5
6# Android Build Performance
7
8This document contains information on optimizing build performance for Android
9apps specifically. For general build performance optimization with Bazel, see
10[Optimizing Performance](skylark/performance.html).
11
12## Recommended flags
13
14The flags are in the
15[`bazelrc` configuration syntax](guide.html#bazelrc-syntax-and-semantics), so
16they can be pasted directly into a `bazelrc` file and invoked with
17`--config=<configuration_name>` on the command line.
18
19**Profiling performance**
20
twerth56fd3582020-08-11 05:57:37 -070021Bazel writes a JSON trace profile by default to a file called
22`command.profile.gz` in Bazel's output base.
23See the [JSON Profile documentation](skylark/performance.html#json-profile) for
jingwen1d897e22019-07-26 07:20:10 -070024how to read and interact with the profile.
25
jingwen1d897e22019-07-26 07:20:10 -070026**Persistent workers for Android build actions**.
27
28A subset of Android build actions has support for
29[persistent workers](https://blog.bazel.build/2015/12/10/java-workers.html).
30
31These 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
47Enabling workers can result in better build performance by saving on JVM
48startup costs from invoking each of these tools, but at the cost of increased
49memory usage on the system by persisting them.
50
51To enable workers for these actions, apply these flags with
52`--config=android_workers` on the command line:
53
54```
55build:android_workers --strategy=DexBuilder=worker
56build:android_workers --strategy=Javac=worker
57build: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
69build:android_workers --persistent_android_resource_processor
70```
71
72The 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)
74by capping the number of instances for each action to `1` or `2`, although this
75may vary depending on the system Bazel is running on, and the project being
76built.
77
78To cap the number of instances for an action, apply these flags:
79
80```
81build:android_workers --worker_max_instances=DexBuilder=2
82build:android_workers --worker_max_instances=Javac=2
83build:android_workers --worker_max_instances=Desugar=2
84build: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
91performance 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
97The `--experimental_multi_threaded_digest` flag is useful for optimizing digest
98computation on SSDs.