本文介绍了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

对开发人员而言,好消息是R8向后兼容ProGuard.如果你有工作ProGuard配置(可以从Stackoverflow折衷地复制),您可以将其带到R8.目前它仍然忽略了一些选项.值得注意的是,R8没有实现选项-whyareyoukeeping和-addconfigurationdebugging,我们认为这对于快速调试至关重要就像我们在上一篇博客中所解释的那样,进入有效的配置.

The good news for developers isthat R8 is backward compatible with ProGuard. If you have a workingProGuard configuration (maybe eclectically copied from Stackoverflow),you can carry that over to R8. It currently still ignores someoptions. Notably, R8 doesn't implement the options -whyareyoukeepingand -addconfigurationdebugging, which we consider essential to quicklyget 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存档库(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 处于 on .

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上,默认情况下,useProguardfalse.并且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

找到您的<your-path>/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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 14:52