首页 文章

Android Gradle DexException:多个dex文件定义Lorg / hamcrest / Description

提问于
浏览
27

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

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

发布版本(没有测试)工作正常,但只要包含测试( hamcrest 是测试库),构建就会因上述错误而失败 .

我've checked my module dependencies and there'没有重复的要求 gradle -q dependencies 证实了这一点 .


Project settings.gradle

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

Project 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()
    }
}

[library module] 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+'
}

[main module] 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:+'
}

4 回答

  • 26

    Robolectric 2.3依赖于JUnit 4.8.1(版本显式) . 您正在导入JUnit 4.10(版本显式) . Hamcrest可能只是dex窒息的许多重复项中的第一个 - 尝试将JUnit需求版本更改为4.8(或者从Robolectric依赖项中排除JUnit) .

  • 0

    我通过在Android Studio中查找名为“描述”的确切类来解决错误 . 原来它出现在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

  • 10

    我的项目依赖于json-simple version 1.1.1,由于某种原因,它对junit版本4.1.0有一个运行时依赖,它本身有一个dependency on Hamcrest . 如果我运行 gradle dependencies 或者通过检查json-simple POM.xml,我可以看到这个 .

    // compile - Classpath for compiling the main sources.
        \--- com.googlecode.json-simple:json-simple:1.1.1
             \--- junit:junit:4.10
                  \--- org.hamcrest:hamcrest-core:1.1
    

    从json-simple中排除junit工件允许我构建 .

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile ('com.googlecode.json-simple:json-simple:1.1.1') {
            exclude module: 'junit'
        }
    }
    
  • 39

    排除模块: junit

    如果你正在使用 json:simple 依赖

相关问题