简体   繁体   中英

GCM registration works for debug APK but not release APK

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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