Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Proguard issue with Google Drive REST API

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,

like image 762
Gerrit Beuze Avatar asked Nov 02 '16 11:11

Gerrit Beuze


Video Answer


2 Answers

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

like image 146
Evgeni Roitburg Avatar answered Oct 13 '22 02:10

Evgeni Roitburg


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.

Edit: Wide rules means that it probably will keep more unused classes in your project hence the apk size and method count will be bigger.

like image 38
Alex Avatar answered Oct 13 '22 02:10

Alex