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

后端 未结 4 1150
灰色年华
灰色年华 2020-12-01 10:53

com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description

occurring while trying to do a debug build/test either via Android

相关标签:
4条回答
  • 2020-12-01 10:59

    Robolectric 2.3 depends on JUnit 4.8.1 (version explicit). You're importing JUnit 4.10 (version explicit). Hamcrest is probably simply the first of many duplicates that dex is choking on - try changing your JUnit requirement version to 4.8+ (or excluding JUnit from the Robolectric dependency).

    0 讨论(0)
  • 2020-12-01 11:14

    My project had a dependency on json-simple version 1.1.1, which for some reason has a run-time dependency on junit version 4.1.0, which itself has a dependency on Hamcrest. I could see this if I ran gradle dependencies or alternatively by inspecting the 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
    

    Excluding the junit artifact from json-simple allowed me to build.

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile ('com.googlecode.json-simple:json-simple:1.1.1') {
            exclude module: 'junit'
        }
    }
    
    0 讨论(0)
  • 2020-12-01 11:19

    I solved the error by looking in Android Studio for the exact class called 'Description'. It turned out to be present in 3 jars. One from junit, one from a direct dependency and one from mockito.

    enter image description here

    It turns out that junit, instead of a normal dependency, includes the Hamcrest classes in the junit jar.

    enter image description here

    To be able to solve the problem include junit-dep instead of junit.

    so change

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

    to

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

    Mockito has the same problem/solution: use mockito-core.1.9.5.jar instead of mockito-all.1.9.5.jar

    0 讨论(0)
  • 2020-12-01 11:23

    exclude module: junit

    if you are using json:simple dependency

    0 讨论(0)
提交回复
热议问题