19

I'm trying to integrate espresso into my application for ui testing. Here are my dependencies in Gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.1'
    compile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.android.support:cardview-v7:21.+'
    compile 'com.android.support:recyclerview-v7:21.+'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
    compile 'com.android.support:support-annotations:22.2.0'
    androidTestCompile 'com.android.support.test:runner:0.3'
    compile project(':common')
    compile project(':service')
}

So all my espresso dependencies are included. However when I try to build I get this error:

Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (22.2.1) and test app (22.2.0) differ.

Has anyone encountered this? I've found it reported here however there's no resolution. Does anyone have a fix for this?

Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • 1
    The solution is to use 'configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:22.1.0' }' – Gabriele Mariotti Aug 26 '15 at 12:54
  • 1
    Hey, you have to remove android.support dependency from each espresso dependency. check: http://stackoverflow.com/questions/29857695/android-tests-build-error-multiple-dex-files-define-landroid-support-test-build – Franklin Hirata Sep 08 '15 at 01:19

8 Answers8

35

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'.

So, the solution to above problem is to exclude 'design-support' lib depedency 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!

For more detailed version of the answer you could check my other answer

Community
  • 1
  • 1
PunitD
  • 2,293
  • 1
  • 20
  • 29
10

So after a lot of digging around, I found I needed to change the dependency for the support annotations.

So I needed to change compile 'com.android.support:support-annotations:22.2.0' to androidTestCompile 'com.android.support:support-annotations:22.+'

DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • 1
    i would suggest rather to use the approach i wrote in my reply http://stackoverflow.com/questions/31807182/android-espresso-issue-dependency-conflict/41160902#41160902 since using the "+" for versioning is discouraged – Noya Dec 15 '16 at 09:42
8

Latest versions of androidTest dependencies depend on appropriate version of support-annotations lib. In my case it is:

androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile 'org.mockito:mockito-core:2.0.31-beta'

Also, as a workaround you can add the next code in your build.gradle, android{} section:

configurations.all {
   resolutionStrategy {
       force 'com.android.support:support-annotations:23.0.1'
   }
}
Irshad
  • 3,071
  • 5
  • 30
  • 51
Nikita Barishok
  • 1,312
  • 1
  • 12
  • 15
  • Adding configurations.all { .. } worked perfectly. Thanks – kevinkl3 Oct 30 '15 at 04:45
  • I know that this answer will solve the problem. But every support-annotations version may rely on diferent methods, So be very carefully because the libs that depends on support-annotation may act strange. – Renato Probst Jul 11 '16 at 15:54
7

In the Jake Wharton U2020 application it is solved in the next way

Add to you gradle.build file

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:23.0.1'
    }
}
shizhen
  • 12,251
  • 9
  • 52
  • 88
Eugene Popovich
  • 3,343
  • 2
  • 30
  • 33
4

I had to combine the following versions for L release after receiving a similar dependency conflict between project and test app:

android {
    useLibrary 'org.apache.http.legacy'
    compileSdkVersion 23
    buildToolsVersion '23.0.1'
    defaultConfig {    
        minSdkVersion 14
        targetSdkVersion 23
    }
}
dependencies {
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:support-v4:23.0.1'

    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}

useLibrary was needed since we use org.apache.http imports, see https://github.com/bitstadium/HockeySDK-Android/issues/80

hcpl
  • 17,382
  • 7
  • 72
  • 73
1

The problem is in this file: android-sdk\extras\android\m2repository\com\android\support\test\runner\0.3\runner-0.3.pom

here:

<dependency>
      <groupId>com.android.support</groupId>
      <artifactId>support-annotations</artifactId>
      <version>22.2.0</version>
      <scope>compile</scope>
</dependency>

if you set 22.2.1 instead 22.2.0 it will work

Valentino
  • 11
  • 1
1

As stated in the google documentation: https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/user-guide#TOC-Resolving-conflicts-between-main-and-test-APK

The way to resolve is to explicitly set the support library in androidTestCompile to the version you are using in the project.

if for example you are using support library version 25.0.1 just add

androidTestCompile 'com.android.support:support-annotations:25.0.1'

in your build.gradle configuration

shizhen
  • 12,251
  • 9
  • 52
  • 88
Noya
  • 3,879
  • 3
  • 26
  • 32
0

just change compile com.android.support:support-annotations:22.2.0 to 23.0.1 if want to use 2.2.1 version

shizhen
  • 12,251
  • 9
  • 52
  • 88
smallfatter
  • 171
  • 1
  • 1
  • 7