在多库项目中处理Android Studio proguard

a.p*_*.p. 29 android proguard gradle

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

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

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

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

申请编译好.

这个问题有方法解决吗?我是否只能在创建签名应用程序时启用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*
Run Code Online (Sandbox Code Playgroud)

我在库和应用程序中使用proguard是一个问题吗?

a.p*_*.p. 53

经过一番搜索,我找到了答案.如果在主项目/应用程序中使用外部/单独的源库,则不应在库模块上使用proguard.相反,你替换以下,

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

使用以下内容(在库/库的build.gradle中):

buildTypes {
    release {
        consumerProguardFiles 'proguard-project.txt'
    }
}
Run Code Online (Sandbox Code Playgroud)

其中proguard-project.txt是包含库项目的proguard规则的文件.在构建应用程序时(在调试或发布模式下),编译器将处理所有规则(在库和应用程序中).

  • 天哪,我有同样的情况,我整整一周都在寻找解决方案.我感到很失落.到现在.非常感谢!谢谢!! (2认同)
  • 根据doc,它应该被指定为:`android {defaultConfig {consumerProguardFiles'lib-proguard-rules.txt'} ...}`https://developer.android.com/studio/projects/android-library.html#注意事项 (2认同)