Android的工作室多DEX文件摇篮错误

人气:1,273 发布:2022-09-10 标签: android android-studio build.gradle android-gradle

问题描述

当我运行 - 我得到这个错误>应用程序在Android的工作室Android应用程序

 意外的顶级例外:
    com.android.dex.DexException:多DEX文件定义LCOM /谷歌/普通/注解/β;
...
 

这里是gradlew -q的输出:应用:依赖

  + ---项目:共享
+ --- com.google.api客户端:谷歌的API客户端:1.19.0
| + --- com.google.oauth客户端:谷歌的OAuth客户端:1.19.0
| | + --- com.google.http客户端:谷歌的HTTP客户端:1.19.0
| | | + --- com.google code.findbugs:JSR305:1.3.9
| | | \ --- org.apache.httpcomponents:HttpClient的:4.0.1
| | | + --- org.apache.httpcomponents:的HttpCore:4.0.1
| | | +  - 共享记录:共享记录:1.1.1
| | | \ --- commons- codeC:commons- codeC:1.3
| | \ --- com.google code.findbugs:JSR305:1.3.9
| + --- com.google.http客户端:谷歌的HTTP客户jackson2:1.19.0
| | + --- com.google.http客户端:谷歌的HTTP客户端:1.19.0(*)
| | \ --- com.fasterxml.jackson.core:杰克逊核心:2.1.3
| \ --- com.google.guava:番石榴JDK5:13.0
+ --- com.google.http客户端:谷歌的HTTP客户GSON:1.19.0
| \ --- com.google code.gson:GSON:2.1
+ --- com.google.api客户端:谷歌API-客户的android:1.19.0
| + --- com.google.api客户端:谷歌的API客户端:1.19.0(*)
| \ --- com.google.http客户端:谷歌的HTTP客户端的android:1.19.0
| \ --- com.google.http客户端:谷歌的HTTP客户端:1.19.0(*)
+ --- com.google.http客户端:谷歌的HTTP客户端的Andr​​oid版本:1.19.0(*)
+ --- com.google.guava:番石榴:14.0 +  - >。 14.0.1
+ ---项目:后端应用程序引擎
| \ --- com.google.api客户端:谷歌的API客户端 - 机器人:1.19.0(*)
+ --- com.android.support:appcompat-v7:20.0.0
| \ --- com.android.support:support-v4:20.0.0
| \ --- com.android.support:support-annotations:20.0.0
+ --- com.google.android.gms:播放服务:5.0.89
\ --- com.google.maps.android:android-maps-utils:0.3.+  - > 0.3.1
 

