R8 完整模式的真正作用是什么(积极优化)?

What really R8 full mode do (aggressive optimizations)?

R8 official documentation 说要激活额外的优化,我必须将其插入 gradle.properties 文件:

android.enableR8.fullMode=true

文档说,为了让应用程序正常工作,我必须设置一些保留规则,但没有详细说明它如何工作以及执行什么操作:

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.

文档建议的常见问题解答 link 仅说明了这一点:

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.

android.enableR8.fullMode 是如何工作的?

非常感谢!

完全模式和兼容模式的区别在R8 FAQ中有描述。

请注意,如果程序的保留规则是完整的,即反射使用的所有内容都包含在保留规则中,那么打开 android.enableR8.fullMode 应该不会引起问题。但是,我们经常看到配置,其中来自 Proguard 的这些(也未记录)约定使配置正常工作。