20

I'm trying to add Espresso 2 to my project (which also has lots of other dependencies), but I'm hitting this error when trying to run tests:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    ...

The non-test build works fine.

Does anybody have any tips on the best way to debug this?

I've tried running ./gradlew -q :<my_project>:dependencies, but I can't see any obvious problems (though I don't know much about interpreting its output):

<snip>

androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.squareup.spoon:spoon-client:1.1.2
+--- com.google.dexmaker:dexmaker-mockito:1.0
|    +--- com.google.dexmaker:dexmaker:1.0
|    \--- org.mockito:mockito-core:1.9.5
|         +--- org.hamcrest:hamcrest-core:1.1
|         \--- org.objenesis:objenesis:1.0
+--- com.google.dexmaker:dexmaker:1.0
+--- org.mockito:mockito-core:1.9.5 (*)
+--- com.jayway.android.robotium:robotium-solo:5.3.1
+--- com.android.support.test.espresso:espresso-core:2.0
|    +--- com.squareup:javawriter:2.1.1
|    +--- org.hamcrest:hamcrest-integration:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- org.hamcrest:hamcrest-library:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- javax.inject:javax.inject:1
|    +--- com.android.support.test.espresso:espresso-idling-resource:2.0
|    +--- com.android.support.test:testing-support-lib:0.1
|    |    \--- junit:junit-dep:4.10
|    |         \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.google.code.findbugs:jsr305:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.hamcrest:hamcrest-core:1.1
\--- com.android.support.test:runner:0.2
     +--- junit:junit-dep:4.10 (*)
     +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
     \--- com.android.support:support-annotations:22.0.0

<snip>

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.1.0
|    \--- com.android.support:support-v4:22.1.0
|         \--- com.android.support:support-annotations:22.1.0
+--- com.android.support:support-v4:22.1.0 (*)
+--- com.afollestad:material-dialogs:0.7.2.4
|    +--- com.android.support:support-v4:22.0.0 -> 22.1.0 (*)
|    +--- com.android.support:appcompat-v7:22.0.0 -> 22.1.0 (*)
|    +--- com.android.support:support-annotations:22.0.0 -> 22.1.0
|    \--- com.android.support:recyclerview-v7:22.0.0
|         +--- com.android.support:support-v4:22.0.0 -> 22.1.0 (*)
|         \--- com.android.support:support-annotations:22.0.0 -> 22.1.0
+--- com.google.android.gms:play-services-base:7.0.0
|    \--- com.android.support:support-v4:22.0.0 -> 22.1.0 (*)
+--- com.google.android.gms:play-services-gcm:7.0.0
|    \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- de.greenrobot:greendao:1.3.7
+--- de.greenrobot:eventbus:2.2.0
+--- com.squareup.wire:wire-runtime:1.4.0
|    \--- com.squareup.okio:okio:0.6.0 -> 1.0.1
+--- com.squareup.okio:okio:1.0.1
+--- com.squareup.okhttp:okhttp-urlconnection:2.0.0
|    \--- com.squareup.okhttp:okhttp:2.0.0
|         \--- com.squareup.okio:okio:1.0.0 -> 1.0.1
+--- com.squareup.retrofit:retrofit:1.6.0
|    \--- com.google.code.gson:gson:2.2.4
+--- com.squareup.retrofit:converter-wire:1.6.0
|    +--- com.squareup.retrofit:retrofit:1.6.0 (*)
|    \--- com.squareup.wire:wire-runtime:1.2.0 -> 1.4.0 (*)
+--- com.squareup.okhttp:okhttp:2.0.0 (*)
+--- com.squareup.picasso:picasso:2.4.0
+--- com.path:android-priority-jobqueue:1.1.2
|    \--- com.google.android:android:2.2.1
|         +--- commons-logging:commons-logging:1.1.1
|         +--- org.apache.httpcomponents:httpclient:4.0.1
|         |    +--- org.apache.httpcomponents:httpcore:4.0.1
|         |    +--- commons-logging:commons-logging:1.1.1
|         |    \--- commons-codec:commons-codec:1.3
|         +--- org.khronos:opengl-api:gl1.1-android-2.1_r1
|         +--- xerces:xmlParserAPIs:2.6.2
|         +--- xpp3:xpp3:1.1.4c
|         \--- org.json:json:20080701
+--- com.netflix.rxjava:rxjava-android:0.16.1
|    \--- com.netflix.rxjava:rxjava-core:0.16.1
+--- com.nineoldandroids:library:2.4.0
+--- it.sephiroth.android.library.horizontallistview:hlistview:1.2.2
|    \--- com.android.support:support-v4:19.1.+ -> 22.1.0 (*)
+--- com.joooonho:selectableroundedimageview:1.0.1
+--- com.crashlytics.sdk.android:crashlytics:2.0.1
|    +--- com.crashlytics.sdk.android:answers:1.0.1
|    |    \--- io.fabric.sdk.android:fabric:1.0.1
|    +--- io.fabric.sdk.android:fabric:1.0.1
|    \--- com.crashlytics.sdk.android:beta:1.0.1
|         \--- io.fabric.sdk.android:fabric:1.0.1
+--- com.mixpanel.android:mixpanel-android:4.5.3
\--- com.facebook.android:facebook-android-sdk:4.0.0
     +--- com.android.support:support-v4:[21,22) -> 22.1.0 (*)
     \--- com.parse.bolts:bolts-android:1.1.4

<snip>

I've also tried searching subfolders of my build/intermediates directory, but I couldn't see any smoking guns (though I'm not sure I was searching all the external dependency JARs in the right way).

Here is the dependency section of my build.gradle:

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.afollestad:material-dialogs:0.7.2.4'
    compile 'com.google.android.gms:play-services-base:7.0.0'
    compile 'com.google.android.gms:play-services-gcm:7.0.0'
    compile 'de.greenrobot:greendao:1.3.7'
    compile 'de.greenrobot:eventbus:2.2.0'
    compile 'com.squareup.wire:wire-runtime:1.4.0'
    compile 'com.squareup.okio:okio:1.0.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.retrofit:retrofit:1.6.0'
    compile 'com.squareup.retrofit:converter-wire:1.6.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile 'com.path:android-priority-jobqueue:1.1.2'
    compile 'com.netflix.rxjava:rxjava-android:0.16.1'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
    compile 'com.joooonho:selectableroundedimageview:1.0.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.0.1@aar') {
        transitive = true;
    }
    compile "com.mixpanel.android:mixpanel-android:4.5.3"
    compile "com.facebook.android:facebook-android-sdk:4.0.0"

    // For the Rollbar JAR, as it is not available in Maven central
    compile fileTree(dir: 'libs', include: '*.jar')

    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.2'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.3.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:runner:0.2'
}

