180

After upgrading to API 22 and support lib revision 22 I'm getting the following warning:

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

Gradle itself is more forgiving, but Android Studio, not so much.

I have no dependencies declared with 21.0.3 ... is one of the dependent libraries using 21.0.3 and Google forgot to update it with the rest of the batch?

My build.gradle with the extras cut out

android {
  compileSdkVersion 22
  buildToolsVersion '22'

  defaultConfig {
    applicationId "com.REDACTED.android"
    minSdkVersion 14
    targetSdkVersion 22
    renderscriptSupportModeEnabled true
    versionName '1.0.0'
    versionCode 100
  }

  buildTypes {
    release {
      minifyEnabled true
      zipAlignEnabled true
      signingConfig signingConfigs.release
    }

    debug {
      minifyEnabled false
      zipAlignEnabled true
      signingConfig signingConfigs.debug
    }
  }

  dependencies {
    provided 'org.projectlombok:lombok:1.16.2'
    googleCompile 'com.google.android.gms:play-services-base:6.5.87'
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:support-v13:22.0.0'
    compile 'com.android.support:cardview-v7:22.0.0'
    compile 'com.android.support:palette-v7:22.0.0'
    compile 'com.android.support:support-annotations:22.0.0'
    compile 'com.github.chrisbanes.photoview:library:1.2.3'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'commons-io:commons-io:2.4'
    compile 'commons-codec:commons-codec:1.10'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'com.jakewharton:disklrucache:2.0.2'
    compile 'com.squareup:otto:1.3.6'
    compile 'com.squareup.picasso:picasso:2.5.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.2.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
    compile 'com.squareup.okio:okio:1.2.0'
    compile 'com.flaviofaria:kenburnsview:1.0.6'
    compile 'com.edmodo:cropper:1.0.1'
    compile 'com.getbase:floatingactionbutton:1.8.0'
    compile 'com.nispok:snackbar:2.10.2'
    compile 'com.github.ksoichiro:android-observablescrollview:1.5.0'
    compile 'in.srain.cube:grid-view-with-header-footer:1.0.9'
    compile 'de.hdodenhof:circleimageview:1.2.2'
    compile fileTree(dir: 'libs', include: '*.jar')
    // Test Only Dependencies
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
  }

Update: (Thanks Mark)

Looks like it's espresso-contrib

+--- com.android.support.test:testing-support-lib:0.1 (*)
\--- com.android.support.test.espresso:espresso-contrib:2.0
     +--- com.android.support:recyclerview-v7:21.0.3
     |    +--- com.android.support:support-annotations:21.0.3
     |    \--- com.android.support:support-v4:21.0.3
     |         \--- com.android.support:support-annotations:21.0.3
     +--- com.android.support:support-v4:21.0.3 (*)
     \--- com.android.support.test.espresso:espresso-core:2.0 (*)
copolii
  • 14,208
  • 10
  • 51
  • 80
  • 5
    The Gradle `dependencies` task may be able to help you identify the culprit: https://gradle.org/docs/current/userguide/tutorial_gradle_command_line.html#para:commandline_dependency_report – CommonsWare Mar 11 '15 at 23:15
  • 1
    Thanks Mark. It looks like `espresso-contrib` is the cause... – copolii Mar 11 '15 at 23:39
  • 2
    You can try an `exclude` to block the 21.0.3 edition of `recyclerview-v7`, pull in the 22.0.0 edition yourself, and pray that they are sufficiently compatible for `espresso-contrib`'s needs. Personally, I'm amazed that you are maintaining your sanity with that long dependencies list... :-) – CommonsWare Mar 11 '15 at 23:40
  • 1
    Yep. This did the trick. If you put that up as the answer I'll give you the beans :) androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') { exclude module: 'support-annotations' } – copolii Mar 11 '15 at 23:46
  • My problem was with assertj. Tossing that out there in case it helps anyone :) – loeschg Apr 16 '15 at 01:33
  • Check [here](https://github.com/googlesamples/android-testing/issues/22) for a solution to this issue. – Toni Aug 13 '15 at 17:25
  • 1
    My same problem was resolved just by simple android studio restart :) – Lakhwinder Singh Dhillon Apr 23 '17 at 17:38
  • Worth try of @LakhwinderSinghDhillon 's comment above to restart studio. It worked for me 2. – Sagar Shah Mar 09 '18 at 12:51

