com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description
occurring while trying to do a debug build/test either via Android
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).
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'
}
}
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
exclude module: junit
if you are using json:simple
dependency