Giter Club home page Giter Club logo

r8's Introduction

D8 dexer and R8 shrinker

The R8 repo contains two tools:

  • D8 is a dexer that converts java byte code to dex code.
  • R8 is a java program shrinking and minification tool that converts java byte code to optimized dex code.

D8 is a replacement for the DX dexer and R8 is an alternative to the ProGuard shrinking and minification tool.

Obtaining prebuilts

There are several places to obtain a prebuilt version without building it yourself.

The stable release versions shipped with Android Studio are available from the Google Maven repository, see https://maven.google.com/web/index.html#com.android.tools:r8.

Our CI builds for each commit and stores all build artifacts in Google Cloud Storage in the bucket r8-releases.

To obtain a prebuild from the CI for a specifc version (including both stable and -dev versions), download from the following URL:

https://storage.googleapis.com/r8-releases/raw/<version>/r8lib.jar

To obtain a prebuild from the CI for a specifc main branch hash, download from the following URL:

https://storage.googleapis.com/r8-releases/raw/main/<hash>/r8lib.jar

The prebuilt JARs have been processed by R8, and for each build the corresponding mapping file is located together with the prebuild under the name r8lib.jar.map.

To get prebuilds which has not been processed by R8 replace r8lib.jar with r8.jar in the URLs above.

The Google Cloud Storage bucket r8-releases can also be used as a simple Maven repository using the following in a Gradle build file:

maven {
    url = uri("https://storage.googleapis.com/r8-releases/raw")
}

See Running D8 and Running R8 below on how to invoke D8 and R8 using the obtained r8lib.jar in place of build/libs/r8.jar.

Downloading source and building

The R8 project uses depot_tools from the chromium project to manage dependencies. Install depot_tools and add it to your path before proceeding.

The R8 project uses Java 8 language features and requires a Java 8 compiler and runtime system.

Typical steps to download and build:

$ git clone https://r8.googlesource.com/r8
$ cd r8
$ tools/gradle.py r8

The tools/gradle.py script will bootstrap using depot_tools to download a version of gradle to use for building on the first run. This will produce a jar file: build/libs/r8.jar which contains both R8 and D8.

Running D8

The D8 dexer has a simple command-line interface with only a few options. D8 consumes class files and produce DEX.

The most important option is whether to build in debug or release mode. Debug is the default mode and includes debugging information in the resulting dex files. Debugging information contains information about local variables used when debugging dex code. This information is not useful when shipping final Android apps to users and therefore, final builds should use the --release flag to remove this debugging information to produce smaller dex files.

Typical invocations of D8 to produce dex file(s) in the out directoy:

Debug mode build:

$ java -cp build/libs/r8.jar com.android.tools.r8.D8 \
       --min-api <min-api> \
       --output out \
       --lib <android.jar/rt.jar> \
       input.jar

Release mode build:

$ java -cp build/libs/r8.jar com.android.tools.r8.D8
       --release \
       --min-api <min-api> \
       --output out \
       --lib <android.jar/rt.jar> \
       input.jar

See Running R8 for information on options --min-api and --lib.

The full set of D8 options can be obtained by running the command line tool with the --help option.

Running R8

R8 is a whole-program optimizing compiler with focus on shrinking the size of programs. R8 uses the ProGuard configuration format for configuring the whole-program optimization including specifying the entry points for an application.

R8 consumes class files and can output either DEX for Android apps or class files for Java apps.

Typical invocations of R8 to produce optimized DEX file(s) in the out directory:

$ java -cp build/libs/r8.jar com.android.tools.r8.R8 \
       --release \
       --min-api <min-api> \
       --output out \
       --pg-conf proguard.cfg \
       --lib <android.jar/rt.jar> \
       input.jar

This produce DEX targeting Android devices with a API level of <min-api> and above.

The option --lib is passing the bootclasspath for the targeted runtime. For targeting Android use an android.jar from the Android Platform SDK, typically located in ~/Android/Sdk/platforms/android-XX, where XX should be the latest Android version.

To produce class files pass the option --classfile and leave out --min-api <min-api>. This invocation will provide optimized Java classfiles in output.jar:

$ java -cp build/libs/r8.jar com.android.tools.r8.R8 \
       --release \
       --classfile \
       --output output.jar \
       --pg-conf proguard.cfg \
       --lib <android.jar/rt.jar> \
       input.jar

When producing output targeted for the JVM one can pass $JAVA_HOME to -lib. R8 will then locate the Java bootclasspath from there.

The full set of R8 options can be obtained by running the command line tool with the --help option.

R8 is not command line compatible with ProGuard, for instance keep rules cannot be passed on the command line, but have to be passed in a file using the --pg-conf option.

Replacing R8 in Android Gradle plugin

