Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What really R8 full mode do (aggressive optimizations)?

The R8 official documentation says that to activate additional optimizations I must insert this in the gradle.properties file:

android.enableR8.fullMode=true

The documentation says that in order to make the App to work I must set some keep rules but there aren't details on how it works and what actions it performs:

Because the additional optimizations make R8 behave differently from ProGuard, they may require you to include additional ProGuard rules to avoid runtime issues. For example, say that your code references a class through the Java Reflection API. By default, R8 assumes that you intend to examine and manipulate objects of that class at runtime—even if you code actually does not—and it automatically keeps the class and its static initializer.

However, when using “full mode”, R8 does not make this assumption and, if R8 asserts that your code otherwise never uses the class at runtime, it removes the class from your app’s final DEX. That is, if you want to keep the class and its static initializer, you need to include a keep rule in your rules file to do that.

The link to the FAQs suggested by the documentation says only this:

R8 full mode

In full mode, R8 performs more aggressive optimizations, meaning that additional ProGuard configuration rules may be required. This section highlights some common issues that have been seen when using full mode.

How does android.enableR8.fullMode really work?

Thanks a lot!

like image 867
user2342558 Avatar asked Nov 06 '19 09:11

user2342558


People also ask

Is R8 better than ProGuard?

R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4. 0 or above then the project uses R8 by default with Proguard rules only.

What does Minifyenabled do?

minify is an Android tool that will decrease the size of your application when you go to build it. It's extremely useful as it means smaller apk files! It detects any code or libraries that aren't being used and ignores them from your final apk.

Is R8 enabled by default?

Android release. In the current Android Studio preview, R8 is enabled by default.

What is R8 Android Studio?

R8 is an app shrinking tool that is used to reduce the size of your application. This tool present in Android Studio works with the rules of Proguard. R8 will convert your app's code into optimized Dalvik code.

How to keep static initializers in R8 full mode?

However, when using “full mode”, R8 does not make this assumption and, if R8 asserts that your code otherwise never uses the class at runtime, it removes the class from your app’s final DEX. That is, if you want to keep the class and its static initializer, you need to include a keep rule in your rules file to do that.

What are some examples of code optimization in R8?

The following are a few examples of such optimizations: If your code never takes the else {} branch for a given if/else statement, R8 might remove the code for the else {} branch. If your code calls a method in only one place, R8 might remove the method and inline it at the single call site.

How do I enable additional optimization for R8 in ProGuard?

You can enable these additional optimizations by including the following in your project’s gradle.properties file: Because the additional optimizations make R8 behave differently from ProGuard, they may require you to include additional ProGuard rules to avoid runtime issues.

What are the optimization modes in the compiler?

Table 1. Optimization Modes (Compiler Settings Page) Optimization Mode Description Balanced (normal flow) The Compiler optimizes synthesis for balanced implementation that respects timing constraints. High Performance Effort


Video Answer


1 Answers

The difference between full mode and compatibility mode is described in the R8 FAQ.

Note, if the keep rules for the program are complete in the sense that everything which is used by reflection is covered by a keep rule, then turning on android.enableR8.fullMode should not cause issues. However, we often see configurations, where these (also not documented) conventions from Proguard are making the configuration work.

like image 139
sgjesse Avatar answered Oct 23 '22 00:10

sgjesse