blob: 0a085c112f1724b55eaf248a60889a1cd21342f5 [file] [log] [blame] [view]
Mads Ager4c9a7fa2017-06-16 11:28:58 +02001# D8 dexer and R8 shrinker
Mads Ager418d1ca2017-05-22 09:35:49 +02002
Mads Ager4c9a7fa2017-06-16 11:28:58 +02003The R8 repo contains two tools:
Mads Ager418d1ca2017-05-22 09:35:49 +02004
Mads Ager4c9a7fa2017-06-16 11:28:58 +02005- D8 is a dexer that converts java byte code to dex code.
6- R8 is a java program shrinking and minification tool that converts java byte
7 code to optimized dex code.
8
Søren Gjesse46cc94c2023-04-14 10:32:51 +02009D8 is a replacement for the DX dexer and R8 is an alternative to
10the [ProGuard](https://www.guardsquare.com/en/proguard) shrinking and
Mads Ager4c9a7fa2017-06-16 11:28:58 +020011minification tool.
12
Søren Gjesse46cc94c2023-04-14 10:32:51 +020013## Obtaining prebuilts
14
15There are several places to obtain a prebuilt version without building it
16yourself.
17
18The stable release versions shipped with Android Studio are available from
19the Google Maven repository, see
20https://maven.google.com/web/index.html#com.android.tools:r8.
21
22Our [CI](https://ci.chromium.org/p/r8/g/main/console) builds for each commit and
23stores all build artifacts in Google Cloud Storage in the bucket r8-releases.
24
25To obtain a prebuild from the CI for a specifc version (including both
26stable and `-dev` versions), download from the following URL:
27
28 https://storage.googleapis.com/r8-releases/raw/<version>/r8lib.jar
29
30To obtain a prebuild from the CI for a specifc main branch hash, download from the
31following URL:
32
33 https://storage.googleapis.com/r8-releases/raw/main/<hash>/r8lib.jar
34
35The prebuilt JARs have been processed by R8, and for each build the corresponding
36mapping file is located together with the prebuild under the name `r8lib.jar.map`.
37
38The Google Cloud Storage bucket r8-releases can also be used as a simple
39Maven repository using the following in a Gradle build file:
40
41 maven {
42 url = uri("https://storage.googleapis.com/r8-releases/raw")
43 }
44
45See [Running D8](#running-d8) and [Running R8](#running-r8) below on how to invoke
46D8 and R8 using the obtained `r8lib.jar` in place of `build/libs/r8.jar`.
47
48## Downloading source and building
Mads Ager4c9a7fa2017-06-16 11:28:58 +020049
50The R8 project uses [`depot_tools`](https://www.chromium.org/developers/how-tos/install-depot-tools)
51from the chromium project to manage dependencies. Install `depot_tools` and add it to
52your path before proceeding.
53
54The R8 project uses Java 8 language features and requires a Java 8 compiler
55and runtime system.
56
57Typical steps to download and build:
58
59
60 $ git clone https://r8.googlesource.com/r8
61 $ cd r8
Rico Winda6ccd882022-04-25 11:11:43 +020062 $ tools/gradle.py r8
Mads Ager4c9a7fa2017-06-16 11:28:58 +020063
64The `tools/gradle.py` script will bootstrap using depot_tools to download
65a version of gradle to use for building on the first run. This will produce
Rico Winda6ccd882022-04-25 11:11:43 +020066a jar file: `build/libs/r8.jar` which contains both R8 and D8.
Mads Ager4c9a7fa2017-06-16 11:28:58 +020067
Søren Gjesse46cc94c2023-04-14 10:32:51 +020068## <a name="running-d8"></a>Running D8
Mads Ager4c9a7fa2017-06-16 11:28:58 +020069
70The D8 dexer has a simple command-line interface with only a few options.
Søren Gjesse46cc94c2023-04-14 10:32:51 +020071D8 consumes class files and produce DEX.
Mads Ager4c9a7fa2017-06-16 11:28:58 +020072
73The most important option is whether to build in debug or release mode. Debug
74is the default mode and includes debugging information in the resulting dex
75files. Debugging information contains information about local variables used
76when debugging dex code. This information is not useful when shipping final
77Android apps to users and therefore, final builds should use the `--release`
78flag to remove this debugging information to produce smaller dex files.
79
80Typical invocations of D8 to produce dex file(s) in the out directoy:
81
82Debug mode build:
83
Søren Gjesse43337c72023-05-01 11:30:14 +020084 $ java -cp build/libs/r8.jar com.android.tools.r8.D8 \
85 --min-api <min-api> \
86 --output out \
87 --lib <android.jar/rt.jar> \
88 input.jar
Mads Ager4c9a7fa2017-06-16 11:28:58 +020089
90Release mode build:
91
Søren Gjesse43337c72023-05-01 11:30:14 +020092 $ java -cp build/libs/r8.jar com.android.tools.r8.D8
Rico Windae272042023-05-01 18:20:17 +020093 --release \
Søren Gjesse43337c72023-05-01 11:30:14 +020094 --min-api <min-api> \
Rico Windae272042023-05-01 18:20:17 +020095 --output out \
Søren Gjesse43337c72023-05-01 11:30:14 +020096 --lib <android.jar/rt.jar> \
97 input.jar
98
99See [Running R8](#running-r8) for information on options `--min-api` and `--lib`.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200100
101The full set of D8 options can be obtained by running the command line tool with
102the `--help` option.
103
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200104## <a name="running-r8"></a>Running R8
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200105
Søren Gjesse43337c72023-05-01 11:30:14 +0200106R8 is a whole-program optimizing compiler with focus on shrinking the size of
107programs. R8 uses the
108[ProGuard configuration format](https://www.guardsquare.com/manual/configuration/usage)
109for configuring the whole-program optimization including specifying the entry points
110for an application.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200111
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200112R8 consumes class files and can output either DEX for Android apps or class files
113for Java apps.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200114
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200115Typical invocations of R8 to produce optimized DEX file(s) in the `out` directory:
116
Søren Gjesse43337c72023-05-01 11:30:14 +0200117 $ java -cp build/libs/r8.jar com.android.tools.r8.R8 \
118 --release \
119 --min-api <min-api> \
120 --output out \
121 --pg-conf proguard.cfg \
122 --lib <android.jar/rt.jar> \
123 input.jar
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200124
Søren Gjesse43337c72023-05-01 11:30:14 +0200125This produce DEX targeting Android devices with a API level of `<min-api>` and above.
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200126
Søren Gjesse43337c72023-05-01 11:30:14 +0200127The option `--lib` is passing the bootclasspath for the targeted runtime.
128For targeting Android use an `android.jar` from the Android Platform SDK, typically
129located in `~/Android/Sdk/platforms/android-XX`, where `XX` should be the latest
130Android version.
131
132To produce class files pass the option `--classfile` and leave out `--min-api <min-api>`.
133This invocation will provide optimized Java classfiles in `output.jar`:
134
135 $ java -cp build/libs/r8.jar com.android.tools.r8.R8 \
136 --release \
137 --classfile \
138 --output output.jar \
139 --pg-conf proguard.cfg \
140 --lib <android.jar/rt.jar> \
141 input.jar
142
143
144When producing output targeted for the JVM one can pass `$JAVA_HOME` to `-lib`.
145R8 will then locate the Java bootclasspath from there.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200146
147The full set of R8 options can be obtained by running the command line tool with
148the `--help` option.
149
Søren Gjesse43337c72023-05-01 11:30:14 +0200150R8 is not command line compatible with ProGuard, for instance keep rules cannot be passed
151on the command line, but have to be passed in a file using the `--pg-conf` option.
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200152
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200153## Testing
154
155Typical steps to run tests:
156
157 $ tools/test.py --no_internal
158
159The `tools/test.py` script will use depot_tools to download a lot of tests
160and test dependencies on the first run. This includes prebuilt version of the
161art runtime on which to validate the produced dex code.
162
ager6e27cfb2017-09-29 09:47:52 -0700163## Contributing
164
165In order to contribute to D8/R8 you have to sign the
166[Contributor License Agreement](https://cla.developers.google.com/about/google-individual).
167If your contribution is owned by your employer you need the
168[Corporate Contributor License Agreement](https://cla.developers.google.com/about/google-corporate).
169
Søren Gjessee4ba9352021-10-26 17:01:15 +0200170Once the license agreement is in place, please send an email to
171[r8-dev@googlegroups.com](mailto:r8-dev@googlegroups.com) to be added as a
172contributor.
173
174After being added as a contributer you can upload your patches
175using `git cl` which is available in `depot_tools`. Once you have a
ager6e27cfb2017-09-29 09:47:52 -0700176change that you are happy with you should make sure that it passes
177all tests and then upload the change to our code review tool using:
178
179 $ git cl upload
180
181On your first upload you will be asked to acquire credentials. Follow the
182instructions given by `git cl upload`.
183
184On successful uploads a link to the code review is printed in the
185output of the upload command. In the code review tool you can
186assign reviewers and mark the change ready for review. At that
187point the code review tool will send emails to reviewers.
188
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200189## Getting help
190
Søren Gjessee4ba9352021-10-26 17:01:15 +0200191For questions, reach out to us at
192[r8-dev@googlegroups.com](mailto:r8-dev@googlegroups.com).
Ian Zerny4ffb2c82017-12-11 10:54:13 +0100193
194For D8, find known issues in the
195[D8 issue tracker](https://issuetracker.google.com/issues?q=componentid:317603)
196or file a new
197[D8 bug report](https://issuetracker.google.com/issues/new?component=317603).
198
199For R8, find known issues in the
200[R8 issue tracker](https://issuetracker.google.com/issues?q=componentid:326788)
201or file a new
202[R8 bug report](https://issuetracker.google.com/issues/new?component=326788).