Android Gradle plugin (AGP) ships with R8 embedded (as part of the builder.jar from com.android.tools.build:builder:<agp version> on https://maven.google.com).

To override the embedded version with a prebuilt R8 with version <version>, merge the following into the top level settings.gradle or settings.gradle.kts:

pluginManagement {
    buildscript {
        repositories {
            mavenCentral()
            maven {
                url = uri("https://storage.googleapis.com/r8-releases/raw")
            }
        }
        dependencies {
            classpath("com.android.tools:r8:<version>")
        }
    }
}

To override the embedded version with a downloaded or freshly built <path>/r8.jar merge the following into the top level settings.gradle or settings.gradle.kts:

pluginManagement {
    buildscript {
        dependencies {
            classpath(files("<path>/r8.jar"))
        }
    }
}

Testing

Typical steps to run tests:

$ tools/test.py --no_internal

The tools/test.py script will use depot_tools to download a lot of tests and test dependencies on the first run. This includes prebuilt version of the art runtime on which to validate the produced dex code.

Contributing

In order to contribute to D8/R8 you have to sign the Contributor License Agreement. If your contribution is owned by your employer you need the Corporate Contributor License Agreement.

Once the license agreement is in place, please send an email to [email protected] to be added as a contributor.

After being added as a contributer you can upload your patches using git cl which is available in depot_tools. Once you have a change that you are happy with you should make sure that it passes all tests and then upload the change to our code review tool using:

$ git cl upload

On your first upload you will be asked to acquire credentials. Follow the instructions given by git cl upload.

On successful uploads a link to the code review is printed in the output of the upload command. In the code review tool you can assign reviewers and mark the change ready for review. At that point the code review tool will send emails to reviewers.

Getting help

For questions, reach out to us at [email protected].

For D8, find known issues in the D8 issue tracker or file a new D8 bug report.

For R8, find known issues in the R8 issue tracker or file a new R8 bug report.

r8's People

Contributors

agrieve avatar albertjin avatar benoitlamarche avatar caoman avatar christofferqa avatar col-e avatar colincross avatar denisvnukov avatar donaldchai avatar gavra0 avatar jakewharton avatar jsjeon avatar jvgogh avatar kageiit avatar kleerwater avatar madsager avatar mkj-gram avatar ricowind avatar sgjesse avatar shishkander avatar vadimsht avatar vapier avatar xxdark avatar zerny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

zjhuang0 icodein

r8's Issues

Allow DexApplication + ProgramClass/Field/Method to be cloned

For usage in dex-translator, clone operations would be very useful.

The IR converter mutating the class/field/method models prevents repeated actions. If we have the initial model, then operate on clones we can effectively handle repeated actions on the "same" input data.

Round-trip D --> J --> D ... is unstable with try-catch block handlers

Converting from Dex to Java to Dex back to Java fails on the 068-classloader sample.

StackTrace:

Caused by: com.android.tools.r8.errors.CompilationError: Different stack heights at jump target: 0 != 1
	at com.android.tools.r8.ir.conversion.CfState.merge(CfState.java:131)
	at com.android.tools.r8.ir.conversion.CfState.merge(CfState.java:126)
	at com.android.tools.r8.ir.conversion.CfSourceCode.recordStateForTarget(CfSourceCode.java:587)
	at com.android.tools.r8.ir.conversion.CfSourceCode.buildInstruction(CfSourceCode.java:567)
	at com.android.tools.r8.ir.conversion.CfSourceCode.buildInstruction(CfSourceCode.java:574)
	at com.android.tools.r8.ir.conversion.IRBuilder.processWorklist(IRBuilder.java:924)
	at com.android.tools.r8.ir.conversion.IRBuilder.build(IRBuilder.java:667)
	at com.android.tools.r8.graph.CfCode.internalBuild(CfCode.java:758)
	at com.android.tools.r8.graph.CfCode.internalBuildPossiblyWithLocals(CfCode.java:664)
	at com.android.tools.r8.graph.CfCode.buildIR(CfCode.java:613)
	at com.android.tools.r8.graph.ProgramMethod.buildIR(ProgramMethod.java:38)
	at com.android.tools.r8.graph.ProgramMethod.buildIR(ProgramMethod.java:32)
	at com.android.tools.r8.ir.conversion.IRConverter.rewriteDesugaredCodeInternal(IRConverter.java:494)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.rewriteNonDesugaredCodeInternal(PrimaryD8L8IRConverter.java:383)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.lambda$rewriteNonDesugaredCode$4(PrimaryD8L8IRConverter.java:368)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.rewriteNonDesugaredCode(PrimaryD8L8IRConverter.java:364)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.convertMethod(PrimaryD8L8IRConverter.java:165)
	at com.android.tools.r8.ir.conversion.D8MethodProcessor.processMethod(D8MethodProcessor.java:164)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.convertMethods(PrimaryD8L8IRConverter.java:130)
	at com.android.tools.r8.ir.conversion.ClassConverter.convertMethods(ClassConverter.java:227)
	at com.android.tools.r8.ir.conversion.ClassConverter$DefaultClassConverter.convertClass(ClassConverter.java:245)
	at com.android.tools.r8.ir.conversion.ClassConverter.lambda$internalConvertClasses$2(ClassConverter.java:162)
	at com.android.tools.r8.utils.ThreadUtils.lambda$processItems$5(ThreadUtils.java:106)
	at com.android.tools.r8.utils.ThreadUtils.processItems(ThreadUtils.java:120)
	at com.android.tools.r8.utils.ThreadUtils.processItems(ThreadUtils.java:106)
	at com.android.tools.r8.ir.conversion.ClassConverter.internalConvertClasses(ClassConverter.java:160)
	at com.android.tools.r8.ir.conversion.ClassConverter.convertClasses(ClassConverter.java:66)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.convertClasses(PrimaryD8L8IRConverter.java:295)
	at com.android.tools.r8.ir.conversion.PrimaryD8L8IRConverter.convert(PrimaryD8L8IRConverter.java:69)
	... 22 more

The output of multiple round-trip passes is expected to be lossy, but not outright fail.

Allow D8 converter to filter what classes are processed

This will allow downstream dex-translator to process classes individually. This is ideal since there are many situations where we may only want to process a sub-set of classes from a whole application.

Primarily, we care about PrimaryD8L8IRConverter. Supporting the other converters (R8/L8) would be nice but not strictly needed for our purposes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.