简体   繁体   English

GCM注册可用于调试APK,但不能发布APK

[英]GCM registration works for debug APK but not release APK

The debug build of my APK always successfully registers with GCM. 我的APK的调试版本始终会成功向GCM注册。 I've just built the first release version of my APK. 我刚刚构建了APK的第一个发行版。 I can install and run the release version, via Android Studio, on my USB-connected device. 我可以在连接USB的设备上通过Android Studio安装和运行发行版。 However GCM registration always fails for the release version. 但是,对于发行版,GCM注册始终失败。

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

I have narrowed down the problem to be related to proguard stripping required GCM classes from my build. 我已经缩小了与在我的构建中剥离Proguard所需的GCM类有关的问题。 If I set "minifyEnabled false" in build.gradle the problem disappears. 如果我在build.gradle中将“ minifyEnabled false”设置为零,问题将消失。 I'm using Google Play services API level 7.8.0. 我正在使用Google Play服务API级别7.8.0。 Page https://developers.google.com/android/guides/setup says: 页面https://developers.google.com/android/guides/setup说:

"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." “ Play服务客户端库中包含ProGuard指令,以保留所需的类。Android Gradle插件会自动将ProGuard配置文件添加到AAR(Android ARchive)包中,然后将该包添加到您的ProGuard配置中。在项目创建期间,Android Studio自动创建ProGuard配置文件和build.gradle属性以供ProGuard使用。要将ProGuard与Android Studio一起使用,必须在build.gradle buildTypes中启用ProGuard设置。有关更多信息,请参阅ProGuard指南。

I interpret this to mean that I do not need to manually need to add any proguard rules for GCM. 我的解释是,我不需要手动为GCM添加​​任何proguard规则。 Can anyone explain the cause of this error and suggest how I fix it? 谁能解释这个错误的原因并建议我如何解决?

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. 该问题不是由Proguard剥离GCM类引起的,而是由Google App Engine API客户端连接至所需的Proguard剥离类型和注释引起的。 To fix this I added these lines to my proguard rules: 为了解决这个问题,我在Proguard规则中添加了以下几行:

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

See https://developers.google.com/api-client-library/java/google-http-java-client/setup#proguard . 请参阅https://developers.google.com/api-client-library/java/google-http-java-client/setup#proguard

SHA1 Certificate might be needed to be added in the API console . 在API控制台中可能需要添加SHA1证书。 For Both debug and release 对于调试和发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM