New R8 Android Code Shrinker available in Android Studio 3.3 Beta

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

R8 will shrink, shrink, and convert an application to DEX format all at once. R8 is able to optimize Java bytecode by making it smaller and more efficient, as well as removing all unused classes, fields, methods and attributes. It will also collapse all program names to hide them and additionally make the executable even smaller. According to Google benchmarks, using R8 dramatically cuts processing time, to almost half the time it takes for the current Android shrink, Proguard. The APK (or DEX) size advantage is not as striking. However, R8 manages to reduce the final APK of the Santa Tracker application that Google used for its benchmark by a few hundred KB.

Google has yet to provide many technical details on how R8 compares to Proguard in terms of features. Proguard’s creator, GuardSquare, provided a comparison of the two products some time ago, based on analysis of R8 source code and their own test suite. In the GuardSquare comparison, R8 provided limited support for thinking; Limited support for obscuring class names in strings, resource files, and resource file names; and non-standard inner class renaming. It is not clear if R8 has evolved since the GuardSquare report to overcome these limitations.

R8 converts code to an intermediate representation and performs static unique assignment analysis (SSA), which is a standard approach to parsing method bodies. Proguard performs partial code-based evaluations, which deepens the dynamic behavior of code.

The main differences between R8 and Proguard seem to be that R8 is a single-pass optimizer, while Proguard can optimize code in multiple passes – 5 by default for Android versions. Indeed, it often happens that an optimization pass opens up new optimization opportunities in a subsequent pass. An example of this could be the optimization of logging statements, which, on a successive optimization pass, could result in the removal of variables that were only used in those logging statements.

R8 is always enabled and you can enable it by setting the following in your gradle.properties to file:

android.enableR8=true

An even more advanced option provided by R8 is “full mode”, which provides more optimizations, although at the expense of Proguard compatibility. You can activate full mode by setting:

android.enableR8.fullMode=true

Since R8 is still in beta, it is possible that your mileage will vary from Proguard. However, what seems most important to developers is the huge improvement in run times that R8 brings, at no cost in terms of file size.


Source link

Comments are closed.