2

I encounter the following runtime errors when doing the instrumentation test.

Unknown source file : UNEXPECTED TOP-LEVEL EXCEPTION:
Unknown source file : com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description;
Unknown source file :   at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
Unknown source file :   at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
Unknown source file :   at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
Unknown source file :   at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
Unknown source file :   at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
Unknown source file :   at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:502)
Unknown source file :   at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
Unknown source file :   at com.android.dx.command.dexer.Main.run(Main.java:277)
Unknown source file :   at com.android.dx.command.dexer.Main.main(Main.java:245)
Unknown source file :   at com.android.dx.command.Main.main(Main.java:106)

I think there are many similar questions with this issue.

However, most of them suggest to exclude the hamcrest librarty from other dependencies, but it did not solve my problem :(

Here is the dependencies part of my build.gradle

dependencies {
    androidTestCompile files('libs/java-hamcrest-2.0.0.0.jar')
    androidTestCompile files('libs/hamcrest-json-0.2.jar')
    androidTestCompile files('libs/jsonassert-1.2.3.jar')
    androidTestCompile ('org.powermock:powermock-module-junit4:1.6.3')
    {
        exclude module: 'hamcrest-core'
        exclude module: 'objenesis'
    }
    androidTestCompile ('org.powermock:powermock-api-mockito:1.6.3') {
        exclude module: 'hamcrest-core'
        exclude module: 'objenesis'
    }

    androidTestCompile ('com.android.support.test:runner:0.4.1') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile ('com.android.support.test:rules:0.4.1') {
        exclude module: 'hamcrest-core'
    }

}

I use gradle dependencies and there is no hamcrest-core in androidTestCompile, but still fails at the instrumentation time.

Something important I miss?

Thank you!

alec.tu
  • 1,647
  • 2
  • 20
  • 41
  • http://stackoverflow.com/questions/21102598/android-studio-unexpected-top-level-exception – IntelliJ Amiya Oct 27 '15 at 08:08
  • 1
    I add a `configurations { all *.exclude module: 'hamcrest-core'}` and it still does not work. – alec.tu Oct 27 '15 at 08:17
  • Possible duplicate of [Android Gradle DexException - Multiple dex files defined](http://stackoverflow.com/questions/22702267/android-gradle-dexexception-multiple-dex-files-defined) – blahdiblah Dec 14 '16 at 00:33

1 Answers1

3

After deeply look into the result of gradle dependencies, I found that the root cause is that org.powermock:powermock-api-mockito contains mockito-all module.

However, the mockito-all module contains hamcrest library internally ( but you can not see it with gradle dependencies), which is conflicted with the other dependencies.

So, to fix this, we need to replace org.powermock:powermock-api-mockito with org.powermock:powermock-api-support, which does not contain mockito-all.

Replace

dependencies {
    androidTestCompile ('org.powermock:powermock-api-mockito:1.6.3') {
        exclude module: 'hamcrest-core'
        exclude module: 'objenesis'
    }
}

with

dependencies {
    androidTestCompile 'org.powermock:powermock-api-support:1.6.3'
}

should work for me.

alec.tu
  • 1,647
  • 2
  • 20
  • 41