119

When building I get the following error:

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

These are my gradle dependencies

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'
    compile 'com.android.support:cardview-v7:23.1.0'
    compile 'com.android.support:recyclerview-v7:23.1.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup:otto:1.3.8'
    compile 'com.snappydb:snappydb-lib:0.5.2'
    compile 'com.esotericsoftware.kryo:kryo:2.24.0'
    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'io.reactivex:rxjava:1.0.14'
    compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'org.apache.commons:commons-lang3:3.4'
    testCompile 'junit:junit:4.12'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile 'org.mockito:mockito-core:1.10.19'
    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 'com.android.support.test.espresso:espresso-web:2.2.1'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}

How can I fix this?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
barq
  • 3,681
  • 4
  • 26
  • 39
  • I can tell you that the 23.1.0 dependency comes from the appcompat lib, because that includes the annotations lib. The 23.0.1 I don't know – Tim Oct 24 '15 at 11:22
  • It doesn't work either if I switch appcompat back to 23.0.1 – barq Oct 24 '15 at 11:28
  • I just Replaced 23.1.0 by 23.0.1, everywhere in dependency, worked for me. – Shahzad Afridi Apr 25 '18 at 09:11
  • Add annotation dependency related to your version error. https://readyandroid.wordpress.com/errorexecution-failed-for-task-apppredebugandroidtestbuild-conflict-with-dependency-com-android-supportsupport-annotations-android/ – Ready Android Aug 09 '18 at 10:18
  • Or might this help you https://readyandroid.wordpress.com/conflict-with-dependency-com-android-supportsupport-annotations-resolved-versions-for-app-23-1-0-and-test-app-23-0-1-differ/ – Ready Android Aug 09 '18 at 10:37

9 Answers9

208

You can force the annotation library in your test using:

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

Something like this:

  // Force usage of support annotations in the test app, since it is internally used by the runner module.
  androidTestCompile 'com.android.support:support-annotations:23.1.0'
  androidTestCompile 'com.android.support.test:runner:0.4.1'
  androidTestCompile 'com.android.support.test:rules:0.4.1'
  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
  androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
  androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'

Another solution is to use this in the top level file:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 8
    This line was the solution:androidTestCompile 'com.android.support:support-annotations:23.1.0' – barq Oct 30 '15 at 09:50
  • 4
    The use of the configurations.all setting worked for me, but not in the project level file, which is what I interpreted initially as "top level file" from the response above. It was in the module level build.gradle file – OYRM Dec 18 '15 at 21:09
  • Is this conflict caused by Espresso originally? – IgorGanapolsky Mar 15 '16 at 01:48
  • resolutionStrategy.force 'com.android.support:support-annotations:23.4.0' doesn't help for me.. problem is EspressoContribution for DatePicker, DrawerActions, RecyclerView, etc.. ('com.android.support.test.espresso:espresso-contrib:2.2.2'){ exclude module: 'support-annotations' exclude module: 'support-v4' } – Ewoks May 12 '16 at 07:38
  • 3
    Just one important point, to make it clear that We need to add configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:23.1.0' } inside `build.gradle` of the **module(app)** to fix the issue. – AADProgramming Jul 05 '16 at 10:18
  • I just added this line " androidTestCompile 'com.android.support:support-annotations:23.1.0' " to build.gradle and error was disappeared, Thanks. – QMaster Jul 29 '18 at 13:55
  • Yeah,It solved my problem but after adding that line,I had done clean and rebuild the project. – Mohammed Nishar Aug 09 '18 at 06:33
  • I had to remove this one also. androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') { – DAS Aug 14 '18 at 08:20
69

Project Rebuild solved my problem.

In Android studio in the toolbar.. Build>Rebuild Project.

Sushanth
  • 1,373
  • 1
  • 10
  • 15
25

Source: CodePath - UI Testing With Espresso

  1. Finally, we need to pull in the Espresso dependencies and set the test runner in our app build.gradle:
// build.gradle
...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    ...
    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:runner:0.5') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
}

I've added that to my gradle file and the warning disappeared.

Also, if you get any other dependency listed as conflicting, such as support-annotations, try excluding it too from the androidTestCompile dependencies.

rexxar
  • 1,671
  • 1
  • 21
  • 27
13

you can try to use

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

instead of

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

androidTestCompile 'com.android.support.test:rules:0.4.1'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
jingsong yang
  • 131
  • 1
  • 2
6

I was getting this error

Error:Execution failed for task ':app:preDebugAndroidTestBuild'. Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

I was having following dependencies in my build.gradle file under Gradle Scripts

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-vector-drawable:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

So, I resolved it by commenting the following dependencies

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

So my dependencies look like this

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-vector-drawable:26.1.0'
//testImplementation 'junit:junit:4.12'
//androidTestImplementation 'com.android.support.test:runner:1.0.2'
//androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Hope it helps!

Shahbaz Ali
  • 1,262
  • 1
  • 12
  • 13
  • 1
    So you removed the testing libraries? This isn't very helpful if you need those libraries. – Pants Jul 08 '18 at 16:52
4

I was getting the same error today:

Error:Execution failed for task ':app:preDebugAndroidTestBuild'.> Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.1.0) and test app (27.1.1) differ.

What I did:

  • I simply updated all my dependencies to 27.1.1 instead of 26.1.0
  • Also, updated my compileSdkVersion 27 and targetSdkVersion 27 which were 26 earlier

And com.android.support:support-annotations error was gone!

For Ref:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:design:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
1

In my case, I added below code in dependencies of app level build.gradle

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

After that, I clean the project and rebuild.My problem solved.

Mohammed Nishar
  • 171
  • 3
  • 12
0

Chang your application level build.gradle file's:

implementation 'com.android.support:appcompat-v7:23.1.0'

to

 implementation 'com.android.support:appcompat-v7:23.0.1'
4b0
  • 21,981
  • 30
  • 95
  • 142
chetu
  • 13
  • 1
  • 5
0

Try this :

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.yourpackagename"
    minSdkVersion 15
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Hanisha
  • 849
  • 10
  • 8