【问题标题】:Android Studio proguard handling in multi-library projects多库项目中的 Android Studio proguard 处理
【发布时间】:2015-08-29 12:24:34
【问题描述】:

我有一个使用外部引用库的应用程序(即,库的目录与应用程序处于同一级别 - 它不会复制到应用程序的文件夹中)。该库由应用程序引用,并且该库和应用程序都包含 proguard 文件。在我构建应用程序之前一切正常。当我构建应用程序时,没有找到对库中定义的类的所有引用 - 我在库类的所有导入中都出现“找不到符号类 ...) 错误。正如我所发现的,这是因为在重建应用程序时,proguard 会混淆所有类和变量,因此应用程序无法引用它们。我已将以下内容添加到我的 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 有问题吗?

【问题讨论】:

    标签: android gradle proguard


    【解决方案1】:

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

    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 是包含库项目的 proguard 规则的文件。在构建应用程序时(在调试或发布模式下),编译器将处理所有规则(在库和应用程序中)。

    【讨论】:

    • 天哪,我有同样的情况,我花了整整一周的时间深入寻找解决方案。我感到很失落。到目前为止。太感谢了!谢谢!!
    • 有趣 - 我从 eclipse 导入了一个项目,它引入了所有库项目,但它完全无法编辑 build.gradle 文件以指定 consumerProguardFiles。
    • 添加这个 -- consumerProguardFiles 'proguard-project.txt' -- 在我的实验插件中让我得到一个“Gradle 同步失败:没有方法签名:org.gradle.model.ModelMap.consumerProguardFiles()适用于参数类型:(java.lang.String) 值:[proguard-project.txt]"。你知道它为什么抱怨吗?谢谢
    • 你应该得到奖牌
    • 根据文档应指定为:android { defaultConfig { consumerProguardFiles 'lib-proguard-rules.txt' } ... } developer.android.com/studio/projects/…
    【解决方案2】:

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

    (例如,看看我对 ButterKnife lib 的回答:link

    【讨论】:

    • 我有这个问题的原因是因为我为我的库定义了proguard规则。当我将 minify 设置为 false(在调试和发布模式下)时,应用程序编译正常。
    • 所以,您知道问题出在哪里。而且不清楚您是否定义了 proguard 规则,因为您没有显示该文件。
    • 我已经编辑了我的问题并添加了我正在使用的 proguard 规则。是的,我知道问题出在哪里,但是解决方案,即禁用 proguard 并不是一个好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多