How to fix Proguard issue with Google Drive REST API

久未见 提交于 2019-12-01 12:03:39

问题


I'm using the Google Rest API v3 to connect my android app to Google Drive. When I run without Proguard (minifyEnabled=false), all is well. However, when I enable proguard the wrong REST API methods are called. When I call Drive.Files.get().execute on the drive root alias "root" I get the result for a Drive.Files.list().execute. When I disable "minifyEnabled" I see the correct result. Here is the section of the build.gradle that controls running Proguard:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

The default Proguard file is the unmodified one that gets distributes with Android Studio 2.2 (not the optimized version)

And this is the contents of my proguard-rules.pro file

-keepattributes EnclosingMethod
-keepattributes InnerClasses
-dontoptimize

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

When I check in the generated mapping.txt I still see renamed members in classes that imo shoudl have been "kept". For example:

com.google.api.client.auth.oauth.OAuthParameters -> com.google.api.client.auth.oauth.OAuthParameters: java.security.SecureRandom RANDOM -> l com.google.api.client.auth.oauth.OAuthSigner signer -> a java.lang.String callback -> b java.lang.String consumerKey -> c java.lang.String nonce -> d java.lang.String realm -> e

I would have thought "-keep class com.google.** " would have avoided this?

Any idea how to fix this?

THanks in advance,


回答1:


You need

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

and

-keep class com.fasterxml.** { *;}

Also you might try to keep less from the SDK. These rules are very wide.




回答2:


In my case I had to put these keeps:

-keep class br.project.pine.** { *;}
-keep class com.google.api.services.drive.** { *;}

Tip: When enabling minify in debug mode, pay attention to the LogCat. It can help you to find out the real missing package/class/attribute.




回答3:


This one worked for me:

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

As seen in the official google sample:

https://github.com/google/google-api-java-client-samples/blob/master/tasks-android-sample/proguard-google-api-client.txt



来源:https://stackoverflow.com/questions/40378671/how-to-fix-proguard-issue-with-google-drive-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!