Android Studio多个dex文件渐变错误

Android Studio多个dex文件渐变错误,android,android-studio,android-gradle-plugin,build.gradle,Android,Android Studio,Android Gradle Plugin,Build.gradle,我在Android Studio中为Android应用程序运行->应用程序时出现此错误 UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lcom/google/common/annotations/Beta; ... 下面是gradlew-q:app:dependencies的输出 +--- project :shared +--- com.google.ap

我在Android Studio中为Android应用程序运行->应用程序时出现此错误

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/google/common/annotations/Beta;
...
下面是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
这是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')
    }   
}

com.google.common.annotations
包似乎是番石榴的一部分。我在依赖项中看到了两种略有不同的变体:一种是作为Google API客户端的一部分,另一种是作为您自己的依赖项:

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

因此,这个错误的原因是,您的其他依赖项包含了在多个dex文件(在Guava库的不同变体中)中定义的相同类。您需要找到一种方法来排除这些重复的依赖项,或者可能只是确保在所有依赖项中使用相同的版本

您可以尝试将guava模块从一个依赖项中排除。因此,在定义了API客户机模块的地方,为guava模块添加一个排除规则:

compile ('com.google.api-client:google-api-client:1.19.0') {
    exclude group: 'com.google.guava', module: 'guava-jdk5'
}
我不能保证这不会给GoogleAPI客户端库带来问题(因为它们是Guava的两个不同版本),但值得一试

编辑:从您的部门,尝试更改以下内容:

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 !!!
致:


google api客户端android库实际上并不包含Guava——我不知道您在其中有两个类似命名的依赖项。

看起来您不能在同一个项目中同时使用Guava和Guava-jdk5。GUAVA JDK5仍在维护中,所以请考虑将您的项目的番石榴引用改为GUAVA JDK5:

compile 'com.google.guava:guava-jdk5:17.0'

对于在Android应用程序中使用谷歌云端点的用户:

compile(project(path: ':backend', configuration: 'android-endpoints')) {
    exclude(module: 'guava-jdk5')
}
其中,
backend
是使用AppEngine应用程序的模块的名称


在所有其他情况下,只需查找
guava-jdk5
可传递依赖项并排除它。

这是正确的解决方案,对我来说很有用。从每个导入中排除guava-jdk5依赖模块会产生相反的效果(对我来说,它不起作用,因为我的应用程序所依赖的后端有一些内部依赖,这暴露了这种方法的真正问题)

以下方法解决了问题,是推荐的方法:

configurations {
    all*.exclude group: 'com.google.guava', module: 'guava-jdk5'
}

来源

谢谢KCopock。我想你一定是对的,但无论我如何尝试,我都不能从com.google.api-client编译中排除guava模块。为什么会这样?我使用:compile(group:'com.google.api client',name:'google api client',version:'1.19.0'){exclude(group:'com.google.guava')}和我能想到的任何其他排除可能性,但它永远不会被排除。However,kcopock,我知道你是正确的,因为我注释掉了第二个guava编译(14.0),我可以构建。不过,我还是想排除第一个番石榴模块,但grade似乎不允许。你知道为什么吗?你确定你完全按照上面所示改变了依赖关系吗?我只是自己试过,没有我的排除线,它无法像你的一样组装。在排除行中,它成功了。据我所知,您将排除放在google api客户端ANDROID模块中,但我们试图排除的guava模块与前面的google api客户端模块一起放在顶部。因此,当我将exclude与您显示的编译完全相同时,它什么也不做,因为在特定的编译中没有番石榴。事实上,它无法再次组装。你能发布你的整个依赖项块吗?非常感谢你,我已经找了很久了。这救了我。谢天谢地,这个soln为我工作。从端点库中排除guava-jdk5-如上所述-听起来像是一个更具吸引力的解决方案,但不起作用。为此,我节省了几个小时。可能是最好的解决方案。谢谢,如果这有帮助,请投票,我确实认为这是正确的解决方案,比公认的答案更好
compile(project(path: ':backend', configuration: 'android-endpoints')) {
    exclude(module: 'guava-jdk5')
}
configurations {
    all*.exclude group: 'com.google.guava', module: 'guava-jdk5'
}