Android support library error after updating to 23

2019-01-07 09:59发布

问题:

I have been using android support v4 23.1.1 and recently tried to update it to 23.3.0 ( the latest one when this was asked) but I got the following error:

Error:Conflict with dependency 'com.android.support:support-annotations'.
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 far I have found this https://code.google.com/p/android/issues/detail?id=206137

I went to both the links but I could not fix my issue, how do I fix this issue?

Edit:

I have these in my dependencies

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

Previously all the versions were 23.1.1 and it worked fine the error occurred after updating

Edit:

Gradle Version 2.10
Gradle Plugin Version 2.0.0
buildToolsVersion "23.0.3"

回答1:

For those people who are still facing this problem just add this line to your dependencies.

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

It solved my problem.

UPDATE:

If you have this error nowadays, you can just insert the new versioncode (23.3.0 in this case, or 27.1.1 in May '18) as it is described in the error into the above described solution.



回答2:

For those who still facing the problem, above answer did not help me in android studio 2.2 Preview.

add this to your gradle file.

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

This fixed my issue.

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



回答3:

Just exemplifying Akshayraj's answer

Original Gradle file:

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

    compile 'com.android.support:support-annotations:25.3.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

}

Received error:

Error:Conflict with dependency 'com.android.support:support-annotations' in project ':app'.
Resolved versions for app (25.1.0) and test app (23.1.1) differ.
See http://g.co/androidstudio/app-test-app-conflict for details. "

FIXED when I added:

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

Final File:

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

    compile 'com.android.support:support-annotations:25.3.0'

    androidTestCompile 'com.android.support:support-annotations:25.3.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}


回答4:

My orignal app.gradle had:

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

    // Testing-only dependencies
    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'
}

which resulted in following error:
Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.4.0) and test app (22.2.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

After reading the link suggested in error, I found theses lines:

When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).

So I modified my app.gradle dependencies to:

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

    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.3.0'
    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'
}

Even after the above change gradle was not happy :-(:
Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.4.0) and test app (23.3.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

Change in test apk version was different! So I modified the version string as pasted below which worked for me:

(Nirvana)

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

    // Testing-only dependencies
    androidTestCompile 'com.android.support:support-annotations:23.4.0' //test APK
    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'
}


回答5:

Took me a while to get out of this error. But this is what has worked for me, give it a try:

NOTE: Am using compileSdkVersion 26

I removed both androidTestImplementation 'com.android.support.test:runner:1.0.2' & androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' in the dependencies block in build.gradle(Module: app). So I ended up with this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.date.brian.cradletest"
        minSdkVersion 15
        targetSdkVersion 26
        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:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    compile 'com.android.support:design:26.1.0'
    compile 'com.android.support:cardview-v7:26.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.getbase:floatingactionbutton:1.9.0'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    testImplementation 'junit:junit:4.12'
}

I hope this comes in handy!



回答6:

You have to use the same version for app and androidTest APKs. To do this, specify the same version as your app,

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

where 24.1.1 is the version number of the dependency used in your app



回答7:

compile 'com.android.support:design:24.1.1'


回答8:

For me, The build tool version has to align with the dependency versions. So lets say the build tool version is 26.1.0, the Gradle dependency version has to obey it.

The simplest way is to create a version variable & use it. See the example below

ext {
    buildVersion = '26.1.0'
}

dependencies {
    compile "com.android.support:appcompat-v7:${buildVersion}"
}


回答9:

Just exclude 'annotations'. No harm will be done

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


回答10:

  1. Open Android Studio

  2. Navigate to Project > Gradle Scripts > build.gradle (Module:app)

  3. Add dependencies {androidTestCompile 'com.android.support:support-annotations:xx.x.x'}

  4. You can replace xx.x.x to version which is showing in an error

  5. Save Gradle script

  6. Build Project

Hope this will work! :)



回答11:

It was solved after adding the last line:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.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'
compile 'com.android.support:support-annotations:27.1.1'}


回答12:

remove test dependency from build.gradel file for resolving the issues



回答13:

/*
Resolves dependency versions across test and production APKs, specifically, transitive
dependencies. This is required since Espresso internally has a dependency on support-annotations.
*/
configurations.all {
    resolutionStrategy.force "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
}