如何使用Google Drive SDK为Android应用程序配置ProGuard?

3
当Eclipse在构建Quickstart: Run a Drive App on Android示例应用程序时包含proguard,该应用程序将失去上传文件的功能。
鉴于发布版本的应用程序在没有proguard的情况下构建时可以正常工作,因此Google API客户端ID似乎已经正确配置。然而,当使用启用了proguard的Eclipse构建相同的发布版本应用程序时,文件上传不起作用。这是我的有问题的proguard配置文件:
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

-libraryjars /libs/android-support-v4.jar
-libraryjars /libs/google-api-client-1.13.2-beta.jar
-libraryjars /libs/google-api-client-android-1.13.2-beta.jar
-libraryjars /libs/google-api-services-drive-v2-rev70-1.13.2-beta.jar
-libraryjars /libs/google-http-client-1.13.1-beta.jar
-libraryjars /libs/google-http-client-android-1.13.1-beta.jar
-libraryjars /libs/google-http-client-gson-1.13.1-beta.jar
-libraryjars /libs/google-http-client-jackson-1.13.1-beta.jar
-libraryjars /libs/google-http-client-jackson2-1.13.1-beta.jar
-libraryjars /libs/google-http-client-protobuf-1.13.1-beta.jar
-libraryjars /libs/google-oauth-client-1.13.1-beta.jar
-libraryjars /libs/gson-2.1.jar
-libraryjars /libs/guava-jdk5-13.0.jar
-libraryjars /libs/jackson-core-2.0.5.jar
-libraryjars /libs/jackson-core-asl-1.9.9.jar
-libraryjars /libs/jsr305-1.3.9.jar
-libraryjars /libs/protobuf-java-2.4.1.jar

# Add any project specific keep options here:

-keep class com.google.**
-dontwarn com.google.**

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

这里是在logcat中显示的结果错误:

D/ALSAModule(  162): setHardwareParams: reqBuffSize 1024 channels 2 sampleRate 48000
D/ALSAModule(  162): setHardwareParams: buffer_size 2048, period_size 1024, period_cnt 2
W/System.err(10707): com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
W/System.err(10707): {
W/System.err(10707):   "code": 403,
W/System.err(10707):   "errors": [
W/System.err(10707):     {
W/System.err(10707):       "domain": "usageLimits",
W/System.err(10707):       "message": "Access Not Configured",
W/System.err(10707):       "reason": "accessNotConfigured"
W/System.err(10707):     }
W/System.err(10707):   ],
W/System.err(10707):   "message": "Access Not Configured"
W/System.err(10707): }
W/System.err(10707):    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.b(Unknown Source)
W/System.err(10707):    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.a(Unknown Source)
W/System.err(10707):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.b(Unknown Source)
W/System.err(10707):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.d(Unknown Source)
W/System.err(10707):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.e(Unknown Source)
W/System.err(10707):    at com.example.testupload2.a.run(Unknown Source)
W/System.err(10707):    at java.lang.Thread.run(Thread.java:856)
D/overlay (  159): Unset pipe=VG0 dpy=0; Unset pipe=RGB1 dpy=0;
D/alsa_ucm(  162): snd_use_case_set(): uc_mgr 0x40e59388 identifier _verb value Inactive
D/alsa_ucm(  162): Set mixer controls for HiFi Lowlatency enable 0
D/alsa_ucm(  162): snd_use_case_set(): uc_mgr 0x40e59388 identifier _disdev value Camcorder Tx

我是一个Proguard新手。有没有关于正确的Proguard配置的任何提示?
更新: 我将此问题提交给了Android问题跟踪器(问题54765)。
更新2: 现在,Google Drive Android API已在Google Play服务中实现,我的应用程序大小已减小,并且Proguard的设置已经得到彻底记录。这两个都是好事情。
3个回答

11

在你的proguard-google-api-client.txt中使用以下代码以保留所需的属性和类成员。

-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault

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

-keep class com.google.** { *;}
-keep interface com.google.** { *;}
-dontwarn com.google.**

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

感谢您的回复。虽然您建议的配置规则已经在生成的默认proguard-google-api-client.txt中,但是您的回复促使我在proguard-google-api-client.txt上进行更多搜索,这让我找到了https://dev59.com/nuo6XIcBkEYKwwoYORwV,该网站建议添加-keep class com.google.** { ;} -keep interface com.google.* { *;} -dontwarn com.google.**。这对我很有用。所以请将其添加到您的答案中,我会将其标记为已接受。再次感谢。 - gregS
它不会缩小那么多库。我将与Java客户端库团队核实是否存在已知问题。 - Burcu Dogan
是的,使用这两个额外的-keep配置规则,.apk的大小从0.186兆字节增加到1.009兆字节。不幸的是,其中一个并没有起作用。 - gregS
稍微详细一点...这个小的.apk以致命异常结束(“java.lang.IllegalArgumentException: unable to create new instance of class com.google.a.b.a.a.e because it has no accessible default constructor”),但在异常之前,该应用程序成功地将示例图像上传到Google Drive。 - gregS

1
如果您正在使用Google Drive API v3,您只需要这些:
-keep class * extends com.google.api.client.json.GenericJson {*;}
-keep class com.google.api.services.drive.** {*;}
-keep class com.google.api.client.** {*;}

1

只需在proguard-project.txt文件中添加以下两行代码

-keep class com.google.android.gms.** { ; }
-dontwarn com.google.android.gms.*

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接