BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News New R8 Android Code Shrinker Available in Android Studio 3.3 Beta

New R8 Android Code Shrinker Available in Android Studio 3.3 Beta

Bookmarks

Following the migration to D8 as its new Dalvik compiler, Google has been working on a new code shrinker, R8, which is now available in Android Studio 3.3 beta. R8 promises to deliver smaller Android applicatin package (APK) files in less time.

R8 will shrink, desugar, and convert an application to the DEX format in just one go. R8 is able to optimize Java bytecode by making it smaller and more efficient as well as to remove all unused classes, fields, methods, and attributes. It will also minify all program names to obfuscate them, and additionally make the executable even smaller. According to Google benchmarks, using R8 reduces processing time dramatically, to almost the half the time it takes for the current Android shrinker, Proguard. The advantage in terms of APK (or DEX) size is not as striking. Still, R8 succeeds in shaving a few hundreds KB off the Santa Tracker app final APK that Google used for its benchmark.

Google has not yet provided a lot of technical details regarding how R8 stacks up against Proguard in terms of features. Proguard's creator, GuardSquare, provided a comparison of the two products some time ago, based on the analysis of R8 source code and their own test suite. In the GuardSquare comparison, R8 provided limited support for reflection; limited support for class name obfuscation in strings, resource files, and in resource file names; and non-standard inner class renaming. It is not clear whether R8 has evolved since the GuardSquare report to overcome those limitations.

R8 converts code to an intermediate representation and performs a static single assignment (SSA) analysis, which is a standard approach to analyze method bodies. Proguard performs partial evaluations across the code base, which looks farther into the dynamic behavior of the code.

The major differences between R8 and Proguard seem to be related to R8 being a one-pass optimizer while Proguard can optimize code in more passes -- 5 by default for Android builds. Indeed, often it is the case that an optimization pass opens up new optimization opportunities in a subsequent pass. An example of that could be optimization of logging statements, which, on a successive optimization pass, could lead to removal of variables that were used only in those logging statements.

R8 is still opt-in and you can enable it by setting the following in your gradle.properties file:

android.enableR8=true

A yet more advanced option provided by R8 is "full mode", which brings more optimizations, albeit at the expense of Proguard compatibility. You can enable full mode by setting:

android.enableR8.fullMode=true

Since R8 is still in beta stage, it is possible that your mileage may vary in comparison with Proguard. Although, what seems to matter most for developers is the huge improvement in running times brought by R8, at seemingly no cost in terms of file size.

Rate this Article

Adoption
Style

BT