当我在Android中为一个Android应用程序运行->应用程序时,我得到了这个错误
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/common/annotations/Beta;
...下面是gradlew -q :app:依赖性的输出
+--- 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')
}
}发布于 2014-09-22 23:17:35
com.google.common.annotations包似乎是番石榴的一部分。我在依赖项中看到了两次不同的变化:一次是作为Google客户机的一部分,一次是作为您自己的依赖:
+--- 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库的不同变体中)被包含在其他依赖项中。您需要找到一种方法来排除这些重复的依赖关系,或者可能只是确保在所有依赖项中使用相同的版本。
您可以尝试将番石榴模块从依赖项中排除出来。因此,在定义API客户端模块的地方,为番石榴模块添加一个排除规则:
compile ('com.google.api-client:google-api-client:1.19.0') {
exclude group: 'com.google.guava', module: 'guava-jdk5'
}我不能保证这不会给Google客户端库带来问题(因为它们是两个不同版本的番石榴),但值得一试。
编辑:从您的依赖项中,尝试更改以下内容:
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 client-android库实际上并不包含番石榴--我没有意识到其中有两个类似名称的依赖项。
发布于 2014-12-02 19:24:12
对于那些在Android应用程序中使用的用户来说:
compile(project(path: ':backend', configuration: 'android-endpoints')) {
exclude(module: 'guava-jdk5')
}其中backend是您的模块名称与AppEngine应用程序。
在所有其他情况下,只需查找guava-jdk5传递依赖并排除它。
发布于 2016-02-21 23:55:28
这里是正确的解决方案,为我工作。将番石榴-jdk5 5依赖模块排除在每个单独的导入中都是适得其反的(对我来说,它没有起作用,因为我的应用程序依赖的后端有一些内部依赖项,这暴露了这种方法的真正问题)。
以下方法解决了这个问题,是推荐的方法:
configurations {
all*.exclude group: 'com.google.guava', module: 'guava-jdk5'
}资料来源:https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html
https://stackoverflow.com/questions/25984426
复制相似问题