blob: 487df6d33717d01c47ef3933a263be018277177e [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
Søren Gjesse58fe0bd2023-10-24 10:27:36 +020038To get prebuilds which has not been processed by R8 replace `r8lib.jar` with `r8.jar`
39in the URLs above.
40
Søren Gjesse46cc94c2023-04-14 10:32:51 +020041The Google Cloud Storage bucket r8-releases can also be used as a simple
42Maven repository using the following in a Gradle build file:
43
44 maven {
45 url = uri("https://storage.googleapis.com/r8-releases/raw")
46 }
47
48See [Running D8](#running-d8) and [Running R8](#running-r8) below on how to invoke
49D8 and R8 using the obtained `r8lib.jar` in place of `build/libs/r8.jar`.
50
51## Downloading source and building
Mads Ager4c9a7fa2017-06-16 11:28:58 +020052
53The R8 project uses [`depot_tools`](https://www.chromium.org/developers/how-tos/install-depot-tools)
54from the chromium project to manage dependencies. Install `depot_tools` and add it to
55your path before proceeding.
56
57The R8 project uses Java 8 language features and requires a Java 8 compiler
58and runtime system.
59
60Typical steps to download and build:
61
62
63 $ git clone https://r8.googlesource.com/r8
64 $ cd r8
Rico Winda6ccd882022-04-25 11:11:43 +020065 $ tools/gradle.py r8
Mads Ager4c9a7fa2017-06-16 11:28:58 +020066
67The `tools/gradle.py` script will bootstrap using depot_tools to download
68a version of gradle to use for building on the first run. This will produce
Rico Winda6ccd882022-04-25 11:11:43 +020069a jar file: `build/libs/r8.jar` which contains both R8 and D8.
Mads Ager4c9a7fa2017-06-16 11:28:58 +020070
Christoffer Adamsen5b385e22024-08-23 10:26:16 +020071## Setting up IntelliJ
72
73Follow the instructions in the above section.
74
751. Open the project `r8/d8_r8` in IntelliJ
762. Navigate to "Settings" > "Build, Execution, Deployment" > "Build Tools" > "Gradle"
77 1. Select `r8/third_party/gradle` as "Local installation" in "Distribution"
78 2. Select `r8/third_party/openjdk/jdk-11/linux` as "Gradle JVM"
793. Sync the project using Gradle
80
81In order to run tests it may currently be necessary to run `tools/test.py` from the command line first.
82
Søren Gjesse46cc94c2023-04-14 10:32:51 +020083## <a name="running-d8"></a>Running D8
Mads Ager4c9a7fa2017-06-16 11:28:58 +020084
85The D8 dexer has a simple command-line interface with only a few options.
Søren Gjesse46cc94c2023-04-14 10:32:51 +020086D8 consumes class files and produce DEX.
Mads Ager4c9a7fa2017-06-16 11:28:58 +020087
88The most important option is whether to build in debug or release mode. Debug
89is the default mode and includes debugging information in the resulting dex
90files. Debugging information contains information about local variables used
91when debugging dex code. This information is not useful when shipping final
92Android apps to users and therefore, final builds should use the `--release`
93flag to remove this debugging information to produce smaller dex files.
94
95Typical invocations of D8 to produce dex file(s) in the out directoy:
96
97Debug mode build:
98
Søren Gjesse43337c72023-05-01 11:30:14 +020099 $ java -cp build/libs/r8.jar com.android.tools.r8.D8 \
100 --min-api <min-api> \
101 --output out \
102 --lib <android.jar/rt.jar> \
103 input.jar
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200104
105Release mode build:
106
Søren Gjesse43337c72023-05-01 11:30:14 +0200107 $ java -cp build/libs/r8.jar com.android.tools.r8.D8
Rico Windae272042023-05-01 18:20:17 +0200108 --release \
Søren Gjesse43337c72023-05-01 11:30:14 +0200109 --min-api <min-api> \
Rico Windae272042023-05-01 18:20:17 +0200110 --output out \
Søren Gjesse43337c72023-05-01 11:30:14 +0200111 --lib <android.jar/rt.jar> \
112 input.jar
113
114See [Running R8](#running-r8) for information on options `--min-api` and `--lib`.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200115
116The full set of D8 options can be obtained by running the command line tool with
117the `--help` option.
118
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200119## <a name="running-r8"></a>Running R8
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200120
Søren Gjesse43337c72023-05-01 11:30:14 +0200121R8 is a whole-program optimizing compiler with focus on shrinking the size of
122programs. R8 uses the
123[ProGuard configuration format](https://www.guardsquare.com/manual/configuration/usage)
124for configuring the whole-program optimization including specifying the entry points
125for an application.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200126
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200127R8 consumes class files and can output either DEX for Android apps or class files
128for Java apps.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200129
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200130Typical invocations of R8 to produce optimized DEX file(s) in the `out` directory:
131
Søren Gjesse43337c72023-05-01 11:30:14 +0200132 $ java -cp build/libs/r8.jar com.android.tools.r8.R8 \
133 --release \
134 --min-api <min-api> \
135 --output out \
136 --pg-conf proguard.cfg \
137 --lib <android.jar/rt.jar> \
138 input.jar
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200139
Søren Gjesse43337c72023-05-01 11:30:14 +0200140This produce DEX targeting Android devices with a API level of `<min-api>` and above.
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200141
Søren Gjesse43337c72023-05-01 11:30:14 +0200142The option `--lib` is passing the bootclasspath for the targeted runtime.
143For targeting Android use an `android.jar` from the Android Platform SDK, typically
144located in `~/Android/Sdk/platforms/android-XX`, where `XX` should be the latest
145Android version.
146
147To produce class files pass the option `--classfile` and leave out `--min-api <min-api>`.
148This invocation will provide optimized Java classfiles in `output.jar`:
149
150 $ java -cp build/libs/r8.jar com.android.tools.r8.R8 \
151 --release \
152 --classfile \
153 --output output.jar \
154 --pg-conf proguard.cfg \
155 --lib <android.jar/rt.jar> \
156 input.jar
157
158
159When producing output targeted for the JVM one can pass `$JAVA_HOME` to `-lib`.
160R8 will then locate the Java bootclasspath from there.
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200161
162The full set of R8 options can be obtained by running the command line tool with
163the `--help` option.
164
Søren Gjesse43337c72023-05-01 11:30:14 +0200165R8 is not command line compatible with ProGuard, for instance keep rules cannot be passed
166on the command line, but have to be passed in a file using the `--pg-conf` option.
Søren Gjesse46cc94c2023-04-14 10:32:51 +0200167
Søren Gjesse58fe0bd2023-10-24 10:27:36 +0200168## <a name="replacing-r8-in-agp"></a>Replacing R8 in Android Gradle plugin
169
170Android Gradle plugin (AGP) ships with R8 embedded (as part of the `builder.jar` from
171`com.android.tools.build:builder:<agp version>` on https://maven.google.com).
172
173To override the embedded version with a prebuilt R8 with version `<version>`, merge
174the following into the top level `settings.gradle` or `settings.gradle.kts`:
175```
176pluginManagement {
177 buildscript {
178 repositories {
179 mavenCentral()
180 maven {
181 url = uri("https://storage.googleapis.com/r8-releases/raw")
182 }
183 }
184 dependencies {
185 classpath("com.android.tools:r8:<version>")
186 }
187 }
188}
189```
190To override the embedded version with a downloaded or freshly built `<path>/r8.jar` merge
191the following into the top level `settings.gradle` or `settings.gradle.kts`:
192```
193pluginManagement {
194 buildscript {
195 dependencies {
196 classpath(files("<path>/r8.jar"))
197 }
198 }
199}
200```
201
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200202## Testing
203
204Typical steps to run tests:
205
206 $ tools/test.py --no_internal
207
208The `tools/test.py` script will use depot_tools to download a lot of tests
209and test dependencies on the first run. This includes prebuilt version of the
210art runtime on which to validate the produced dex code.
211
ager6e27cfb2017-09-29 09:47:52 -0700212## Contributing
213
214In order to contribute to D8/R8 you have to sign the
215[Contributor License Agreement](https://cla.developers.google.com/about/google-individual).
216If your contribution is owned by your employer you need the
217[Corporate Contributor License Agreement](https://cla.developers.google.com/about/google-corporate).
218
Søren Gjessee4ba9352021-10-26 17:01:15 +0200219Once the license agreement is in place, please send an email to
220[r8-dev@googlegroups.com](mailto:r8-dev@googlegroups.com) to be added as a
221contributor.
222
223After being added as a contributer you can upload your patches
224using `git cl` which is available in `depot_tools`. Once you have a
ager6e27cfb2017-09-29 09:47:52 -0700225change that you are happy with you should make sure that it passes
226all tests and then upload the change to our code review tool using:
227
228 $ git cl upload
229
230On your first upload you will be asked to acquire credentials. Follow the
231instructions given by `git cl upload`.
232
233On successful uploads a link to the code review is printed in the
234output of the upload command. In the code review tool you can
235assign reviewers and mark the change ready for review. At that
236point the code review tool will send emails to reviewers.
237
Mads Ager4c9a7fa2017-06-16 11:28:58 +0200238## Getting help
239
Søren Gjessee4ba9352021-10-26 17:01:15 +0200240For questions, reach out to us at
241[r8-dev@googlegroups.com](mailto:r8-dev@googlegroups.com).
Ian Zerny4ffb2c82017-12-11 10:54:13 +0100242
243For D8, find known issues in the
244[D8 issue tracker](https://issuetracker.google.com/issues?q=componentid:317603)
245or file a new
246[D8 bug report](https://issuetracker.google.com/issues/new?component=317603).
247
248For R8, find known issues in the
249[R8 issue tracker](https://issuetracker.google.com/issues?q=componentid:326788)
250or file a new
251[R8 bug report](https://issuetracker.google.com/issues/new?component=326788).