Android gradle unit tests migration from jars – copied hamcrest files from JUnit and mockito
I eventually migrated the old unit tests run using jar files to gradle, but I had a lot of trouble getting the right combination and wasn't sure what I was doing was right or wrong. First, these are all the jar files I'm using
dexmaker-1.0.jar
dexmaker-mockito-1.0.jar
fest-android-1.0.7.jar
fest-assert-core-2.0M10.jar
fest-util-1.2.5.jar
junit-4.11.jar
mockito-all-1.9.5.jar
the-missing-android-xml-junit-test-runner-release-1.3_2.jar <---- I think this is used to get reports for the unit tests, is there a way that I don't have to use this anymore?
When I import all these jars, like the following jars, it works normally, so this is a good thing, but not ideal:
androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
Next, I try to change all jar files to gradle Maven dependencies, as shown below
androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
androidTestCompile 'junit:junit:4.11+'
androidTestCompile ('com.squareup:fest-android:1.0.+') {
exclude group: 'com.android.support'
}
androidTestCompile 'org.easytesting:fest-assert-core:2.0M10'
androidTestCompile 'org.easytesting:fest-util:1.2.+'
androidTestCompile 'org.mockito:mockito-all:1.9.+'
androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
This doesn't work because when I try to build and run unit tests, I get the following results:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
...
Invalid Sign
androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
androidTestCompile('junit:junit:4.11+') {
exclude group: 'org.hamcrest'
}
androidTestCompile ('com.squareup:fest-android:1.0.+') {
exclude group: 'com.android.support'
}
androidTestCompile 'org.easytesting:fest-assert-core:2.0M10'
androidTestCompile 'org.easytesting:fest-util:1.2.+'
androidTestCompile 'org.mockito:mockito-all:1.9.+'
androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
and
androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
androidTestCompile 'junit:junit:4.11+'
androidTestCompile ('com.squareup:fest-android:1.0.+') {
exclude group: 'com.android.support'
}
androidTestCompile 'org.easytesting:fest-assert-core:2.0M10'
androidTestCompile 'org.easytesting:fest-util:1.2.+'
androidTestCompile('org.mockito:mockito-all:1.9.5') {
exclude group: 'org.hamcrest'
}
androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
However, the following results are still produced:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
I tried the following changes to JUnit and mockito based on some suggestions
androidTestCompile 'junit:junit-dep:4.11+'
androidTestCompile 'org.mockito:mockito-core:1.9.+'
I got it
Error: duplicate files during packaging of APK /.../app/build/outputs/apk/app-debug-test-unaligned.apk Path in archive: LICENSE.txt Origin 1: /.../.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar Origin 2: /.../.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
1f71acb49bf010a0/hamcrest-core-1.3.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'LICENSE.txt'
}
}
Failed
FAILURE: Build Failed with an exception.
Execution Failed for task ':app:packageDebugTest'.
resolvent:
In fact, I finally knew it just by adding it and it worked
android { // stuff before packagingOptions { exclude 'Meta-INF/LICENSE.txt' exclude 'LICENSE.txt' } }
My dependencies are as follows
dependencies { androidTestCompile ('com.squareup:fest-android:1.0.+') { exclude group: 'com.android.support' } androidTestCompile 'com.google.dexmaker:dexmaker:1.+' androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+' androidTestCompile 'junit:junit:4.11' androidTestCompile 'org.hamcrest:hamcrest-library:1.3' androidTestCompile 'org.mockito:mockito-core:1.9.5' }