GCM注册适用于调试APK,但不能发布APK [英] GCM registration works for debug APK but not release APK

查看:269
本文介绍了GCM注册适用于调试APK,但不能发布APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的APK的调试版本始终成功注册到GCM。我刚刚构建了我的APK的第一个发行版本。我可以在我的USB连接设备上通过Android Studio安装并运行发行版。但是,GCM注册始终会在发布版本中失败。

The debug build of my APK always successfully registers with GCM. I've just built the first release version of my APK. I can install and run the release version, via Android Studio, on my USB-connected device. However GCM registration always fails for the release version.

private static final String TAG = "GcmRegIntentService";
MyRegistrationEpt backendRegService = getRegistrationService();

try {
    synchronized (TAG) {
        InstanceID instanceID = InstanceID.getInstance(appContext);
        String token = instanceID.getToken(GCM_SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        Log.i(TAG, "before register, token: " + token);

        backendRegService.register(token).execute();
    }
} catch (Exception ex) {
    Log.i(TAG, "Error: " + ex.getMessage());
}

private MyRegistrationEpt getRegistrationService() {

    MyRegistrationEpt.Builder builder = new MyRegistrationEpt.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
            .setRootUrl("https://some_valid_app.appspot.com/_ah/api/")
            .setApplicationName(getResources().getString(R.string.app_name));

    return builder.build();
}

当这段代码运行时,这是日志:

When this code runs this is the log:

08-21 12:52:54.225   4145-13398/technology.grandma.margriver I/GcmRegIntentService﹕ before register, token: <some_long_string>
08-21 12:52:55.430   4145-13398/technology.grandma.margriver I/GcmRegIntentService﹕ Error: 404 Not Found

我已经缩小了与构建中的proguard剥离所需的GCM类相关的问题。如果我在build.gradle中设置minifyEnabled false,问题就会消失。我正在使用Google Play服务API级别7.8.0。 https://developers.google.com/android/guides/setup 说:

I have narrowed down the problem to be related to proguard stripping required GCM classes from my build. If I set "minifyEnabled false" in build.gradle the problem disappears. I'm using Google Play services API level 7.8.0. Page https://developers.google.com/android/guides/setup says:


Play服务客户端库中包含ProGuard指令以保留所需的类.Gradle Android插件自动将ProGuard配置文件附加到AAR(Android ARchive)软件包并将该软件包附加到您的ProGuard配置中在项目创建过程中,Android Studio会自动创建ProGuard配置文件和build.gradle属性,以供ProGuard使用。

"ProGuard directives are included in the Play services client libraries to preserve the required classes. The Android Plugin for Gradle automatically appends ProGuard configuration files in an AAR (Android ARchive) package and appends that package to your ProGuard configuration. During project creation, Android Studio automatically creates the ProGuard configuration files and build.gradle properties for ProGuard use. To use ProGuard with Android Studio, you must enable the ProGuard setting in your build.gradle buildTypes. For more information, see the ProGuard guide."

我将其解释为意味着我不需要为您的build.gradle buildTypes提供更多信息,请参阅ProGuard指南。手动需要为GCM添加​​任何proguard规则。任何人都可以解释这个错误的原因,并建议我如何解决它?

I interpret this to mean that I do not need to manually need to add any proguard rules for GCM. Can anyone explain the cause of this error and suggest how I fix it?

推荐答案

我解决了这个问题。问题不是由proguard剥离GCM类造成的,而是由Google App Engine API客户端连接到的proguard剥离类型和注释造成的。为了解决这个问题,我将这些代码添加到了我的proguard规则中:

I fixed this problem. The problem was not caused by proguard stripping GCM classes but by proguard stripping types and annotations needed to connect to by Google App Engine API client. To fix this I added these lines to my proguard rules:

#  Needed by google-api-client to keep generic types and @Key annotations accessed via reflection

-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault

-keepclassmembers class * {
   @com.google.api.client.util.Key <fields>;
}

请参阅 https://developers.google.com/api-client-library/java/google-http-java-客户端/安装程序#proguard

这篇关于GCM注册适用于调试APK,但不能发布APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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