Android Gradle Plugin 2.2.0 ProGuard 开始保持内部类
Android Gradle Plugin 2.2.0 ProGuard began to keep inner classes
将AndroidStudio更新到2.2版本,将AndroidGradle插件更新到2.2.0后,我build release apk的时候出现了很多警告,比如:
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(android.support.graphics.drawable.b) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is not an inner class.
此外,我的发布 apk 大小增加了。因此,我使用 dex2jar 工具将其转换为 jar,并与之前的版本(使用 com.android.tools.build:gradle:2.1.3
构建)进行了比较。我注意到的第一件事是新版本仍然有一些内部 类,这在以前的版本中是没有的。
然后我将 -keepattributes EnclosingMethod
添加到我的 proguard 配置中以避免警告,但它通过现在保留所有内部 类 增加了文件大小。
那么,我可以构建 apk 而没有不必要的内部 类 和 -keepattributes EnclosingMethod
,就像在 Android Gradle 插件 2.1.3 中那样吗?
ProGuard 配置:
# Retrolambda
-dontwarn java.lang.invoke.*
# okhttp
-dontwarn okio.**
-dontwarn okhttp3.**
-keep class okio.**
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
# appcompat
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }
-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
-keep public class * implements com.myapppackage.InterfaceClass
-keepattributes EnclosingMethod # was added to avoid 2.2.0 warnings
# Soft obfuscation
-keep public class !com.myapppackage.subpackage.** {
public protected *;
}
模块 build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
}
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
}
应用build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
是的,Android Gradle 插件中包含的默认 ProGuard 规则已在 2.2-beta2 中更改。主要变化是在 -keepattributes
和 -renamesourcefileattribute
左右。
我们设法通过复制插件中包含的文件 proguard-android.txt
和 proguard-android-optimize.txt
来修复它,撤消一些更改并让 Gradle 插件改用它:
proguardFiles 'proguard-android-modified.txt', 'proguard-rules.pro'
如果您将 Android Gradle 插件的版本降级到 2.2-beta1 并比较 ProGuard 文件,那么您会看到差异(是的,这些不是唯一的变化.. . 还有一堆新的 -keep
和 -keepclasseswithmembernames
规则。
将AndroidStudio更新到2.2版本,将AndroidGradle插件更新到2.2.0后,我build release apk的时候出现了很多警告,比如:
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(android.support.graphics.drawable.b) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is not an inner class.
此外,我的发布 apk 大小增加了。因此,我使用 dex2jar 工具将其转换为 jar,并与之前的版本(使用 com.android.tools.build:gradle:2.1.3
构建)进行了比较。我注意到的第一件事是新版本仍然有一些内部 类,这在以前的版本中是没有的。
然后我将 -keepattributes EnclosingMethod
添加到我的 proguard 配置中以避免警告,但它通过现在保留所有内部 类 增加了文件大小。
那么,我可以构建 apk 而没有不必要的内部 类 和 -keepattributes EnclosingMethod
,就像在 Android Gradle 插件 2.1.3 中那样吗?
ProGuard 配置:
# Retrolambda
-dontwarn java.lang.invoke.*
# okhttp
-dontwarn okio.**
-dontwarn okhttp3.**
-keep class okio.**
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
# appcompat
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }
-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
-keep public class * implements com.myapppackage.InterfaceClass
-keepattributes EnclosingMethod # was added to avoid 2.2.0 warnings
# Soft obfuscation
-keep public class !com.myapppackage.subpackage.** {
public protected *;
}
模块 build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
}
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
}
应用build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
是的,Android Gradle 插件中包含的默认 ProGuard 规则已在 2.2-beta2 中更改。主要变化是在 -keepattributes
和 -renamesourcefileattribute
左右。
我们设法通过复制插件中包含的文件 proguard-android.txt
和 proguard-android-optimize.txt
来修复它,撤消一些更改并让 Gradle 插件改用它:
proguardFiles 'proguard-android-modified.txt', 'proguard-rules.pro'
如果您将 Android Gradle 插件的版本降级到 2.2-beta1 并比较 ProGuard 文件,那么您会看到差异(是的,这些不是唯一的变化.. . 还有一堆新的 -keep
和 -keepclasseswithmembernames
规则。