Google Drive Api v3 和 proguard

问题描述 投票:0回答:4

使用 proguard files().list() 缩小我的发布 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() 返回有效的 JSON 结果,但 files 为空。

这是我的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'
        }

}
android google-drive-android-api
4个回答
26
投票

仅添加

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

proguard.cfg 有效!


5
投票

对我有用的是:

-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault
-keep class * extends com.google.api.client.json.GenericJson { *; }
-keep class com.google.api.services.drive.** { *; }
-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}

这个问题以及这个proguard规则文件

很有帮助

1
投票

检查此相关问题

导出签名的 APK 时 Proguard 是否有机会启动?如果您依赖变量名称将 JSON 映射到 POJO,那么如果没有适当的 Proguard 排除/规则,这可能会失败。查看您的

project.properties
文件并以
proguard.config=<file_name>
的形式注释掉任何行。之后,导出另一个签名的 APK 并重新测试。

确保将

minifyEnabled true
添加到
build.gradle
文件中适当的构建类型,以便按照本文档中的说明启用 ProGuard 代码收缩。


0
投票

测试示例:https://github.com/googleworkspace/android-samples/tree/master/drive/deprecation

-keep class * extends com.google.api.client.json.GenericJson { *; }
-keep class com.google.api.services.drive.** { *; }
-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}

规则适用于最新的驱动器 API 取决于:

    implementation("com.google.android.gms:play-services-auth:21.0.0")
    implementation("com.google.http-client:google-http-client-gson:1.44.1")
    implementation("com.google.api-client:google-api-client-android:2.3.0") {
        exclude("org.apache.httpcomponents")
    }
    implementation("com.google.apis:google-api-services-drive:v3-rev20240123-2.0.0") {
        exclude("org.apache.httpcomponents")
    }
© www.soinside.com 2019 - 2024. All rights reserved.