使用proguard

时间:2018-09-20 10:41:59

标签: android google-play-services android-proguard

当我尝试使用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删除gms规则时,无需添加任何内容来保护proguard,

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库是最新的。使用适用于Google Play的位置和位置的15.0.1版(最新)

Proguard Output

1 个答案:

答案 0 :(得分:0)

除了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方法的情况下,提供可靠的规则是非常不可能的。