自 Android Studio 3.5 更新以来,我在构建我的应用程序时收到此警告:
DSL 元素“useProguard”已过时,很快将被删除。在 gradle.properties 中使用 'android.enableR8' 在 R8 和 Proguard 之间切换。
自 Android Studio 3.5 更新以来,我在构建我的应用程序时收到此警告:
DSL 元素“useProguard”已过时,很快将被删除。在 gradle.properties 中使用 'android.enableR8' 在 R8 和 Proguard 之间切换。
从build.gradle 中删除“useProguard” :
release {
minifyEnabled true
//useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
解决了这个问题。
在项目的 gradle.properties 文件中设置以下内容
android.enableR8=true
R8 还具有与 Proguard 不直接兼容的完整模式。为了尝试一下,您可以在 gradle.properties 文件中另外设置以下内容
android.enableR8.fullMode=true
这会开启更多优化,从而进一步减小应用程序大小。但是,您可能需要一些额外的保留规则才能使其正常工作。
At a glance, when you build you project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler by default to handle the Shrink, obfuscate, and optimize your app. However, you can disable certain tasks or customize R8’s behavior through ProGuard rules files.
In fact, R8 works with all of your existing ProGuard rules files, so updating the Android Gradle plugin to use R8 should not require you to change your existing rules.
When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform. However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. You may enable them using the below code -
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
...
}
For the more adventurous, R8 also has full mode. In order to try that out you can additionally set the following in your gradle.properties
file.3
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work. Learn more here - https://youtu.be/uQ_yK8kRCaA
R8 is the default tool available in Android Studio 3.4 onwards. There is no need to explicitly enable R8. Just remove the useProguard true
line from the app/build.gradle
file.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}