【问题标题】:Proguard configuration when using Android Studio's Google Cloud Endpoints template使用 Android Studio 的 Google Cloud Endpoints 模板时的 Proguard 配置
【发布时间】:2015-01-31 17:46:47
【问题描述】:

我们需要在线存储和检索用户使用我们的应用生成的内容。为此,我们决定使用 Android Studio 集成的 Google Cloud Endpoints 模板快速创建 API(官方使用示例 here)。

它在调试中工作正常,但在发布模式下,启用 Proguard 时,它会失败。更糟糕的是,我找不到任何关于将 Proguard 与 Android Studio 的 Endpoints 模板一起使用的文档或示例。

经过一个小时左右的探索并尝试使其工作,proguard-rules.pro 现在看起来像这样:

-keep class com.google.api.** { public *; }
-dontwarn com.google.api.**
-keep class com.google.common.** { public *; }
-dontwarn com.google.common.**

# Not allowed to post company and app names, but this line is correct in the real file
-keep class com.companyname.appname.application.backend.** { *; }

使用此配置,我的ArrayAdapter 中出现类转换异常:

java.lang.ClassCastException: com.google.api.client.util.ArrayMap cannot be cast to com.companyname.appname.application.backend.messageApi.model.Message

似乎没有在某处执行返回数据的转换,而不是Message 对象的List,而是com.google.api.client.util.ArrayMap 对象的List(顺便说一下,它们确实包含有效数据)。

我可以检查应用程序是否在发布模式下运行并手动进行转换,但是,这是一种老套的方式,我更愿意正确地进行。那么,有人可以告诉我 Proguard 配置文件中缺少什么吗?

【问题讨论】:

    标签: android google-app-engine android-studio proguard google-cloud-endpoints


    【解决方案1】:

    我在我的一个应用程序中使用端点做类似的事情。我在使用 Proguard 时也遇到了一些问题(不记得具体是什么)。

    我的 Proguard 规则的这一部分似乎适用:

    # Needed by google-api-client to keep generic types and @Key annotations accessed via reflection
    -keepclassmembers class * {
      @com.google.api.client.util.Key <fields>;
    }
    -keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
    

    不知道有没有必要,不过我也有这个板块:

    # Play Services
    -dontwarn com.google.android.gms.**
    -dontwarn com.google.common.cache.**
    -dontwarn com.google.common.primitives.**
    -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 *;
    }
    

    希望对你有帮助。

    【讨论】:

    • 您发布的第一部分确实是正确的解决方案。
    • 我可以确认第一部分解决了问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多