Android GoogleCredential 调试与发布

Android GoogleCredential Debug vs Release

我正在尝试读取我的 GCS 存储桶,但发现了一个有趣的行为。当 运行 通过 Android Studio(作为调试变体)连接我的应用程序时,我能够下载对象。当我创建签名 APK(或 运行 作为发布变体)时,我无法再下载对象并收到 404 错误。

这是我的授权方式:

GoogleCredential googleCredential = null;
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();

/** Authorizes the installed application to access user's protected data. */
public GoogleCredential authorize() throws Exception {
    String p12Password = "****";
    String keyAlias = "****";
    KeyStore keystore = KeyStore.getInstance("PKCS12");
    keystore.load(getResources().openRawResource(R.raw.gcskeystore), p12Password.toCharArray());

    PrivateKey privateKey = (PrivateKey)keystore.getKey(keyAlias, p12Password.toCharArray());

    Set<String> scopes = new HashSet<String>();
    scopes.add(StorageScopes.DEVSTORAGE_READ_WRITE);

    String emailAddress = "********@developer.gserviceaccount.com";

    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(emailAddress)
            .setServiceAccountPrivateKey(privateKey)
            .setServiceAccountScopes(scopes)
            .build();

    return credential;
}

正在打印我的凭证,运行ning:

时看起来像这样
com.google.api.client.googleapis.auth.oauth2.GoogleCredential@42ed8060

当我收到错误时:

bpr@46096240

由于在创建签名版本时出现问题,我认为我的 proguard-rules 出问题了,但我添加的唯一行是:

-dontwarn sun.misc.*

希望有人以前见过这种行为!任何帮助将不胜感激!!

珍妮

我能够解决我的问题。我深入研究了 proguard-rules。进行此更新解决了我的问题:

# Needed to keep generic types and @Key annotations accessed via reflection

-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault

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

# Needed by Guava
# See https://groups.google.com/forum/#!topic/guava-discuss/YCZzeCiIVoI

-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue

# Needed by google-http-client-android when linking against an older platform version

-dontwarn com.google.api.client.extensions.android.**

# Needed by google-api-client-android when linking against an older platform version

-dontwarn com.google.api.client.googleapis.extensions.android.**