Update: when I update from com.android.tools.build:gradle:1.0.0 to com.android.tools.build:gradle:1.1.1, I get this warning:

Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (22.1.0) and test app (22.0.0) differ.
Dan J
  • 25,433
  • 17
  • 100
  • 173
  • Post your `build.gradle.` – Jared Burrows Apr 24 '15 at 21:22
  • Do you remember the last dependency you added before the error? You have so many. – Jared Burrows Apr 24 '15 at 21:25
  • Also we do not know what is is `compile fileTree(dir: 'libs', include: '*.jar')`, `compile project(':CookbriteJava')` and `compile project(':camera')`. – Jared Burrows Apr 24 '15 at 21:27
  • I thought the output from the Gradle `dependencies` task showed everything you would need? I'll update the question with my build.gradle too. – Dan J Apr 24 '15 at 21:28
  • I most recently added the last two in the list for Espresso, and to get past a very similar error I had to update my Support Library and Google Play Services dependencies, and remove AssertJ. – Dan J Apr 24 '15 at 21:28
  • `gradlew dependencies` tasks shows the `build.gradle` depdencies, it doesn't know about the other projects `*.jars`. – Jared Burrows Apr 24 '15 at 21:28
  • Does this error have a message along the lines of "jar included twice in LICENSE.txt"? – Emmanuel Apr 24 '15 at 21:35
  • No, it does not - it's specifically complaining about a duplicate BuildConfig class. – Dan J Apr 24 '15 at 21:37
  • @DanJ Are you predexing? Can please post your entire `build.gradle`? – Jared Burrows Apr 24 '15 at 21:38
  • I am predexing - I don't want to post my entire `build.gradle`, but I'll post some other updates... – Dan J Apr 24 '15 at 21:47
  • Do you get any other information other than that exception? If there is anything else relevant in your build output you should add it. Also, this won't fix your problem, but it is still a good idea, but you should get rid of explicitly adding dexmaker and mockito-core, because they are already being added transitively by dexmaker-mockito (you can see it in your gradlew -q dependencies output). I don't see any duplicates in your compile scope but there are a lot of dependencies there so I can't fully tell. – Daniel A. González Apr 24 '15 at 21:51
  • @DanJ That's what I thought. Try `gradlew clean` and then `gradlew assembleDebug`. I believe your issue is the fact that you are building a `build.config` that is already `pre-dexed` and then adding more to your `dex` later one. Does this happen often? When is the last time your cleaned? – Jared Burrows Apr 24 '15 at 21:52
  • I've been doing Clean builds a lot, and a colleague has also tried building this branch too (with the same failure result). I'll try Daniel's suggestion now... – Dan J Apr 24 '15 at 21:55
  • I am telling you to build without predexing. – Jared Burrows Apr 24 '15 at 21:56
  • My build is already very slow, so turning off predexing is not something I want to do (though I will try it just to see if this flushes out any other issues). – Dan J Apr 24 '15 at 22:00
  • @DanJ Thank you. I want to learn from this weird error myself as it is hard to duplicated on my end. – Jared Burrows Apr 24 '15 at 22:01
  • @DanJ Any luck with my suggestions? – Jared Burrows Apr 24 '15 at 22:37

