10

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!

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • 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. – Marcos Vasconcelos Nov 06 '19 at 17:07
  • @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 R8 – user2342558 Nov 07 '19 at 08:22

1 Answers1

13

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.

sgjesse
  • 3,793
  • 14
  • 17
  • 1
    you say "we are working on" might be good to say which party you represent – Tim Nov 07 '19 at 15:10
  • 4
    I represent the R8 authors. – sgjesse Nov 08 '19 at 09:22
  • @sgjesse 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 you – Anton Kazakov Jun 28 '21 at 20:22
  • Please take a look at the R8 [compatibility FAQ](https://r8.googlesource.com/r8/+/refs/heads/main/compatibility-faq.md). It has a section on GSON, lncluding using GSON with full mode. Hope that can help. – sgjesse Jun 30 '21 at 06:37
  • the way you describe it sounded like "strict mode" to me – You Qi May 29 '23 at 08:35
  • Could you clarify what you mean with "strict mode" in this context? – sgjesse May 30 '23 at 09:37