Android Gradle插件2.2.0 ProGuard开始保留内部类

Android Gradle插件2.2.0 ProGuard开始保留内部类,第1张

概述在将AndroidStudio更新为2.2版并将AndroidGradle插件更新为2.2.0之后,在构建发行版apk时会有很多警告,例如:Error:warning:IgnoringInnerClassesattributeforananonymousinnerclassError:(android.support.graphics.drawable.b)thatdoesn’tcomewithanErro

在将Android Studio更新为2.2版并将AndroID Gradle插件更新为2.2.0之后,在构建发行版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 @R_502_6852@ 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构建)进行了比较.我注意到的第一件事是新版本仍然具有一些内部类,而以前的版本中没有.

然后,我在我的proguard配置中添加了-keepattributes EnclosingMethod以避免警告,但是通过现在保留所有内部类,它大大增加了文件大小.

那么,是否可以像在AndroID Gradle插件2.1.3中那样,在没有不必要的内部类和-keepattributes EnclosingMethod的情况下构建apk?

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            proguard@R_502_6852@s getDefaultProguard@R_502_6852@('proguard-androID.txt'), 'proguard-rules.pro'        }    }    lintoptions {        abortOnError false    }}

解决方法:

是的,AndroID Gradle插件中包含的默认ProGuard规则在2.2-beta2中已更改.主要更改围绕-keepattributes和-renamesource@R_502_6852@attribute.

我们设法通过复制插件中包含的文件proguard-androID.txt和proguard-androID-optimize.txt修复了该问题,撤消了部分更改,使Gradle插件改为使用该文件:

proguard@R_502_6852@s 'proguard-androID-modifIEd.txt', 'proguard-rules.pro'

如果您将AndroID Gradle插件的版本降级到2.2-beta1并比较ProGuard文件,那么您会看到差异(是的,这不是唯一的更改…有一堆新的-keep和-keepclasseswithmembernames规则太).

总结

以上是内存溢出为你收集整理的Android Gradle插件2.2.0 ProGuard开始保留内部类全部内容,希望文章能够帮你解决Android Gradle插件2.2.0 ProGuard开始保留内部类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1120839.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存