Android Gradle DexException : Multiple dex files define Lorg/hamcrest/Description

标签 android gradle android-studio android-gradle-plugin

com.android.dex.DexException:多个 dex 文件定义 Lorg/hamcrest/Description

在尝试通过 Android Studio 或通过我的应用程序上的 Gradle 命令行进行调试构建/测试时发生。

发布版本(没有测试)工作正常,但是一旦包含测试(hamcrest 是一个测试库),构建就会失败并出现上述错误。

我检查了我的模块依赖项,没有 gradle -q dependencies 证实的重复要求。


项目设置.gradle

include ':[library module]'
include ':[main module]'

项目 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

[库模块] build.gradle

apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile 'com.google.zxing:core:3.0.+'
    compile 'com.bugsnag:bugsnag-android:2.1.1+'
}

[主模块] build.gradle

apply plugin: 'android'

android {
    signingConfigs {
    release {
        [...]
    }
}

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            res.srcDirs = ['src/main/res']
        }
        androidTest {
            setRoot('src/test')
        }
        instrumentTest {
        }
    }

    compileSdkVersion 19
    buildToolsVersion '19.0.0'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        testPackageName "[main.packageName].tests"
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

apply plugin: 'android-test'



androidTest {
    // configure the set of classes for JUnit tests
    include '**/*Test.class'

    // configure max heap size of the test JVM
    maxHeapSize = "2048m"
}

repositories {
    maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
    androidTestCompile 'junit:junit:4.10'
    androidTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
    androidTestCompile 'com.squareup:fest-android:1.0.+'
    compile project(':[library module]')
    compile 'com.github.gabrielemariotti.changeloglib:library:1.4.+'
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.google.android.gms:play-services:+'
    compile 'com.android.support:appcompat-v7:+'
    compile ('de.keyboardsurfer.android.widget:crouton:1.8.+') {
        exclude group: 'com.google.android', module: 'support-v4'
    }
    compile files('libs/CWAC-LoaderEx.jar')
    compile 'com.squareup.okhttp:okhttp:1.5.+'
    compile 'com.octo.android.robospice:robospice:1.4.11'
    compile 'com.octo.android.robospice:robospice-cache:1.4.11'
    compile 'com.octo.android.robospice:robospice-retrofit:1.4.11'
    compile 'com.commonsware.cwac:security:0.1.+'
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
}

最佳答案

我通过在 Android Studio 中查找名为“Description”的确切类解决了该错误。原来它存在于 3 个 jar 中。一个来自 junit,一个来自直接依赖项,一个来自 mockito。

enter image description here

事实证明,junit 不是普通的依赖项,而是在 junit jar 中包含 Hamcrest 类。

enter image description here

为了能够解决问题,请使用 junit-dep 而不是 junit。

所以改变

androidTestCompile('junit:junit:4.8.+')

androidTestCompile('junit:junit-dep:4.8.+')

Mockito 有同样的问题/解决方案:使用 mockito-core.1.9.5.jar 代替 mockito-all.1.9.5.jar

关于Android Gradle DexException : Multiple dex files define Lorg/hamcrest/Description,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22702267/

相关文章:

groovy - 什么是运算符<<(小于两倍)在gradle中?

android - Gradle Firebase AdMob 依赖项与 app compat v7 28.0.0 冲突

Android:NotificationListenerService 未再次创建

android - 在 android 中进行 USSD 调用

android - 如何使用手机传感器(不包括 GPS)绘制(地形) map ?

android - 如何在 Firebase (Android) 中删除特定的 child

gradle - 在 Debug模式下构建 Flutter 应用程序时,Gradle 详细日志在哪里?

android-studio - 在 flutter 中将文件添加到git忽略

android - 我在哪里可以将我的自定义模板放在 Android Studio 4.1 中

java - 如何在我的 android studio 应用程序中实现 TLS V1.1 和 V1.2?