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 | |
| 38 | The Google Cloud Storage bucket r8-releases can also be used as a simple |
| 39 | Maven repository using the following in a Gradle build file: |
| 40 | |
| 41 | maven { |
| 42 | url = uri("https://storage.googleapis.com/r8-releases/raw") |
| 43 | } |
| 44 | |
| 45 | See [Running D8](#running-d8) and [Running R8](#running-r8) below on how to invoke |
| 46 | D8 and R8 using the obtained `r8lib.jar` in place of `build/libs/r8.jar`. |
| 47 | |
| 48 | ## Downloading source and building |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 49 | |
| 50 | The R8 project uses [`depot_tools`](https://www.chromium.org/developers/how-tos/install-depot-tools) |
| 51 | from the chromium project to manage dependencies. Install `depot_tools` and add it to |
| 52 | your path before proceeding. |
| 53 | |
| 54 | The R8 project uses Java 8 language features and requires a Java 8 compiler |
| 55 | and runtime system. |
| 56 | |
| 57 | Typical steps to download and build: |
| 58 | |
| 59 | |
| 60 | $ git clone https://r8.googlesource.com/r8 |
| 61 | $ cd r8 |
Rico Wind | a6ccd88 | 2022-04-25 11:11:43 +0200 | [diff] [blame] | 62 | $ tools/gradle.py r8 |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 63 | |
| 64 | The `tools/gradle.py` script will bootstrap using depot_tools to download |
| 65 | 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] | 66 | 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] | 67 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 68 | ## <a name="running-d8"></a>Running D8 |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 69 | |
| 70 | 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] | 71 | D8 consumes class files and produce DEX. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 72 | |
| 73 | The most important option is whether to build in debug or release mode. Debug |
| 74 | is the default mode and includes debugging information in the resulting dex |
| 75 | files. Debugging information contains information about local variables used |
| 76 | when debugging dex code. This information is not useful when shipping final |
| 77 | Android apps to users and therefore, final builds should use the `--release` |
| 78 | flag to remove this debugging information to produce smaller dex files. |
| 79 | |
| 80 | Typical invocations of D8 to produce dex file(s) in the out directoy: |
| 81 | |
| 82 | Debug mode build: |
| 83 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 84 | $ 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 Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 89 | |
| 90 | Release mode build: |
| 91 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 92 | $ java -cp build/libs/r8.jar com.android.tools.r8.D8 |
Rico Wind | ae27204 | 2023-05-01 18:20:17 +0200 | [diff] [blame] | 93 | --release \ |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 94 | --min-api <min-api> \ |
Rico Wind | ae27204 | 2023-05-01 18:20:17 +0200 | [diff] [blame] | 95 | --output out \ |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 96 | --lib <android.jar/rt.jar> \ |
| 97 | input.jar |
| 98 | |
| 99 | 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] | 100 | |
| 101 | The full set of D8 options can be obtained by running the command line tool with |
| 102 | the `--help` option. |
| 103 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 104 | ## <a name="running-r8"></a>Running R8 |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 105 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 106 | R8 is a whole-program optimizing compiler with focus on shrinking the size of |
| 107 | programs. R8 uses the |
| 108 | [ProGuard configuration format](https://www.guardsquare.com/manual/configuration/usage) |
| 109 | for configuring the whole-program optimization including specifying the entry points |
| 110 | for an application. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 111 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 112 | R8 consumes class files and can output either DEX for Android apps or class files |
| 113 | for Java apps. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 114 | |
Søren Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 115 | Typical invocations of R8 to produce optimized DEX file(s) in the `out` directory: |
| 116 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 117 | $ 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 Gjesse | 46cc94c | 2023-04-14 10:32:51 +0200 | [diff] [blame] | 124 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 125 | 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] | 126 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 127 | The option `--lib` is passing the bootclasspath for the targeted runtime. |
| 128 | For targeting Android use an `android.jar` from the Android Platform SDK, typically |
| 129 | located in `~/Android/Sdk/platforms/android-XX`, where `XX` should be the latest |
| 130 | Android version. |
| 131 | |
| 132 | To produce class files pass the option `--classfile` and leave out `--min-api <min-api>`. |
| 133 | This 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 | |
| 144 | When producing output targeted for the JVM one can pass `$JAVA_HOME` to `-lib`. |
| 145 | R8 will then locate the Java bootclasspath from there. |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 146 | |
| 147 | The full set of R8 options can be obtained by running the command line tool with |
| 148 | the `--help` option. |
| 149 | |
Søren Gjesse | 43337c7 | 2023-05-01 11:30:14 +0200 | [diff] [blame] | 150 | R8 is not command line compatible with ProGuard, for instance keep rules cannot be passed |
| 151 | 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] | 152 | |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 153 | ## Testing |
| 154 | |
| 155 | Typical steps to run tests: |
| 156 | |
| 157 | $ tools/test.py --no_internal |
| 158 | |
| 159 | The `tools/test.py` script will use depot_tools to download a lot of tests |
| 160 | and test dependencies on the first run. This includes prebuilt version of the |
| 161 | art runtime on which to validate the produced dex code. |
| 162 | |
ager | 6e27cfb | 2017-09-29 09:47:52 -0700 | [diff] [blame] | 163 | ## Contributing |
| 164 | |
| 165 | In order to contribute to D8/R8 you have to sign the |
| 166 | [Contributor License Agreement](https://cla.developers.google.com/about/google-individual). |
| 167 | If 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 Gjesse | e4ba935 | 2021-10-26 17:01:15 +0200 | [diff] [blame] | 170 | Once 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 |
| 172 | contributor. |
| 173 | |
| 174 | After being added as a contributer you can upload your patches |
| 175 | using `git cl` which is available in `depot_tools`. Once you have a |
ager | 6e27cfb | 2017-09-29 09:47:52 -0700 | [diff] [blame] | 176 | change that you are happy with you should make sure that it passes |
| 177 | all tests and then upload the change to our code review tool using: |
| 178 | |
| 179 | $ git cl upload |
| 180 | |
| 181 | On your first upload you will be asked to acquire credentials. Follow the |
| 182 | instructions given by `git cl upload`. |
| 183 | |
| 184 | On successful uploads a link to the code review is printed in the |
| 185 | output of the upload command. In the code review tool you can |
| 186 | assign reviewers and mark the change ready for review. At that |
| 187 | point the code review tool will send emails to reviewers. |
| 188 | |
Mads Ager | 4c9a7fa | 2017-06-16 11:28:58 +0200 | [diff] [blame] | 189 | ## Getting help |
| 190 | |
Søren Gjesse | e4ba935 | 2021-10-26 17:01:15 +0200 | [diff] [blame] | 191 | For questions, reach out to us at |
| 192 | [r8-dev@googlegroups.com](mailto:r8-dev@googlegroups.com). |
Ian Zerny | 4ffb2c8 | 2017-12-11 10:54:13 +0100 | [diff] [blame] | 193 | |
| 194 | For D8, find known issues in the |
| 195 | [D8 issue tracker](https://issuetracker.google.com/issues?q=componentid:317603) |
| 196 | or file a new |
| 197 | [D8 bug report](https://issuetracker.google.com/issues/new?component=317603). |
| 198 | |
| 199 | For R8, find known issues in the |
| 200 | [R8 issue tracker](https://issuetracker.google.com/issues?q=componentid:326788) |
| 201 | or file a new |
| 202 | [R8 bug report](https://issuetracker.google.com/issues/new?component=326788). |