Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 1 | # D8 dexer and R8 shrinker |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 2 | |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 3 | The R8 repo contains two tools: |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 4 | |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 5 | - 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 Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 9 | D8 is a replacement for the DX dexer and R8 is an alternative to |
| 10 | the [ProGuard](https://www.guardsquare.com/en/proguard) shrinking and |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 11 | minification tool. |
| 12 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 13 | ## Obtaining prebuilts |
| 14 | |
| 15 | There are several places to obtain a prebuilt version without building it |
| 16 | yourself. |
| 17 | |
| 18 | The stable release versions shipped with Android Studio are available from |
| 19 | the Google Maven repository, see |
| 20 | https://maven.google.com/web/index.html#com.android.tools:r8. |
| 21 | |
| 22 | Our [CI](https://ci.chromium.org/p/r8/g/main/console) builds for each commit and |
| 23 | stores all build artifacts in Google Cloud Storage in the bucket r8-releases. |
| 24 | |
| 25 | To obtain a prebuild from the CI for a specifc version (including both |
| 26 | stable and `-dev` versions), download from the following URL: |
| 27 | |
| 28 | https://storage.googleapis.com/r8-releases/raw/<version>/r8lib.jar |
| 29 | |
| 30 | To obtain a prebuild from the CI for a specifc main branch hash, download from the |
| 31 | following URL: |
| 32 | |
| 33 | https://storage.googleapis.com/r8-releases/raw/main/<hash>/r8lib.jar |
| 34 | |
| 35 | The prebuilt JARs have been processed by R8, and for each build the corresponding |
| 36 | mapping file is located together with the prebuild under the name `r8lib.jar.map`. |
| 37 | |
Søren Gjesse | 58fe0bd | 2023-10-24 10:27:36 +0200 | [diff] [blame] | 38 | To get prebuilds which has not been processed by R8 replace `r8lib.jar` with `r8.jar` |
| 39 | in the URLs above. |
| 40 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 41 | The Google Cloud Storage bucket r8-releases can also be used as a simple |
| 42 | Maven repository using the following in a Gradle build file: |
| 43 | |
| 44 | maven { |
| 45 | url = uri("https://storage.googleapis.com/r8-releases/raw") |
| 46 | } |
| 47 | |
| 48 | See [Running D8](#running-d8) and [Running R8](#running-r8) below on how to invoke |
| 49 | D8 and R8 using the obtained `r8lib.jar` in place of `build/libs/r8.jar`. |
| 50 | |
| 51 | ## Downloading source and building |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 52 | |
| 53 | The R8 project uses [`depot_tools`](https://www.chromium.org/developers/how-tos/install-depot-tools) |
| 54 | from the chromium project to manage dependencies. Install `depot_tools` and add it to |
| 55 | your path before proceeding. |
| 56 | |
| 57 | The R8 project uses Java 8 language features and requires a Java 8 compiler |
| 58 | and runtime system. |
| 59 | |
| 60 | Typical steps to download and build: |
| 61 | |
| 62 | |
| 63 | $ git clone https://r8.googlesource.com/r8 |
| 64 | $ cd r8 |
Rico Wind | a6ccd88 | 2022-04-25 11:11:43 +0200 | [diff] [blame] | 65 | $ tools/gradle.py r8 |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 66 | |
| 67 | The `tools/gradle.py` script will bootstrap using depot_tools to download |
| 68 | a version of gradle to use for building on the first run. This will produce |
Rico Wind | a6ccd88 | 2022-04-25 11:11:43 +0200 | [diff] [blame] | 69 | a jar file: `build/libs/r8.jar` which contains both R8 and D8. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 70 | |
Christoffer Adamsen | 5b385e2 | 2024-08-23 10:26:16 +0200 | [diff] [blame] | 71 | ## Setting up IntelliJ |
| 72 | |
| 73 | Follow the instructions in the above section. |
| 74 | |
| 75 | 1. Open the project `r8/d8_r8` in IntelliJ |
| 76 | 2. 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" |
| 79 | 3. Sync the project using Gradle |
| 80 | |
| 81 | In order to run tests it may currently be necessary to run `tools/test.py` from the command line first. |
| 82 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 83 | ## <a name="running-d8"></a>Running D8 |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 84 | |
| 85 | The D8 dexer has a simple command-line interface with only a few options. |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 86 | D8 consumes class files and produce DEX. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 87 | |
| 88 | The most important option is whether to build in debug or release mode. Debug |
| 89 | is the default mode and includes debugging information in the resulting dex |
| 90 | files. Debugging information contains information about local variables used |
| 91 | when debugging dex code. This information is not useful when shipping final |
| 92 | Android apps to users and therefore, final builds should use the `--release` |
| 93 | flag to remove this debugging information to produce smaller dex files. |
| 94 | |
| 95 | Typical invocations of D8 to produce dex file(s) in the out directoy: |
| 96 | |
| 97 | Debug mode build: |
| 98 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 99 | $ 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 Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 104 | |
| 105 | Release mode build: |
| 106 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 107 | $ java -cp build/libs/r8.jar com.android.tools.r8.D8 |
Rico Wind | ae27204 | 2023-05-01 18:20:17 +0200 | [diff] [blame] | 108 | --release \ |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 109 | --min-api <min-api> \ |
Rico Wind | ae27204 | 2023-05-01 18:20:17 +0200 | [diff] [blame] | 110 | --output out \ |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 111 | --lib <android.jar/rt.jar> \ |
| 112 | input.jar |
| 113 | |
| 114 | See [Running R8](#running-r8) for information on options `--min-api` and `--lib`. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 115 | |
| 116 | The full set of D8 options can be obtained by running the command line tool with |
| 117 | the `--help` option. |
| 118 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 119 | ## <a name="running-r8"></a>Running R8 |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 120 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 121 | R8 is a whole-program optimizing compiler with focus on shrinking the size of |
| 122 | programs. R8 uses the |
| 123 | [ProGuard configuration format](https://www.guardsquare.com/manual/configuration/usage) |
| 124 | for configuring the whole-program optimization including specifying the entry points |
| 125 | for an application. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 126 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 127 | R8 consumes class files and can output either DEX for Android apps or class files |
| 128 | for Java apps. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 129 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 130 | Typical invocations of R8 to produce optimized DEX file(s) in the `out` directory: |
| 131 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 132 | $ 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 Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 139 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 140 | This produce DEX targeting Android devices with a API level of `<min-api>` and above. |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 141 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 142 | The option `--lib` is passing the bootclasspath for the targeted runtime. |
| 143 | For targeting Android use an `android.jar` from the Android Platform SDK, typically |
| 144 | located in `~/Android/Sdk/platforms/android-XX`, where `XX` should be the latest |
| 145 | Android version. |
| 146 | |
| 147 | To produce class files pass the option `--classfile` and leave out `--min-api <min-api>`. |
| 148 | This 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 | |
| 159 | When producing output targeted for the JVM one can pass `$JAVA_HOME` to `-lib`. |
| 160 | R8 will then locate the Java bootclasspath from there. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 161 | |
| 162 | The full set of R8 options can be obtained by running the command line tool with |
| 163 | the `--help` option. |
| 164 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 165 | R8 is not command line compatible with ProGuard, for instance keep rules cannot be passed |
| 166 | on the command line, but have to be passed in a file using the `--pg-conf` option. |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 167 | |
Søren Gjesse | 58fe0bd | 2023-10-24 10:27:36 +0200 | [diff] [blame] | 168 | ## <a name="replacing-r8-in-agp"></a>Replacing R8 in Android Gradle plugin |
| 169 | |
| 170 | Android 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 | |
| 173 | To override the embedded version with a prebuilt R8 with version `<version>`, merge |
| 174 | the following into the top level `settings.gradle` or `settings.gradle.kts`: |
| 175 | ``` |
| 176 | pluginManagement { |
| 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 | ``` |
| 190 | To override the embedded version with a downloaded or freshly built `<path>/r8.jar` merge |
| 191 | the following into the top level `settings.gradle` or `settings.gradle.kts`: |
| 192 | ``` |
| 193 | pluginManagement { |
| 194 | buildscript { |
| 195 | dependencies { |
| 196 | classpath(files("<path>/r8.jar")) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | ``` |
| 201 | |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 202 | ## Testing |
| 203 | |
| 204 | Typical steps to run tests: |
| 205 | |
| 206 | $ tools/test.py --no_internal |
| 207 | |
| 208 | The `tools/test.py` script will use depot_tools to download a lot of tests |
| 209 | and test dependencies on the first run. This includes prebuilt version of the |
| 210 | art runtime on which to validate the produced dex code. |
| 211 | |
ager | 6e27cfb | 2017-09-29 09:47:52 -0700 | [diff] [blame] | 212 | ## Contributing |
| 213 | |
| 214 | In order to contribute to D8/R8 you have to sign the |
| 215 | [Contributor License Agreement](https://cla.developers.google.com/about/google-individual). |
| 216 | If 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 Gjesse | e4ba935 | 2021-10-26 17:01:15 +0200 | [diff] [blame] | 219 | Once 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 |
| 221 | contributor. |
| 222 | |
| 223 | After being added as a contributer you can upload your patches |
| 224 | using `git cl` which is available in `depot_tools`. Once you have a |
ager | 6e27cfb | 2017-09-29 09:47:52 -0700 | [diff] [blame] | 225 | change that you are happy with you should make sure that it passes |
| 226 | all tests and then upload the change to our code review tool using: |
| 227 | |
| 228 | $ git cl upload |
| 229 | |
| 230 | On your first upload you will be asked to acquire credentials. Follow the |
| 231 | instructions given by `git cl upload`. |
| 232 | |
| 233 | On successful uploads a link to the code review is printed in the |
| 234 | output of the upload command. In the code review tool you can |
| 235 | assign reviewers and mark the change ready for review. At that |
| 236 | point the code review tool will send emails to reviewers. |
| 237 | |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 238 | ## Getting help |
| 239 | |
Søren Gjesse | e4ba935 | 2021-10-26 17:01:15 +0200 | [diff] [blame] | 240 | For questions, reach out to us at |
| 241 | [r8-dev@googlegroups.com](mailto:r8-dev@googlegroups.com). |
Ian Zerny | 4ffb2c8 | 2017-12-11 10:54:13 +0100 | [diff] [blame] | 242 | |
| 243 | For D8, find known issues in the |
| 244 | [D8 issue tracker](https://issuetracker.google.com/issues?q=componentid:317603) |
| 245 | or file a new |
| 246 | [D8 bug report](https://issuetracker.google.com/issues/new?component=317603). |
| 247 | |
| 248 | For R8, find known issues in the |
| 249 | [R8 issue tracker](https://issuetracker.google.com/issues?q=componentid:326788) |
| 250 | or file a new |
| 251 | [R8 bug report](https://issuetracker.google.com/issues/new?component=326788). |