Android espresso-contrib gradle build failing

Issue

I’m trying to learn android espresso.. I followed some basic tutorials and it was working fine. But now I want to do some tests on the android navigation drawer. For that I need to use gradle dependency androidTestCompile ‘com.android.support.test.espresso:espresso-contrib:2.2.2’ but it’s causing conflict with other dependencies. My gradle file :

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "my.com.myapp_android"
    minSdkVersion 18
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}
repositories {
jcenter()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//material design
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'

//zxing
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'

//Testing
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'




//inMarketSDK
//compile group: 'com.inmarket', name: 'm2msdk', version: '2.29', ext: 'aar'

}

Error is something like this:

Error:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

followed this : link for espresso install

I also tried to exclude annotation dependency :

 androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2')  {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2')
        {
            // Necessary if your app targets Marshmallow (since Espresso
            // hasn't moved to Marshmallow yet)
            exclude group: 'com.android.support', module: 'support-annotations'
        } 

Solution

TL;DR;

New version of espresso-contrib 2.2.2 library has now dependency on com.android.support:appcompat-v7:23.1.1 resulting into conflict when using different version of appcompat-v7 in our compile time dependency like below:

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:23.4.0'

     androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
}

To avoid conflict when we exclude appcompat-v7 dependency from espresso-contrib like below it breaks again due to some value dependencies on design support lib.

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'appcompat-v7'
}

Error:

Error:(69) Error retrieving parent for item: No resource found that matches the given name ‘TextAppearance.AppCompat.Display1’.

Root cause:

This is because the design support lib has dependency on appcompat-v7.
So,when we exclude ‘appcompat-v7’ module from espresso-contrib dependencies(like above) , the design support lib downloaded as part of transitive dependency of espresso-contrib lib couldn’t find the compatible version of appcompat-v7 lib(23.1.1) it is using internally in its resources files and thus gives out the above error.

So, the solution to above problem is to exclude ‘design-support’ lib dependency from espresso-contrib like below:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
    exclude module: 'design'
}

That solves the conflict problem!

LONGER VERSION (in case someone is interested):

To found out the reasons of various conflict issues we face when using `espresso-contrib’ library i have created sample app to find out the root cause.

Step 1:Using Espresso-Contrib Lib version 2.2.1

Created App to use ‘espresso-contrib’ lib version 2.2.1 by adding following lines in app/build.gradle file:

dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'

     androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'

}

Note: In this case,I am not importing any other support library components like
appcompat-v7,recyclerview-v7,etc.

The dependency graph for the above setup looks like below:
enter image description here

As can be seen that espresso-contrib 2.2.1lib has transitive dependencies on version 23.0.1 of
support-v4,recyclerview-v7,support-annotations ,etc.

As i am not defining dependencies for recyclerview-v7,support-annotations in my project the above setup would work just fine.

But when we define those as compile dependencies [like below] in our project we get version conflict issues as stated in your question.

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'

To avoid those conflicts we add below line to our espresso-contrib lib:

    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
}

This makes sure those dependencies aren’t downloaded as part of espresso-contrib transitive dependencies.
Everything runs fine with above setup.No issues!

Step 2: Using Espresso-Contrib lib version 2.2.2

Changed App’s build.gradle to use ‘espresso-contrib’ lib version 2.2.2 by changing the previous build.gradle file:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
testCompile 'junit:junit:4.12'

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
    exclude module: 'support-annotations'
    exclude module: 'support-v4'
    exclude module: 'support-v13'
    exclude module: 'recyclerview-v7'
  }
}

But when i build project using above setup..build fails with error posted in question..

Error:

Error:Conflict with dependency ‘com.android.support:appcompat-v7’. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

So, looking at error i added one more line to above build.gradle:

exclude module: 'appcompat-v7' (inside androidTestCompile block of espresso-contrib)

But that doesn’t resolves the conflict issue and i get value dependencies error posted in comments.
So i check for dependency graph of my app again: enter image description here

As can be seen now that espresso-contrib 2.2.2 lib has now transitive dependency on com.android.support:design:23.1.1 causing the above conflict.

So, we need to add below line inside androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2') block:

exclude module: 'design'

This resolves the conflict issue in lib version 2.2.2!

Answered By – PunitD

Leave a Comment