10 Answers10

287

Step #1 when dealing with this sort of thing is to get comfortable with command-line Gradle.

Step #2 is to run the Gradle dependencies report (e.g., gradle -q app:dependencies from the project root). This will provide the ASCII tree as shown in the update to the question, and it should help you identify what is asking for the conflicting artifact versions.

Step #3 is to decide what needs replacing. You elected to replace just the conflict (support-annotations). Personally, I would have gone with the root of the wrong-version tree (recyclerview-v7), though for all I know that might not be the best course of action in this case.

Step #4 is to add the exclude directive to block what you chose in Step #3:

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude module: 'support-annotations'
}

Step #5 is to test the heck out of this change. What you are doing is saying that espresso-contrib has to deal with the 22.0.0 edition of support-annotations. That may work. That may not. It depends on the backwards compatibility of the conflict. In this case, support-annotations should be pretty good about it.

Step #6 is to consume the beverage of your choice, one appropriate for your locale and time of day.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 4
    I used `exclude group: 'com.android.support', module: 'support-annotations' ` since your version raised a "cannot infer argument types" warning from gradle – appoll Apr 20 '15 at 16:43
  • 2
    Works... also consider to apply this to com.android.support.test:runner:0.3, com.android.support.test:rules:0.3 and com.android.support.test.espresso:espresso-core:2.2 – uudashr Jul 31 '15 at 07:48
  • In Step #4, where are you adding that androidTestCompile directive ? If I try to add that syntax to my app's dependencies block I get some syntax errors. As this is written now, it's a bit tough to tell precisely where you add it. – OYRM Jan 25 '16 at 17:59
  • 1
    @OYRM: "where are you adding that androidTestCompile directive ?" -- in `dependencies`. See [this sample](https://github.com/chiuki/espresso-samples/blob/master/idling-resource-elapsed-time/app/build.gradle) from Chiu-Ki Chan. – CommonsWare Jan 26 '16 at 10:03
  • I had been adding to "dependencies" within the Module gradule file, but must have introduced a typo. After triple checking the syntax, the error is cleared and the androidTestCompile method with it's "exclude" argument has worked exactly as you described – OYRM Jan 26 '16 at 16:36
  • Yes, in my case it was `com.android.support.test:runner:0.5` that had a dependency on `com.android.support:support-annotations:23.1.1` which caused this error. – IgorGanapolsky Mar 22 '16 at 16:44
  • 16
    Step #6 is key here – Odaym Apr 11 '16 at 08:15
  • @CommonsWare My issue was Resolved versions for app (25.3.0) and test app (23.1.1) differ.I didn't use any annotations. how to solve this. – Prasad Mar 21 '17 at 12:27
  • @Prasad: Follow the instructions in my answer, particularly Step #2. – CommonsWare Mar 21 '17 at 12:35
  • @CommonsWare Where should I type the command line. in the terminal. I tried it displays "gradle is not internal or external command" – Prasad Mar 21 '17 at 12:40
  • @Prasad: In your case, try `gradlew` in your project root directory, instead of `gradle`. – CommonsWare Mar 21 '17 at 12:40
  • stupid question: why can't just each library use their own version of a dependency? – User Jun 16 '17 at 11:32
  • @lxx: because neither Java nor Android resources work that way. In Java, you cannot have N classes with an identical fully-qualified name (e.g., `android.support.v4.app.Fragment`). In Android, resources are all combined into a single namespace, merging in all the libraries, plus the app's own resources. – CommonsWare Jun 16 '17 at 11:36
146

What I did to solve it was add the below line in to my build.gradle script

androidTestCompile 'com.android.support:support-annotations:xx.x.x'

Replace xx.x.x with whatever version of support-annotations your app is using - this will be shown in your dependencies, or the Gradle sync message as: Resolved version for app (xx.x.x) should there be a problem syncing.

Mark
  • 9,604
  • 5
  • 36
  • 64
Renan Nery
  • 3,475
  • 3
  • 19
  • 23
  • Wow. I do this for upgrading `Junit` but totally for got I can do it for other dependencies as well. – Jared Burrows May 07 '15 at 22:33
  • 10
    This is indeed how the Google samples [do it](https://github.com/googlesamples/android-testing/blob/ba14ef9e925fa17621bf86abe5336dcb9d53e466/runner/AndroidJunitRunnerSample/app/build.gradle#L36), a comment reading "Force usage of support annotations in the test app, since it is internally used by the runner module.". – desseim Oct 21 '15 at 13:28
  • This was the best answer for me. Note: Must update to '23.1.1': `androidTestCompile 'com.android.support:support-annotations:23.1.1'` – David Manpearl Feb 12 '16 at 02:38
  • Thanks, in my case the line was already there, but explicitly stated a conflicting version, updated to required one and everything worked like a charm. – RAM237 Apr 05 '17 at 12:13
  • how to add this to the build.gradle? – ubuntu_noob May 01 '18 at 10:37
58

Actually it's a bug of new update version of Espresso Contrib, you can refer this workaround : android-testing/build.gradle

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}
bony
  • 611
  • 4
  • 7
23

Please refer https://github.com/JakeWharton/u2020/blob/05a57bf43b9b61f16d32cbe8717af77cd608b0fb/build.gradle#L136-L140

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

This solved my issue.

Alternatively you can run gradlew on windows and ./gradlew for mac/linux this will download your dependency when needed

maruti060385
  • 707
  • 8
  • 11
8

I also ran into this problem as well as other conflicts involving appcompat and the solution I found was adding test compiles and setting them to the sdk you are currently using. In my case it's 25, so it looks like this:

androidTestCompile 'com.android.support:support-annotations:25.+'
androidTestCompile 'com.android.support:appcompat-v7:25.+'
androidTestCompile 'com.android.support:support-v4:25.+'
androidTestCompile 'com.android.support:recyclerview-v7:25.+'
androidTestCompile 'com.android.support:design:25.+'

Furthermore, as you can see I added the design dependency, which is related to Android Material Design.

Hope it helps =)

