What does R8 Full Mode really do? (aggressive optimizations)
Asked Answered
S

1

17

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!

Supersensual answered 6/11, 2019 at 9:13 Comment(2)
Not sure about really works but the example you posted is clear that code without reach by runtime (explicit code) will be erased, so if you do Reflection API you must add keep class and keep names for the classes only acessibles by other means. The best test would be activate it and test your app.Comedienne
@MarcosVasconcelos that is an example of what it do but I think it's strange that there are too few information about how it works and what it do. Also, I can test my App after activating it, but I already tested my App deeply, if I know hw R8 works before test my App I can save lots of time, instead of test all my whole app in search of "possible" bug due to R8Supersensual
I
16

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.

Indeed answered 7/11, 2019 at 8:27 Comment(8)
you say "we are working on" might be good to say which party you representTintype
I represent the R8 authors.Indeed
@Indeed Hello. R8 full mode breaks using gson for me, it shrink default constructors and gson can't create instance without expicit InstanceCreator for every type. What's the best solution here? Thank youLifeboat
Please take a look at the R8 compatibility FAQ. It has a section on GSON, lncluding using GSON with full mode. Hope that can help.Indeed
the way you describe it sounded like "strict mode" to meUtilitarianism
Could you clarify what you mean with "strict mode" in this context?Indeed
The documentation states that "The default constructor (<init>()) is not implicitly kept when a class is kept.". Does this mean that the init{} clauses are removed? That would make no sense.Buzzell
It is only the implicit keeping of the no-args constructor which it not there in full mode. With a keep rule like -keep class A { <init>(); } the no-args constructor of A will be kept in full mode. With a keep rule like this -keep class A the no-args constructor of A is not kept in full mode.Indeed

© 2022 - 2024 — McMap. All rights reserved.