7 Answers7

46

Update (9/07/2015):

You can continue to work with 22.2.1 if you use the following excludes:

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test:runner:0.3') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test:rules:0.3') {
    exclude group: 'com.android.support', module: 'support-annotations'
}

If you depend on espresso-contrib, you need the exclude as well.

Update (8/03/2015):

With support library 22.2.1, the dependencies are broken again; please don't upgrade to 22.2.1 until a new runner is released.

Update (6/04/2015):

With the latest release of runner 0.3 and rules 0.3, this answer is no longer needed. You can simply use

androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'

with latest support libraries. (22.2.0 as of this writing)

Update (5/30/2015):

compile 'com.android.support:appcompat-v7:22.2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
// com.android.support.test:testing-support-lib:0.1 // <-- causes issue

Update (4/24/2015):

The problem is that com.android.support:support-v4:22.1.1 is clashing with com.android.support.test:runner:0.2 (as that depends on com.android.support:support-v4:22.0.0).

com.android.support.test.espresso:espresso-core:2.1 has a dependency on com.android.support.test:runner:0.2, so it also causes the same error.

So, this combination will work:

compile 'com.android.support:support-v4:22.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'

...and so will this one (without 'com.android.support.test:runner:0.2'):

compile 'com.android.support:support-v4:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

Original Answer:

Contrary to what the Espresso documentation says, you should remove this dependency:

androidTestCompile 'com.android.support.test:runner:0.2'

As it is the cause for library version conflict.

You should also update to Android gradle plugin 1.1.1, as that version will tell you the exact version conflict, which is useful in this case.

Thomas Keller
  • 5,933
  • 6
  • 48
  • 80
Yenchi
  • 2,076
  • 24
  • 30
  • Why `espresso-core` vs `espresso-contrib`? – IgorGanapolsky Jun 15 '15 at 04:42
  • 1
    @IgorGanapolsky espresso-contrib 2.1 used to contain test runner 0.2 jar file which also had compatibility issue with support library 22.1. As to a more general discussion on when should one use espresso-contrib, please check if you need to use any of the classes listed in http://developer.android.com/reference/android/support/test/espresso/contrib/package-summary.html – Yenchi Jun 15 '15 at 05:19
  • 3
    removing `com.android.support.test:testing-support-lib:0.1` dependency solved my issue. thanks! – Veaceslav Gaidarji Jun 17 '15 at 14:47
  • Any news about this? `compile 'com.android.support:support-annotations:22.2.1'` is broken, too :-/ – Ivan Morgillo Aug 11 '15 at 10:26
  • I'm still getting this when using `espresso-contrib:2.0`. How to fix this? – John Ernest Guadalupe Dec 15 '15 at 14:01
  • `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.TaskStackBuilder android.support.v4.app.TaskStackBuilder.addParentStack(android.app.Activity)' on a null object reference` Help :( – John Ernest Guadalupe Dec 15 '15 at 14:11
6

One other useful tip is how to force dependency resolution to a specific version.

Here is one way:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.0.0'
}

...and here is another:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'com.android.support') {
            details.useVersion '22.0.0'
        }
    }
}

Using either of these with com.android.support.test.espresso:espresso-core:2.1 should work.

See the Forcing consistent version for a group of libraries section in the Gradle documentation for more information.

Dan J
  • 25,433
  • 17
  • 100
  • 173
4

I recently ran into this error after we enabled incremental gradle builds.

dexOptions {
    javaMaxHeapSize "2g"
    incremental true
}

This was resolved by disabling predexing libraries.

dexOptions {
    javaMaxHeapSize "2g"
    incremental true
    preDexLibraries = false
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
James P
  • 41
  • 1
  • What is the downside of using ```preDexLibraries = false```? This works for me, but am I going to run into performance issues then? – Sam Edwards Dec 09 '15 at 15:13
3

My solution:

compile 'com.android.support:appcompat-v7:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support:support-annotations:22.1.0'
androidTestCompile 'com.android.support.test:runner:0.2'

+

android {
    packagingOptions {
        exclude 'LICENSE.txt'
   }
}
Max Kammerer
  • 116
  • 5
1

I got this error trying to set up Espresso as well. Try using espresso-contrib:2.1, not 2.0

c2knaps
  • 1,817
  • 17
  • 23
0

Try excluding the following from espresso (one at a time):

androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
  exclude group: 'javax.inject'
  exclude group: 'com.google.code.findbugs'
  exclude group: 'com.android.support', module: 'support-annotations'
}

Probably need to do the same with runner

Eric
  • 5,323
  • 6
  • 30
  • 35
0

According to this bug report, you can also use resolutionStrategy:

allprojects {
    repositories {
        mavenCentral()
        maven {
            url 'http://download.crashlytics.com/maven'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:22.2.0'
    }
}

This is the solution that worked for me and allowed me to use the most recent version of appcompat-v7 and appcompat-v4.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268