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

廉价感情. 提交于 2019-11-27 08:00:00

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).

userM1433372

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.

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

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

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

exclude module: junit

if you are using json:simple dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!