Android/java:从 ProGuard 过渡/迁移到 R8? [英] Android/java: Transition / Migration from ProGuard to R8?

查看:46
本文介绍了Android/java:从 ProGuard 过渡/迁移到 R8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从 ProGuard 过渡/迁移到 R8.

I wonder how to make the transition / migration from ProGuard to R8.

我是否应该从 Gradle 文件中删除与 Proguard 相关的行并添加 android.enableR8 = true 行?

Should I just remove the Proguard-related lines from my Gradle files and add the android.enableR8 = true line instead ?

谢谢.

推荐答案

Proguard 由 GuardSquare 开发和维护,而 R8 由 Android 团队开发和维护,这意味着它们是两种不同的产品,尽管 R8 与 Proguard 兼容.

Proguard is developed and maintained by GuardSquare while R8 is developed and maintained by Android team which means they are two different products although R8 is compatible with Proguard.

从这里可以看到 https://www.guardsquare.com/en/博客/proguard-and-r8

ProGuard 与 R8 的兼容性

对开发者来说好消息是R8 向后兼容 ProGuard.如果你有一个工作ProGuard 配置(可能是从 Stackoverflow 折衷复制的),你可以把它带到R8.它目前仍然忽略一些选项.值得注意的是,R8 没有实现选项 -whyareyoukeeping和 -addconfigurationdebugging,我们认为这对于快速进入工作配置,正如我们在之前的博客中所解释的那样.

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 功能.

Yes, android.enableR8 = true will enable the R8 feature.

另请注意,R8 目前(从 Android Studio 3.2.1 开始)不支持 Android Archive Library (AAR) 项目.仅在构建 APK 文件时使用.

Also note that, R8 does not currently (as the time of Android Studio 3.2.1) support Android Archive Library (AAR) projects. It is used only when building APK files.

编辑 #1

来自 @Archie,如果您使用的是 Gradle 插件版本 3.4.0 及更高版本,R8 默认开启.

From @Archie, If you are using Gradle plugin version 3.4.0 and above, R8 is on by default.

参见:https://developer.android.com/studio/releases#r8-默认

编辑#2

对于从 Proguard 到 R8 的过渡,您可以按照以下步骤操作:

For the transition from Proguard to R8, you can follow below steps:

1.禁用 Proguard

更新 buildTypes { } 配置以禁用 Proguard,例如对于 release 构建类型:

Update the buildTypes { } configuration to disable Proguard, e.g. for release build type:

   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.

On Android Studio 3.4, useProguard by default is false. And R8 is enabled by default.

2.(可选)设置完整的 R8 配置报告文件

2. (Optional) Set full R8 configurations report file

将以下行添加到您的 proguard-rules.pro 中,以输出 R8 在构建项目时应用的所有规则的完整报告.

Add below line into your proguard-rules.pro to output a full report of all the rules that R8 applies when building your project.

// You can specify any path and filename.
-printconfiguration <your-path>/full-r8-config.txt

3.生成混淆的应用.

./gradlew assembleRelease

4.(可选)微调和故障排除

4. (Optional) Fine-tune and trouble shooting

找到您的 /full-r8-config.txt 以微调配置或进行故障排除(如果有).

Find your <your-path>/full-r8-config.txt to fine-tune the configuration or do trouble shooting if any.

参考:

https://developer.android.com/studio/build/shrink-code.html#configuration-files

这篇关于Android/java:从 ProGuard 过渡/迁移到 R8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