使用 proguard 时构建期间出现 GMS 错误

Posted

技术标签:

【中文标题】使用 proguard 时构建期间出现 GMS 错误【英文标题】:GMS Error during build while using proguard 【发布时间】:2019-02-24 15:44:04 【问题描述】:

当我尝试在使用 proguard 的同时构建我的项目时,我在预验证时收到以下错误。

Preverifying...
Unexpected error while performing partial evaluation:
  Class       = [com/google/android/gms/internal/zzmz]
  Method      = [zza(Lcom/google/android/gms/internal/zzmh;Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lcom/google/android/gms/internal/zzne;Lcom/google/android/gms/internal/zzgf;Lcom/google/android/gms/internal/zzmy;)Lcom/google/android/gms/internal/zzmk;]
  Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [com/google/android/gms/internal/zzmk] (with 1 known super classes) and [java/lang/String] (with 2 known super classes))
Unexpected error while preverifying:
  Class       = [com/google/android/gms/internal/zzmz]
  Method      = [zza(Lcom/google/android/gms/internal/zzmh;Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lcom/google/android/gms/internal/zzne;Lcom/google/android/gms/internal/zzgf;Lcom/google/android/gms/internal/zzmy;)Lcom/google/android/gms/internal/zzmk;]
  Exception   = [java.lang.IllegalArgumentException] (Can't find common super class of [com/google/android/gms/internal/zzmk] (with 1 known super classes) and [java/lang/String] (with 2 known super classes))
Warning: Exception while processing task java.io.IOException: java.lang.IllegalArgumentException: Can't find common super class of [com/google/android/gms/internal/zzmk] (with 1 known super classes) and [java/lang/String] (with 2 known super classes)

我在 proguard 中添加了以下内容,其他库也有其他规则。

-keep class com.google.android.gms.**  *; 
-dontwarn com.google.android.gms.**
-keep class com.facebook.ads.**  *; 
-dontwarn com.google.android.gms.ads.**

我的 gradle.build 有以下库

implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:preference-v7:$rootProject.supportLibraryVersion"
    implementation "com.android.support:design:$rootProject.supportLibraryVersion"
    implementation "com.android.support:palette-v7:$rootProject.supportLibraryVersion"
    implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
    implementation "android.arch.persistence.room:rxjava2:$rootProject.roomVersion"
    //ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:$rootProject.lifecycleVersion"
    implementation "android.arch.work:work-runtime:$rootProject.workManagerVersion"
    implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"
    implementation "com.jakewharton.timber:timber:$rootProject.timberVersion"
    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.interceptorVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rx2AndroidVersion"
    implementation "io.reactivex.rxjava2:rxjava:$rootProject.rx2AndroidVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$rootProject.retrofitVersion"
    implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
    implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
    implementation "com.android.billingclient:billing:$rootProject.billingVersion"
    implementation "com.facebook.android:audience-network-sdk:$rootProject.facebookAds"
    implementation ("com.google.android.gms:play-services-places:$rootProject.gPlacesVersion")
        exclude module: 'play-services-ads'
    
    implementation ("com.google.android.gms:play-services-location:$rootProject.gLocationVersion") 
        exclude module: 'play-services-ads'
    
    implementation 'com.android.support:multidex:1.0.3'

    implementation "com.google.firebase:firebase-core:$rootProject.firebaseCore"
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
//    implementation "com.google.firebase:firebase-config:$rootProject.firebaseConfig"
    implementation "com.google.firebase:firebase-perf:$rootProject.firebasePerf"

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"
    annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
    annotationProcessor "android.arch.lifecycle:compiler:$rootProject.lifecycleVersion"
    annotationProcessor "com.github.bumptech.glide:compiler:$rootProject.glideVersion"
    annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    debugImplementation 'com.amitshekhar.android:debug-db:1.0.4'

根据 Google Play 服务设置站点,无需向 proguard 添加任何内容,当我从 proguard 中删除 gms 规则时,会得到一长串类似这样的警告

Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced class com.google.android.gms.common.internal.zzf$zzb
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced class com.google.android.gms.common.internal.zzf$zzc
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced class com.google.android.gms.internal.zzaf
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced class com.google.android.gms.internal.zzaf$zza
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced class com.google.android.gms.internal.zzaf$zza
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced method 'void disconnect()' in program class com.google.android.gms.internal.zzapu
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced method 'boolean isConnected()' in program class com.google.android.gms.internal.zzapu
Warning: com.google.android.gms.internal.zzapt$zza: can't find referenced method 'boolean isConnecting()' in program class com.google.android.gms.internal.zzapu

警告列表继续。如果需要,我可以添加其余的 proguard 规则。 根据 Firebase 网站,我使用的 Google Play 服务是 4.1.0,我的 Firebase 库是最新的。使用版本 15.0.1(最新)的 Google Play Google Play 位置和位置

Proguard Output

【问题讨论】:

在没有这些dontwarn 规则的情况下运行时能否添加ProGuard 的output @MartinZeitler 我收到了问题中提到的警告错误。 这些几乎没有关系,而输出确实......至少是它抱怨的部分。 @MartinZeitler 抱歉,我对 proguard 有点陌生,我如何获得输出? 它是常规构建日志的一部分 - 它通常非常明确地告诉我们要做什么(要定义哪些规则)......添加 -verbose 选项也可能有助于了解发生了什么在那里。 【参考方案1】:

除了com.google.android.gms.internal.* 的规则(意思是:Google Services)之外,您还缺少很多规则。 manual 解释了所有这些建议;虽然我仍然可以为那一类留下一个例子:

-verbose
-keep,includedescriptorclasses class com.google.android.gms.internal.**  *; 
-keep class com.google.android.gms.internal.** com.google.android.gms.internal.** initialize(android.content.Context);
# -dontnote com.google.android.gms.internal.**
# -dontwarn com.google.android.gms.internal.**

刚刚注释掉了dontnotedontwarn,因为在开发规则集时更容易得到通知。添加修饰符includedescriptorclasses 至少应该已经减少了几行抱怨——虽然你必须定义规则,直到没有更多的抱怨。这是一个漫长而麻烦的过程,没有任何默认配方 - 并且无法提供可靠的规则,同时无法使用trial & error的方法。

【讨论】:

以上是关于使用 proguard 时构建期间出现 GMS 错误的主要内容,如果未能解决你的问题,请参考以下文章

更新到 Google Play 服务 11.8.0 后 Proguard 无法正常工作

Proguard 错误:“找不到...的通用超类”

Proguard 和 com.google.android.gms.common.api.internal.BasePendingResult$ReleasableResultGuardian

库版本 11.2.0 的 firebase ProGuard 错误(警告)

评估指令时出现 Proguard 错误

Proguarding Google Play Services 7.+ 导致 com.google.android.gms.internal.zzig:找不到引用的方法 'boolean isAtt