为什么在使用Proguard时从Play Services lib中获得ClassNotFoundException? [英] Why am I getting a ClassNotFoundException from Play Services lib when using Proguard?

查看:216
本文介绍了为什么在使用Proguard时从Play Services lib中获得ClassNotFoundException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我的构建中打开了ProGuard,现在我得到了一个

  java.lang.ClassNotFoundException:Didn' t在路径中找到类com.google.android.gms.chimera.GmsModuleInitializer:DexPathList [[zip文件/system/app/PlayGames.apk],本地库目录= [/vendor/lib,/ system / lib]] 

文档说,我需要在Play服务中使用Proguard的所有内容都应该包含在Android Gradle插件:


注意:Play服务客户端
库中包含ProGuard指令以保留所需的类。用于
Gradle的Android插件会自动在AAR
(Android ARchive)包中附加ProGuard配置文件,并将该包附加到您的ProGuard
配置中。在项目创建期间,Android Studio会自动为
创建ProGuard配置文件,并为ProGuard使用build.gradle属性
。要在Android Studio中使用ProGuard,您必须在您的build.gradle buildType中启用
ProGuard设置。有关更多
的信息,请参阅ProGuard指南。


这是我的应用程序模块build.gradle文件的重要部分:

  apply plugin:'com.android.application'

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


依赖关系{
...

//谷歌播放服务
编译'com.google.android .gms:play-services-gcm:8.4.0'
compile'c​​om.google.android.gms:play-services-analytics:8.4.0'
compile'c​​om.google.android.gms :play-services-location:8.4.0'
}

这是我的顶级build.gradle文件:

  buildscript {
repositories {
jcenter()
mavenCentral()
}
依赖关系{
classpath'com.android.tools.build:gradle:2.1.2'
}


allprojects {
repositories {
jcenter()
mavenCentral()
}
}
解决方案p> ClassNotFoundException 通常在应用程序尝试加载类时发生通过它的字符串名称,但没有定义具有指定名称的类可以找到。



论坛,您可以通过添加 com.google.android.gms。** {*; }


只需添加到您的proguard-project.txt中即可:




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

  • dontwarn com.google.android.gms。**


您也可以在此 SO问题


Google Play服务aars包含带必要条款的proguard.txt。所以设置不应该是必要的。您可以调查ProGuard输出文件中片段发生的情况。检查app / build / output / mapping / {buildVariant} /usage.txt和mapping.txt。这些片段应该在其中一个中提到。


希望这有助于您!


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?

解决方案

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!

这篇关于为什么在使用Proguard时从Play Services lib中获得ClassNotFoundException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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