Android Studio 多个 dex 文件 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.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 的依赖 block

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

最佳答案

对于在 Android 应用中使用 Google Cloud Endpoints 的用户:

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

其中 backend 是您的 AppEngine 应用模块的名称。

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

关于Android Studio 多个 dex 文件 gradle 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25984426/

相关文章:

android - 背景上的 FragmentStatePagerAdapter IndexOutOfBound

java - Android Studio 2.1 将字节码转换为 dex 时出错

android - 构建版本更改后无法获取构建

android-gradle-plugin - 安卓工作室 |依赖管理

android - 如何使用主页小部件启动 Activity

Android fragment 未添加到返回堆栈

java - Android Studio 无法从外部库自动完成 XML 中的值

java - Android导入项目失败

android - 如何解决依赖冲突?

android - 当 edittext 的默认文本为浮点值时,编辑文本光标重置为左侧