如何使R8 + proguard-android-optimize.txt + Google Drive API无缝运行? [英] How to make R8 + proguard-android-optimize.txt + Google Drive API works seamlessly?

查看:60
本文介绍了如何使R8 + proguard-android-optimize.txt + Google Drive API无缝运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们的R8版本

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

Current version is: 1.4.94 (build 390954928f0db9c3b888a367f7f128ce3bbfb160 from go/r8bot (luci-r8-ci-archive-0-5g74)).

当我从

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%正常工作.


Take note that, the above code works 100% fine, before we're switching from proguard-android.txt to proguard-android-optimize.txt.

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

It seems that based on proguard-android-optimize.txt, R8 stripes out some important information, which causes calling to Google Drive API fails.

任何人如何使proguard-android-optimize.txt与Google云端硬盘API兼容?

Anyone how to make proguard-android-optimize.txt work with Google Drive API?

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

If I use ProGuard, version 6.0.3 by applying android.enableR8=false (Still using proguard-android-optimize.txt), there are no issue when running Google Drive API related code.

// 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(...);
}

推荐答案

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

Could you try the following keep rule?

-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}

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

The google api client uses the com.google.api.client.util.Key by reflection: https://developers.google.com/api-client-library/java/google-http-java-client/setup

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

This seems to work for others who has reported a similar issue.

这篇关于如何使R8 + proguard-android-optimize.txt + Google Drive API无缝运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