我想知道如何从 ProGuard 过渡/迁移到 R8。
我应该从我的 Gradle 文件中删除与 Proguard 相关的行并添加 android.enableR8 = true
行吗?
谢谢。
最佳答案
Proguard 由 GuardSquare 开发和维护,而 R8 由 Android 团队开发和维护,这意味着它们是两个不同的产品,尽管 R8 与 Proguard 兼容。
从这里看 https://www.guardsquare.com/en/blog/proguard-and-r8
Compatibility of ProGuard and R8
The good news for developers is that R8 is backward compatible with ProGuard. If you have a working ProGuard configuration (maybe eclectically copied from Stackoverflow), you can carry that over to R8. It currently still ignores some options. Notably, R8 doesn't implement the options -whyareyoukeeping and -addconfigurationdebugging, which we consider essential to quickly get to a working configuration, as we've explained in a previous blog.
是的,android.enableR8 = true
将启用 R8 功能。
另请注意,R8 目前(截至 Android Studio 3.2.1 时)不支持 Android 存档库 (AAR) 项目。仅在构建 APK 文件时使用。
编辑#1
来自 @Archie , 如果您使用的是 Gradle 插件版本 3.4.0 及更高版本,R8 默认为 on。
见:https://developer.android.com/studio/releases#r8-default
编辑#2
对于从 Proguard 到 R8 的转换,您可以按照以下步骤操作:
1.禁用 Proguard
更新buildTypes { }
禁用 Proguard 的配置,例如对于 release
构建类型:
android {
...
buildTypes {
release {
useProguard false // <-- disable proguard
minifyEnabled true // <-- enable minification
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
...
}
在 Android Studio 3.4 上,useProguard
默认为 false
. R8默认开启。
<强>2。 (可选)设置完整的 R8 配置报告文件
在您的 proguard-rules.pro
中添加以下行输出 R8 在构建项目时应用的所有规则的完整报告。
// You can specify any path and filename.
-printconfiguration <your-path>/full-r8-config.txt
3.生成混淆应用。
./gradlew assembleRelease
4. (可选)微调和故障排除
找到您的<your-path>/full-r8-config.txt
微调配置或进行故障排除(如果有)。
引用资料:
https://developer.android.com/studio/build/shrink-code.html#configuration-files
关于安卓/java : Transition/Migration from ProGuard to R8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818404/