Android Studio 多个 dex 文件 gradle 错误 [英] Android Studio Multiple dex files gradle error
问题描述
当我在 Android Studio 中运行->Android 应用程序的应用程序时出现此错误
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;
...
这是 gradlew -q :app:dependencies 的输出
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
这是来自 build.gradle 的依赖块
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')
}
}
推荐答案
com.google.common.annotations
包似乎是 Guava 的一部分.我在依赖项中看到它两次,略有不同:一次作为 Google API 客户端的一部分,一次作为您自己的依赖项:
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
和
+--- com.google.guava:guava:14.0.+ -> 14.0.1
因此,此错误的原因是您的其他依赖项包含在多个 dex 文件(在 Guava 库的不同变体中)中定义的相同类.您需要找到一种方法来排除这些重复的依赖项,或者可能只是确保您在所有依赖项中使用相同的版本.
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.
您可以尝试的一件事是从依赖项之一中排除 guava 模块.因此,在定义了 API 客户端模块的地方,为 guava 模块添加排除规则:
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'
}
我不能保证这不会对 Google API 客户端库造成问题(因为它们是 Guava 的两个不同版本),但值得一试.
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.
从你的依赖,尝试改变这个:
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 !!!
到:
compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
exclude(group: 'com.google.guava', module: 'guava-jdk5')
google-api-client-android 库实际上并不包含 Guava -- 我没有意识到您在那里有两个类似命名的依赖项.
The google-api-client-android library doesn't actually contain Guava -- I didn't realize you had two similarly named dependencies in there.
这篇关于Android Studio 多个 dex 文件 gradle 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!