Android GoogleCredential 调试与发布 [英] Android GoogleCredential Debug vs Release
问题描述
我正在尝试读取我的 GCS 存储桶,但发现了一个有趣的行为.通过 Android Studio(作为 Debug 变体)运行我的应用程序时,我能够下载对象.当我创建签名 APK(或作为发布变体运行)时,我无法再下载对象并收到 404 错误.
I am trying to read my GCS bucket but am seeing an interesting behavior. When running my app through Android Studio (as Debug variant) I am able to download objects. When I create a Signed APK (or run as Release variant) I am no longer able to download object and receive a 404 error.
这是我的授权方式:
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;
}
打印出我的凭证,运行时是这样的:
Printing out my credential, it looks like this when running:
com.google.api.client.googleapis.auth.oauth2.GoogleCredential@42ed8060
当我收到错误时:
bpr@46096240
由于在创建签名版本时出现问题,我认为我的 proguard 规则正在绊倒某些东西,但我添加的唯一一行是:
Since the issue occurs when creating a signed release I thought my proguard-rules are tripping something up but the only line I have added is:
-dontwarn sun.misc.*
希望有人以前见过这种行为!任何帮助将不胜感激!
Hopefully someone has seen this type of behavior before! Any help would be greatly appreciated!!
珍妮
推荐答案
我能够解决我的问题.我更深入地研究了 proguard 规则.进行此更新解决了我的问题:
I was able to fix my issue. I dug into the proguard-rules a little bit more. Making this update resolved my problem:
# 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.**
这篇关于Android GoogleCredential 调试与发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!