【问题标题】:Android Gradle DexException: Multiple dex files define Lorg/hamcrest/DescriptionAndroid Gradle DexException:多个dex文件定义Lorg/hamcrest/Description
【发布时间】:2014-05-07 06:59:30
【问题描述】:

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:+'
}

【问题讨论】:

  • 您可以尝试升级到更新版本的构建工具 (buildToolsVersion "19.0.3")。保持最新状态似乎对我有很大帮助。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    为了能够解决问题,包括 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

    【讨论】:

    • 这对我有用 powermock androidTestCompile('org.mockito:mockito-core:1.10.8'){ exclude group: 'junit' exclude group: 'org.hamcrest', module: 'hamcrest -core' } androidTestCompile('org.powermock:powermock-api-mockito:1.5.6'){ 排除组:'junit' 排除组:'org.mockito' }
    • 在 Idea 中搜索类的好主意
    • +1 太棒了。网络上有很多关于这个主题的食谱和解决方法。这是我发现的第一条可靠的诊断建议
    【解决方案3】:

    我的项目依赖于 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'
        }
    }
    

    【讨论】:

    • 我很难理解哪个库发生冲突。这是json简单的junit。谢谢。
    【解决方案4】:

    排除模块:junit

    如果你使用json:simple依赖

    【讨论】:

    • 这与上面的答案相同,但在 2 年后,细节较少。这是一种被否决的事情,但它已经排在最后了。
    • 我的意思是,只有在为主题添加新的/不同的分辨率时,才应该提供另一个答案。您的答案与已经提供的答案相同。如果您查看以前的答案,他们提供了两种不同的方法来解决这种情况。你的没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多