Google 驱动 Api v3 和 proguard

Google Drive Api v3 and proguard

在使用 proguard files().list() returns 压缩我的发布 apk 后,没有文件(在调试模式下它确实如此)。我使用快速入门示例进行登录。没有错误消息。好像登录成功了。

这是我的 proguard.cfg:

-optimizationpasses 1
#-dontoptimize
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*,!class/unboxing/enum
-dontwarn android.support.v4.**
-dontwarn com.google.**
#-keep public class com.google.**
#-keep public class android.**
#-keep class com.google.android.gms.** { *; }
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault

#-keep public class * extends android.app.Activity
#-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService


#-keep public class pub.devrel.easypermissions.**


-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}


-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

这是检索文件的代码:

private List<String> getDataFromApi() throws Exception {
            // Get a list of up to 10 files.
            lib.setgstatus("getDataFromApi Start");
            List<String> fileInfo = new ArrayList<String>();
            FileList result = mService.files().list()
                    .setPageSize(10)
                    .setFields("nextPageToken, files(id, name)")
                    .execute();
            lib.setgstatus(result.toString());
            List<File> files = result.getFiles();

            if (files != null) {
                lib.setgstatus("getDataFromApi files.size:" + files.size());
                for (File file : files) {
                    fileInfo.add(String.format("%s (%s)\n",
                            file.getName(), file.getId()));
                }
            }
            else
            {
                lib.setgstatus("getDataFromApi files is null");
            }

            lib.setgstatus("getDataFromApi Finish");
            return fileInfo;
        }

result.toString() Returns 有效 JSON 结果,但文件为空。

这是我的 build.gradle:

apply plugin: 'com.android.application'

    android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"

    defaultConfig {
        applicationId "org.de.jmg.jmgphotouploader"
        minSdkVersion 11
        targetSdkVersion 24
    }
    signingConfigs {
        release {
            keyAlias 'jmgphotouploader'
            keyPassword '****'
            storeFile file('/pub/keystore')
            storePassword '****'
        }
        debug {
            keyAlias 'jmgphotouploader'
            keyPassword '****'
            storeFile file('/pub/keystore')
            storePassword '****'
        }
    }

    buildTypes {
        debug {
            debuggable true
            minifyEnabled false
            //shrinkResources true
            //proguardFile 'proguard.cfg'
        }
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFile 'proguard.cfg'
            //proguardFile getDefaultProguardFile('proguard-android.txt')
            //proguardFiles getDefaultProguardFile('proguard-android.txt') ,'proguard.cfg'
        }
    }
}
dependencies {
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile project(':src')
    compile project(':utilities')
    compile 'com.google.android.gms:play-services-auth:9.6.1'
    compile 'pub.devrel:easypermissions:0.1.5'
    compile('com.google.api-client:google-api-client-android:1.22.0') {
        exclude group: 'org.apache.httpcomponents'
        }
    compile('com.google.apis:google-api-services-drive:v3-rev47-1.22.0') {
        exclude group: 'org.apache.httpcomponents'
        }

}

检查此相关 issue

Any chance Proguard kicks in when exporting your signed APK? If you rely on i.e. variable names to map the JSON onto POJOs, this is likely to brake without the appropriate Proguard exclusions/rules. Have a look in your project.properties file and comment out any lines in the form of proguard.config=<file_name>. After that, export another signed APK and retest.

确保将 minifyEnabled true 添加到 build.gradle 文件中的适当构建类型,以启用 ProGuard 压缩代码,如本 documentation 所述。

只添加

-keep class * extends com.google.api.client.json.GenericJson {
*;
}
-keep class com.google.api.services.drive.** {
*;
}

至 proguard.cfg 有效!