简体   繁体   English

Android Studio多个dex文件gradle错误

[英]Android Studio Multiple dex files gradle error

I get this error when I Run->app for an Android application in Android Studio 当我在Android Studio中运行Android应用程序时,我收到此错误

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 这是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 这是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')
    }   
}

For those who are using Google Cloud Endpoints in Android app: 对于在Android应用中使用Google Cloud Endpoints的用户:

compile(project(path: ':backend', configuration: 'android-endpoints')) {
    exclude(module: 'guava-jdk5')
}

Where backend is name of your module with AppEngine app. backend是使用AppEngine应用程序的模块名称。

In every other case just look for guava-jdk5 transitive dependency and exclude it. 在其他所有情况下,只需查找guava-jdk5传递依赖并排除它。

The com.google.common.annotations package seems to be part of Guava. com.google.common.annotations包似乎是Guava的一部分。 I see it in the dependencies twice in slightly different variations: once as part of Google API Client, once as your own dependency: 我在依赖项中看到了两次略有不同的变体:一次作为Google API Client的一部分,一次作为您自己的依赖项:

+--- 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. 因此,此错误的原因是您在其他依赖项中包含的多个dex文件(在Guava库的不同变体中)中定义了相同的类。 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. 您可以尝试的一件事是从其中一个依赖项中排除guava模块。 So, where you have the API Client module defined, add an exclusion rule for the guava module: 因此,在定义API客户端模块的位置,为guava模块添加排除规则:

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. 我不能保证这不会给Google API客户端库带来问题(因为它们是两个不同版本的Guava),但值得一试。

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. google-api-client-android库实际上并不包含Guava - 我没有意识到你有两个类似命名的依赖项。

It seems you can't use both guava and guava-jdk5 in the same project. 看来你不能在同一个项目中同时使用guava和guava-jdk5。 Guava-jdk5 is still being maintained, so consider changing your project's guava reference to guava-jdk5: Guava-jdk5仍在维护中,因此请考虑将项目的guava引用更改为guava-jdk5:

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

Here is the correct solution solution, worked for me. 这是正确的解决方案,对我有用。 Excluding the guava-jdk5 dependency module from each individual import is counterproductive (for me it didn't work because I had some internal dependencies coming from my backend on which my app depends, and this exposes the real issue with this approach). 从每个单独的导入中排除guava-jdk5依赖模块是适得其反的(对我来说它不起作用,因为我有一些来自我的应用所依赖的后端的内部依赖,这暴露了这种方法的真正问题)。

The following solved the problem and is the recommended approach: 以下解决了问题,是推荐的方法:

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

Source : https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html 资料来源https//docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM