6

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,

Gerrit Beuze
  • 903
  • 2
  • 10
  • 26

3 Answers3

11

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

Evgeni Roitburg
  • 1,958
  • 21
  • 33
3

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.

Alex
  • 3,382
  • 2
  • 32
  • 41
  • Unfortunately this does not help. The Drive API calls still get mixed up and in the mapping.txt I still see renaming in the com.google class members – Gerrit Beuze Nov 02 '16 at 13:17
  • When I added this to the main module's proguard-rules.pro it still failed, however, when I alos added that to a module that the main module is depending on, (the module that actually uses the Drive API), it works!. Thanks a lot. – Gerrit Beuze Nov 02 '16 at 13:24
  • 1
    I was able to narrow it down to these keeps: -keep,allowshrinking class com.google.api.services.drive.model.** { *;} -keep,allowshrinking class com.google.api.services.drive.* { *;} – Gerrit Beuze Nov 02 '16 at 14:42
  • Worked like a charm. After 5 hours of trying to figure out what was going on, this post was a life saver. In my case, I was having issues uploading files to Google Drive once my signed APK was created. When in debug mode, the problem was not able to be reproduced. Somehow, the ProGuard processing was removing the affects of the setName on the MetaData. Net effect was that all files were uploaded with the name of the file being NULL. Thanks again for the post! – BluJ IT Aug 06 '19 at 01:52
  • hi Alex, your solution might work as well I have only down voted because Evgeni Roitburg fix exactly pinpoints the issue. As you have said its a blanket fix, the less we keep original source code safer it will be. Good work though, please keep up. If possible please improve your answer so developers know what that wide rule means. – MG Developer Jul 12 '20 at 19:06
0

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.

Cícero Moura
  • 2,027
  • 1
  • 24
  • 36