Why am I getting a ClassNotFoundException from Play Services lib when using Proguard?

十年热恋 提交于 2019-12-10 18:29:11

问题


I just turned on ProGuard on my build and now I'm getting a

java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.chimera.GmsModuleInitializer" on path: DexPathList[[zip file "/system/app/PlayGames.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

The docs say that everything that I need to use Proguard with Play Services should be included by the Android Gradle plugin:

Note: ProGuard directives are included in the Play services client libraries to preserve the required classes. The Android Plugin for Gradle automatically appends ProGuard configuration files in an AAR (Android ARchive) package and appends that package to your ProGuard configuration. During project creation, Android Studio automatically creates the ProGuard configuration files and build.gradle properties for ProGuard use. To use ProGuard with Android Studio, you must enable the ProGuard setting in your build.gradle buildTypes. For more information, see the ProGuard guide.

This the important part of my app module build.gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        ...
    }
    ...
    buildTypes {
        ...{
            applicationIdSuffix ".debug"
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
}


dependencies {
    ...

    //google play services
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    compile 'com.google.android.gms:play-services-analytics:8.4.0'
    compile 'com.google.android.gms:play-services-location:8.4.0'
}

This is my top level build.gradle file:

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
    }
}

What am I missing?


回答1:


ClassNotFoundException usually occurs when an application tries to load in a class through its string name but no definition for the class with the specified name could be found.

From this forum, you can fix it by adding com.google.android.gms.** { *; }.

Just add to your proguard-project.txt:

  • keep class com.google.android.gms.** { *; }
  • dontwarn com.google.android.gms.**

You can also check on the suggested comment in this SO question.

Google Play Services aars contain proguard.txt with the necessary clauses. So the setting shouldn't really be necessary. You can investigate what happened with the fragment in ProGuard output files. Check app/build/output/mapping/{buildVariant}/usage.txt and mapping.txt. The fragment should be mentioned in one of those.

Hope this helps!



来源:https://stackoverflow.com/questions/39279008/why-am-i-getting-a-classnotfoundexception-from-play-services-lib-when-using-prog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!