使用 Android Studio 的 Google Cloud Endpoints 模板时的 Proguard 配置 [英] Proguard configuration when using Android Studio's Google Cloud Endpoints template

查看:19
本文介绍了使用 Android Studio 的 Google Cloud Endpoints 模板时的 Proguard 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We need to store and retrieve the content that users generate with our app online. To do so, we decided to use Android Studio's integrated Google Cloud Endpoints template to quickly create an API (official usage example here).

它在调试中工作正常,但在发布模式下,启用 Proguard 时,它会失败.更糟糕的是,我没有找到任何关于将 Proguard 与 Android Studio 的端点模板结合使用的文档或示例.

It works fine in debug, but in release mode, with Proguard enabled, it fails. Worse still, I've failed to find any documentation or samples about using Proguard with the Android Studio's Endpoints templates.

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

After an hour or so of poking around and trying to make it work, the proguard-rules.pro now looks like this:

-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 中收到类转换异常:

With this configuration, I'm getting a class cast exception in my ArrayAdapter:

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

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

It seems the conversion of returned data isn't performed somewhere and, instead of a List of Message objects, I get a List of com.google.api.client.util.ArrayMap objects (they do contain valid data, by the way).

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

I COULD check whether the app is running in release mode and do the conversion manually, however, it's a hacky way and I'd prefer to do it properly. So, can someone please tell me what I'm missing in the Proguard configuration file?

推荐答案

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

I do similar things with endpoints in one of my apps. I had some problems with Proguard as well (can't remember exactly what).

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

This section of my Proguard rules seems applicable:

# 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

我不知道是否有必要,但我也有这个部分:

I don't know if it is necessary, but I also have this section:

# 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 *;
}

希望有帮助.

这篇关于使用 Android Studio 的 Google Cloud Endpoints 模板时的 Proguard 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