下面是build.gradle依赖块

 相关性{
    编译文件树(导演:库,包括:['的* .jar'])
    编制项目(:共享)
    //添加谷歌的API客户端库。
    编译(组:com.google.api客户端,名称:谷歌的API客户端,版本:1.19.0){
        //排除工件在Android SDK /运行时提供。
        排除(组:com.google.guava')//  - !这似乎并没有工作!
        排除(组:XPP3',模块:XPP3)
        排除(组:org.apache.httpcomponents',模块:HttpClient的)
        排除(组:JUnit的',模块:JUnit的)
        排除(组:com.google.android',模块:机器人)
        排除(组:com.google.http客户端,模块:谷歌的HTTP客户端)
    }

    编译(com.google.http客户端:谷歌的HTTP客户GSON:1.19.0'){
        排除模块:HttpClient的
        排除(组:com.google.http客户端,模块:谷歌的HTTP客户端)
    }

    编译(组:com.google.api客户端,名称:谷歌API的客户端Android的版本:1.19.0){
        排除(组:com.google.android.gms',模块:播放服务)
        排除组:com.google.guava',模块:番石榴,JDK5
    }

   编译(组:com.google.http客户端,名称:谷歌的HTTP客户端,Android的版本:1.19.0){
        排除(组:com.google.android',模块:机器人)
    }

    //这是所使用的谷歌的HTTP客户机库。
    编译(组:com.google.guava,名称:番石榴,版本:'14 0.0 +')

    //  - 端点
    依赖{
        编制项目(路径::后端应用程序引擎,配置:Android的终端)
    }

    编译com.android.support:appcompat-v7:20.0.0
    编译(com.google.android.gms:播放服务:5.0.89'){
        排除(组:com.android.support',模块:支持-V4)
    }
    编译(com.google.maps.android:android-maps-utils:0.3.+'){
        排除(组:com.google.android.gms',模块:播放服务)
    }
}
 

解决方案

com.google.common.annotations 包似乎是番石榴的一部分。我看到它在依赖两次略有不同的变化:曾经是谷歌的API客户端的一部分,曾经作为自己的依赖关系:

  + --- com.google.api客户端:谷歌的API客户端:1.19.0
| \ --- com.google.guava:番石榴JDK5:13.0
 

  + --- com.google.guava:番石榴:14.0 +  - > 14.0.1
 

所以这个错误的原因是,你必须在多个DE​​X文件中定义的相同类(在番石榴库的不同变体)被列入您的其他依赖。你需要找到一种方法来排除这些重复的依赖关系,或可能只是确保您使用相同的版本在所有依赖关系。

有一件事你可以尝试是从依赖项之一排除番石榴模块。所以,在这里你必须定义的API客户端模块,为番石榴模块添加排除规则:

 编译('com.google.api客户端:谷歌的API客户端:1.19.0'){
    排除组:com.google.guava',模块:番石榴,JDK5
}
 

我不能保证这不会导致对谷歌API客户端库的问题(因为它们是两个不同的版本番石榴),但它是值得一试。

编辑:从您的depdencies,尝试改变这样的:

 编译(组:com.google.api客户端,名称:谷歌的API客户端,版本:1.19.0){
    //排除工件在Android SDK /运行时提供。
    排除(组:com.google.guava')//  - !这似乎并没有工作!
 

 编译(组:com.google.api客户端,名称:谷歌的API客户端,版本:1.19.0){
    排除(组:com.google.guava',模块:番石榴JDK5)
 

在谷歌-API客户端的Andr​​oid库实际上并不包含番石榴 - 我不知道你在那里有两个类似的命名依赖

I get this error when I Run->app for an Android application in Android Studio

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/google/common/annotations/Beta;
...

And here is the output of gradlew -q :app:dependencies

+--- project :shared
+--- com.google.api-client:google-api-client:1.19.0
|    +--- com.google.oauth-client:google-oauth-client:1.19.0
|    |    +--- com.google.http-client:google-http-client:1.19.0
|    |    |    +--- com.google.code.findbugs:jsr305:1.3.9
|    |    |    \--- org.apache.httpcomponents:httpclient:4.0.1
|    |    |         +--- org.apache.httpcomponents:httpcore:4.0.1
|    |    |         +--- commons-logging:commons-logging:1.1.1
|    |    |         \--- commons-codec:commons-codec:1.3
|    |    \--- com.google.code.findbugs:jsr305:1.3.9
|    +--- com.google.http-client:google-http-client-jackson2:1.19.0
|    |    +--- com.google.http-client:google-http-client:1.19.0 (*)
|    |    \--- com.fasterxml.jackson.core:jackson-core:2.1.3
|    \--- com.google.guava:guava-jdk5:13.0
+--- com.google.http-client:google-http-client-gson:1.19.0
|    \--- com.google.code.gson:gson:2.1
+--- com.google.api-client:google-api-client-android:1.19.0
|    +--- com.google.api-client:google-api-client:1.19.0 (*)
|    \--- com.google.http-client:google-http-client-android:1.19.0
|         \--- com.google.http-client:google-http-client:1.19.0 (*)
+--- com.google.http-client:google-http-client-android:1.19.0 (*)
+--- com.google.guava:guava:14.0.+ -> 14.0.1
+--- project :backend-appengine
|    \--- com.google.api-client:google-api-client-android:1.19.0 (*)
+--- com.android.support:appcompat-v7:20.0.0
|    \--- com.android.support:support-v4:20.0.0
|         \--- com.android.support:support-annotations:20.0.0
+--- com.google.android.gms:play-services:5.0.89
\--- com.google.maps.android:android-maps-utils:0.3.+ -> 0.3.1

Here is the dependency block from build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':shared')
    // Add the Google API client library.
    compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
        // Exclude artifacts that the Android SDK/Runtime provides.
        exclude(group: 'com.google.guava')     //-- !!! this does not seem to work !!!
        exclude(group: 'xpp3', module: 'xpp3')
        exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
        exclude(group: 'junit', module: 'junit')
        exclude(group: 'com.google.android', module: 'android')
        exclude(group: 'com.google.http-client', module: 'google-http-client')
    }

    compile('com.google.http-client:google-http-client-gson:1.19.0') {
        exclude module: 'httpclient'
        exclude(group: 'com.google.http-client', module: 'google-http-client')
    }

    compile(group: 'com.google.api-client', name: 'google-api-client-android', version: '1.19.0') {
        exclude(group: 'com.google.android.gms', module: 'play-services')
        exclude group: 'com.google.guava', module: 'guava-jdk5'
    }

   compile(group: 'com.google.http-client', name: 'google-http-client-android', version: '1.19.0') {
        exclude(group: 'com.google.android', module: 'android')
    }

    // This is used by the Google HTTP client library.
    compile(group: 'com.google.guava', name: 'guava', version: '14.0.+')

    //-- endpoints
    dependencies {
        compile project(path: ':backend-appengine', configuration: 'android-endpoints')
    }

    compile 'com.android.support:appcompat-v7:20.0.0'
    compile ('com.google.android.gms:play-services:5.0.89') {
        exclude(group: 'com.android.support', module: 'support-v4')
    }
    compile ('com.google.maps.android:android-maps-utils:0.3.+') {
        exclude(group: 'com.google.android.gms', module: 'play-services')
    }   
}

解决方案

The com.google.common.annotations package seems to be part of Guava. I see it in the dependencies twice in slightly different variations: once as part of Google API Client, once as your own dependency:

+--- com.google.api-client:google-api-client:1.19.0
|    \--- com.google.guava:guava-jdk5:13.0

and

+--- com.google.guava:guava:14.0.+ -> 14.0.1

So the cause of this error is that you have the same classes defined in multiple dex files (in different variations of the Guava library) being included by your other dependencies. You'll need to find a way to exclude these duplicated dependencies, or possibly just ensure that you use the same version across all dependencies.

One thing you could try is to exclude the guava module from one of the dependencies. So, where you have the API Client module defined, add an exclusion rule for the guava module:

compile ('com.google.api-client:google-api-client:1.19.0') {
    exclude group: 'com.google.guava', module: 'guava-jdk5'
}

I can't guarantee this won't cause problems for the Google API Client library (since they are two different versions of Guava) but it's worth a try.

EDIT: From your depdencies, try changing this:

compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
    // Exclude artifacts that the Android SDK/Runtime provides.
    exclude(group: 'com.google.guava')     //-- !!! this does not seem to work !!!

to:

compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
    exclude(group: 'com.google.guava', module: 'guava-jdk5')

The google-api-client-android library doesn't actually contain Guava -- I didn't realize you had two similarly named dependencies in there.

284