Android 多库项目中的 Studio proguard 处理

Android Studio proguard handling in multi-library projects

我有一个使用外部引用库的应用程序(也就是说,库的目录与应用程序处于同一级别 - 它没有复制到应用程序的文件夹中)。该库由应用程序引用,并且库和应用程序都包含 proguard 文件。在我构建应用程序之前一切正常。当我构建应用程序时,所有引用库中定义的 classes 都找不到 - 我得到“找不到符号 class ...)所有导入库 class 的错误es。正如我发现的那样,这是因为在重建应用程序时,proguard 混淆了所有 classes 和变量,因此应用程序无法引用它们。我已将以下内容添加到我的 build.gradle 文件中,

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
    debug {
        minifyEnabled false
    }
}

但似乎在构建应用程序时,没有考虑以上内容(或者构建是在发布模式下完成的)。如果我将上面的内容更改为以下内容(即在发布模式下禁用 proguard),

buildTypes {
    release {
        **minifyEnabled false**
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
    debug {
        minifyEnabled false
    }
}

应用程序编译正常。

有解决办法吗?我只能在创建签名应用程序时启用 proguard 吗?

这是库 proguard 文件:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-optimizations !method/marking/static

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-dontwarn **CompatHoneycomb
-keep class android.support.v4.** { *; }

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();
}

-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @com.google.android.gms.common.annotation.KeepName *;
}

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

-keep class com.google.android.gms.** { *; }

-keep public class com.google.ads.** { public *; }
-keep public class com.google.gson.** { public protected *; }
-keep public class com.google.ads.internal.** {*;} 
-keep public class com.google.ads.internal.AdWebView.** {*;} 
-keep public class com.google.ads.internal.state.AdState {*;} 
-keep public class com.google.ads.mediation.** { public *; }

-keep public class com.google.ads.searchads.** {*;} 
-keep public class com.google.ads.util.** {*;} 

-keep class com.google.ads.**
-dontwarn com.google.ads.**

-keepattributes *Annotation*

我在库和应用程序中都使用混淆器有问题吗?

我认为您需要为您的库定义 proguard-rules。通常它们在图书馆文档中。

(例如,在这里查看我对 ButterKnife 库的回答:

经过一番搜索,我找到了答案。如果您将 external/separate 源库与主 project/application 一起使用,则不应在库模块上使用混淆器。相反,您替换以下内容,

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
    debug {
        minifyEnabled false
    }
}

与以下内容(在 library/libraries 的 build.gradle 中):

buildTypes {
    release {
        consumerProguardFiles 'proguard-project.txt'
    }
}

其中 proguard-project.txt 是包含您的库项目的混淆规则的文件。在构建应用程序时(无论是调试模式还是发布模式),编译器都会处理所有规则(在库和应用程序中)。