GCM registration works for debug APK but not release APK

荒凉一梦 提交于 2019-12-24 05:36:14

问题


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

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:

"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."

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?


回答1:


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

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




回答2:


SHA1 Certificate might be needed to be added in the API console . For Both debug and release



来源:https://stackoverflow.com/questions/32132647/gcm-registration-works-for-debug-apk-but-not-release-apk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!