gmartinsnull
  • 1,075
  • 1
  • 12
  • 11
8

for sdkversion 25

     androidTestCompile 'com.android.support:support-annotations:25.2.0'
dc10
  • 2,160
  • 6
  • 29
  • 46
3

you can run gradlew on windows and ./gradlew for mac/linux this will download your dependency when needed.

You can check if one of the library has dependency on the support annotation or any library with is named in error and try to excluded it in gradle something like below

compile("org.apache.maven:maven-ant-tasks:${mavenAntTaskVer}‌​") { exclude group: 'junit' }

Error:Conflict with dependency 'junit:junit'. Resolved versions for app (3.8.1) and test app (4.12) differ. See g.co/androidstudio/app-test-app-conflict for details.

this was the error i was getting so I used above gradle line to fix the issue

maruti060385
  • 707
  • 8
  • 11
  • This seems a really good way to solve problem like this. And in addition, try to run this _**./gradlew :app:dependencies**_ command line under your project root directory, it's will make a tree about your project's dependencies, and have a check carefully, then you'll find where the conflict from. – frank jorsn Apr 11 '17 at 05:51
2

Simply delete these lines in your build.gradle file:

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Pang
  • 9,564
  • 146
  • 81
  • 122
0

This is common issue in instrumentation test as stated here and can be resolved simply by adding the dependencies for androidTestCompile inside the build.gradle. In my case conflict arised in appcompat, recyclerview and design dependencies. And resolved by adding following lines

    androidTestCompile 'com.android.support:appcompat-v7:23.4.0'
    androidTestCompile 'com.android.support:recyclerview-v7:23.4.0'
    androidTestCompile 'com.android.support:design:23.4.0'
cammando
  • 596
  • 7
  • 20
0

I was having the same problem with 26.0.0 and 27.1.1 and actually just upgrading the former to the later make it work.

Ed_
  • 973
  • 11
  • 25