其他分享
首页 > 其他分享> > Android Gradle插件2.2.0 ProGuard开始保留内部类

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

作者:互联网

在将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 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构建)进行了比较.我注意到的第一件事是新版本仍然具有一些内部类,而以前的版本中没有.

然后,我在我的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
            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规则太).

标签:proguard,android-proguard,android
来源: https://codeday.me/bug/20191118/2025889.html