Kotlin-style lambda class merging, phase 1.

The first part of class merging for Kotlin k-style lambdas, includes
basic working functionality and leaves out a few improvements which
will be worked on separately.

Since Kotlin lambda classes are generated based on a common template
many of them have same structure and differ only by code of main
lambda method. In this CL we combine lambda with same structure
classes into groups, assign each lambda in a group a unique id, and
then use same lambda group class to represent all lambdas inside the
group dispatching the calls by lambda id when necessary.

Grouping criteria:
  - lambda implement same functional interface
  - lambda has the same "capture signature"
  - lambda has same "main method"
  - lambda are located inside same package (unless access is relaxed)
  - lambda have same generic signature (if present)
  - lambda have same InnerClasses/EnclosingMethod attribute (if present)

Since the implementation is based on several assumptions about lambda
class struture, we exclude lambdas failing these assumptions from processing.
We also exclude lambda classes having unexpected references/usages.

Lambda groups consisting from single lambdas are not processed, such
lambda classes are kept as-is.

Note that the efficiency of merging depends on options. For example if
EnclosingMethod is kept, we only merge lambdas inside one method. OTOH,
if access relaxation is enabled and signature/inner/enclosing attributes
are removed, we end up with as many groups as <functional-interface X
captures X main method>. (And main methods are actually expected to
always be inlined and removed, we’ll address this in follow up CL.)

Current implementation is only enabled if tree shaking is enabled since
we rely on it to remove unused lambda classes. This may be revised later.

Bug:72860066

Change-Id: Ib5dd1af6ca13c74fd513714ed5a8d1f3c7fbea0b
61 files changed
tree: 9877e7ddae1f8526b4bd0d7163e27b301904e3b3
  1. buildSrc/
  2. library-licensing/
  3. scripts/
  4. src/
  5. tests/
  6. third_party/
  7. tools/
  8. .gitignore
  9. AUTHORS
  10. build.gradle
  11. codereview.settings
  12. CONTRIBUTING.md
  13. copyAdditionalJctfCommonFiles.gradle
  14. LIBRARY-LICENSE
  15. LICENSE
  16. README.md
README.md

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 a replacement for the Proguard shrinking and minification tool.

Downloading 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 d8 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 two jar files: build/libs/d8.jar and build/libs/r8.jar.

Running D8

The D8 dexer has a simple command-line interface with only a few options.

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 -jar build/libs/d8.jar --output out input.jar

Release mode build:

$ java -jar build/libs/d8.jar --release --output out input.jar

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

Running R8

R8 is a Proguard replacement for whole-program optimization, shrinking and minification. R8 uses the Proguard keep rule format for specifying the entry points for an application.

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

$ java -jar build/libs/r8.jar --release --output out --pg-conf proguard.cfg input.jar

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

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, 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 r8-dev@googlegroups.com.

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.