Android GoogleCredential Debug vs Release

时间:2015-02-07 16:39:46

标签: java android android-studio google-oauth google-cloud-storage

我正在尝试阅读我的GCS存储桶,但我看到了一个有趣的行为。当通过Android Studio运行我的应用程序(作为Debug变体)时,我可以下载对象。当我创建签名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;
}

打印出我的凭证,运行时看起来像这样:

com.google.api.client.googleapis.auth.oauth2.GoogleCredential@42ed8060

这是我收到错误的时候:

bpr@46096240

由于在创建签名版本时出现问题,我认为我的proguard规则正在绊倒,但我添加的唯一一行是:

-dontwarn sun.misc.*

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

珍妮

1 个答案:

答案 0 :(得分:3)

我能够解决我的问题。我进一步深入研究了规则。进行此更新解决了我的问题:

# 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.**