如何使R8 + proguard-android-optimize.txt + Google Drive API无缝运行?

时间:2019-07-15 12:55:34

标签: android google-drive-api proguard r8

这是我们的R8版本

当前版本是:1.4.94(来自go / r8bot(luci-r8-ci-archive-0-5g74)的内部版本390954928f0db9c3b888a367f7f128ce3bbfb160)。

当我从

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

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

我们收到以下错误。

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
    {
      "code": 403,
      "errors": [
        {
          "domain": "usageLimits",
          "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
          "reason": "dailyLimitExceededUnreg",
          "extendedHelp": "https://code.google.com/apis/console"
        }
      ],
      "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
    }
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:378)
        at com.google.api.client.http.HttpRequest.a(HttpRequest.java:1067)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:476)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:409)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:526)
        at com.yocto.wenote.sync.Utils.searchFromGoogleDrive(Utils.java:808)

崩溃发生在以下代码

try {
    Drive.Files.List request = drive.files().list().setSpaces(APP_DATA_FOLDER)
            .setQ("name = '" + Utils.getWeNoteZipFileTitle() + "'")
            .setFields("nextPageToken, files(id, name, modifiedTime)")
            .setOrderBy("modifiedTime desc")
            .setPageSize(10);

    do {
        FileList fileList = request.execute();  // <-- This is line Utils.java:808

请注意,在我们从proguard-android.txt切换到proguard-android-optimize.txt之前,上述代码可以100%正常工作。

R8似乎基于proguard-android-optimize.txt剥离了一些重要信息,这导致无法调用Google Drive API。

任何人如何使proguard-android-optimize.txt与Google Drive API兼容?


如何用Proguard替换R8?

如果我通过应用android.enableR8=false使用了ProGuard 6.0.3版(仍然使用proguard-android-optimize.txt),则在运行与Google Drive API相关的代码时没有问题。


我的Google Drive API库

// For Google Drive REST API - https://github.com/gsuitedevs/android-samples/blob/master/drive/deprecation/app/build.gradle
implementation('com.google.http-client:google-http-client-gson:1.26.0') {
    exclude group: 'org.apache.httpcomponents'
}
implementation('com.google.api-client:google-api-client-android:1.26.0') {
    exclude group: 'org.apache.httpcomponents'
}
implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0') {
    exclude group: 'org.apache.httpcomponents'
}

我完整的proguard文件

-keep class * implements android.os.Parcelable {
*;
}



# https://github.com/huanghaibin-dev/CalendarView
-keep class com.yocto.wenote.calendar.CustomWeekView {
    public <init>(android.content.Context);
}
-keep class com.yocto.wenote.calendar.CustomMonthView {
    public <init>(android.content.Context);
}
-keep class com.yocto.wenote.calendar.CustomWeekBar {
    public <init>(android.content.Context);
}
-keep class com.yocto.wenote.calendar.FullscreenCustomWeekBar {
    public <init>(android.content.Context);
}
-keep class com.haibin.calendarview.DefaultWeekView {
    public <init>(android.content.Context);
}
-keep class com.haibin.calendarview.DefaultYearView {
    public <init>(android.content.Context);
}

# http://stackoverflow.com/questions/3913338/how-to-debug-with-obfuscated-with-proguard-applications-on-android
# http://proguard.sourceforge.net/index.html#manual/examples.html (Producing useful obfuscated stack traces)
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable,InnerClasses



# https://github.com/bumptech/glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}



# Used in menu/search_toolbar_menu.xml
-keep class com.yocto.wenote.search.SearchView {
*;
}



# setHintTextTypeface & setFloatTextTypeface & setFloatTextColor
-keep class com.google.android.material.textfield.TextInputLayout {
*;
}
# setHintTextTypeface & setFloatTextTypeface
-keep class com.google.android.material.internal.CollapsingTextHelper {
*;
}
# setMaxHeight & setMinimumHeight
-keep class android.widget.ImageView {
  public <methods>;
}



-keep class com.yocto.wenote.recording.RippleBackground$RippleView {
  void setScaleX(***);
  void setScaleY(***);
}



# https://github.com/zhihu/Matisse
-dontwarn com.squareup.picasso.**
-dontwarn com.bumptech.glide.**



# For Google Drive API
-dontwarn com.google.common.**
-dontwarn com.google.api.client.util.**
-keep class * extends com.google.api.client.json.GenericJson {
*;
}
-keep class com.google.api.services.drive.** {
*;
}


# VerifyError in Android 4
# https://github.com/material-components/material-components-android/issues/397
-keep class com.google.android.material.tabs.TabLayout$Tab {
*;
}


# http://developer.android.com/google/play/billing/billing_best_practices.html#obfuscate
-keep class com.android.vending.billing.**



# https://r8.googlesource.com/r8/+/refs/heads/master/compatibility-faq.md
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}



# https://stackoverflow.com/questions/13218772/removing-log-call-using-proguard
-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}



# https://stackoverflow.com/questions/13218772/removing-log-call-using-proguard
-assumenosideeffects class com.yocto.wenote.Utils {
    public static void trackView(...);
    public static void trackEvent(...);
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下保持规则吗?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

    <!-- main css -->
    <link rel="stylesheet" href="css/style.css" />

    <title>Systainable Agriculture - UOP</title>
  </head>
  <body>

    <div id="root"></div>

    <!-- removed other js files to make the code minial -->

    <script src="js/theme.js"></script>

  </body>
</html>

google api客户端通过反射使用com.google.api.client.util.Key: https://developers.google.com/api-client-library/java/google-http-java-client/setup

这似乎对其他报告了类似问题的人有用。