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.

Android

Android Problem Overview


I am new to Android App Development. When I tried to create a new project,Android Project...the following message popped up..

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. Information:Gradle tasks [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar]

This is the screenshot of my project click here to see screenshot of the error i got

i also tried adding this code to my dependency.. androidTestCompile 'com.android.support:support-annotations:23.3.0' this didn't work out. I also tried 27.1.1 and 26.1.0.. that didn't work out either.

Android Solutions


Solution 1 - Android

Based on your screenshot i found two working solutions:

First solution: add to dependencies of your gradle module this line

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

and sync your project

Note: if you are using Android studio 3+ change compile to implementation

Second solution: Configure project-wide properties found in the documentation https://developer.android.com/studio/build/gradle-tips.html#configure-project-wide-properties

in project gradle add this line:

// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 26
    // You can also use this to specify versions for dependencies. Having consistent
    // versions between modules can avoid behavior conflicts.
    supportLibVersion = "27.1.1"
}

Then to access this section change compileSdkVersionline to be

compileSdkVersion rootProject.ext.compileSdkVersion

and at dependencies section change the imported library to be like this:

compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"

and sync your project

Note: if you are using Android studio 3+ change compile to implementation

For the difference between compile and implementation look at this https://stackoverflow.com/questions/44493378/whats-the-difference-between-implementation-and-compile-in-gradle

Solution 2 - Android

Add the below line in your app.gradle file before depencencies block.

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

There's also screenshot below for a better understanding.

Configurations.all block

  1. the configurations.all block will only be helpful if you want your target sdk to be 26. If you can change it to 27 the error will be gone without adding the configuration block in app.gradle file.

  2. There is one more way if you would remove all the test implementation from app.gradle file it would resolve the error and in this also you dont need to add the configuration block nor you need to change the targetsdk version.

Hope that helps.

Solution 3 - Android

If you use version 26 then inside dependencies version should be 1.0.1 and 3.0.1 i.e., as follows

  androidTestImplementation 'com.android.support.test:runner:1.0.1'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

If you use version 27 then inside dependencies version should be 1.0.2 and 3.0.2 i.e., as follows

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

Solution 4 - Android

If you using Android Studio 3.1.+ or above

just put this in your gradle depedencies:

implementation 'com.android.support:support-annotations:27.1.1'

Overall like this:

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

    implementation 'com.android.support:support-annotations:27.1.1'
}

Solution 5 - Android

This is due a conflict of versions, to solve it, just force an update of your support-annotations version, adding this line on your module: app gradle

implementation ('com.android.support:support-annotations:27.1.1')

Hope this solves your issue ;)

Edit

Almost forgot, you can declare a single extra property (https://docs.gradle.org/current/userguide/writing_build_scripts.html#sec:extra_properties) for the version, go to your project (or your top) gradle file, and declare your support, or just for this example, annotation version var

ext.annotation_version = "27.1.1"

Then in your module gradle replace it with:

implementation ("com.android.support:support-annotations:$annotation_version")

This is very similar to the @emadabel solution, which is a good alternative for doing it, but without the block, or the rootproject prefix.

Solution 6 - Android

Adding this to build.gradle (Module app) worked for me:
compile 'com.android.support:support-annotations:27.1.1'

Solution 7 - Android

Don't worry It is simple:

Go to the "Project" Directory structure and in that go to "Gradle Scripts" and inside it go to "build.gradle (Module:app)" and double click it.

Now - Scroll down the program and in that go to the dependencies section : Like below


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

}


Now in this Delete the last two lines of code and rebuild the app and now it will work

The dependencies should be:


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.2'
testImplementation 'junit:junit:4.12'

}


REBUILD THE APP AND IT WORKS !!

Solution 8 - Android

Go to the build.gradle(Module App) in your project:

enter image description here

Follow the pic and change those version:

compileSdkVersion: 27
targetSdkVersion: 27

and if android studio version 2: Change the line with this line:

compile 'com.android.support:appcompat-v7:27.1.1'

else Change the line with this line:

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

and hopefully, you will solve your bug.

Solution 9 - Android

Add this to your gradle file.

implementation 'com.android.support:support-annotations:27.1.1'

Solution 10 - Android

Another simple way to solve this problem is to edit your build.gradle (app):

  1. Go to android tag and change compileSdkVersion 26 to compileSdkVersion 27
  2. Go to defaultConfig tag and change targetSdkVersion 26 to targetSdkVersion 27
  3. Got to dependencies tag and change implementation 'com.android.support:appcompat-v7:26.1.0' to implementation 'com.android.support:appcompat-v7:27.1.1'

Solution 11 - Android

I have the same problem, in build.gradle (Module:app) add the following line of code inside dependencies:

dependencies 
{
   ...
   compile 'com.android.support:support-annotations:27.1.1'
}

It worked for me perfectly

Solution 12 - Android

#Important Update

###Go to project level build.gradle, define global variables

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlinVersion = '1.2.61'

    ext.global_minSdkVersion = 16
    ext.global_targetSdkVersion = 28
    ext.global_buildToolsVersion = '28.0.1'
    ext.global_supportLibVersion = '27.1.1'
}

###Go to app level build.gradle, and use global variables

app build.gradle

android {
    compileSdkVersion global_targetSdkVersion
    buildToolsVersion global_buildToolsVersion
    defaultConfig {
        minSdkVersion global_minSdkVersion
        targetSdkVersion global_targetSdkVersion
}
...

dependencies {
    implementation "com.android.support:appcompat-v7:$global_supportLibVersion"
    implementation "com.android.support:recyclerview-v7:$global_supportLibVersion"
    // and so on...
}

some library build.gradle

android {
    compileSdkVersion global_targetSdkVersion
    buildToolsVersion global_buildToolsVersion
    defaultConfig {
        minSdkVersion global_minSdkVersion
        targetSdkVersion global_targetSdkVersion
}
...

dependencies {
    implementation "com.android.support:appcompat-v7:$global_supportLibVersion"
    implementation "com.android.support:recyclerview-v7:$global_supportLibVersion"
    // and so on...
}

The solution is to make your versions same as in all modules. So that you don't have conflicts.

###Important Tips

> I felt when I have updated versions of everything- gradle, sdks, > libraries etc. then I face less errors. Because developers are working > hard to make it easy development on Android Studio.

Always have latest but stable versions Unstable versions are alpha, beta and rc, ignore them in developing.

I have updated all below in my projects, and I don't face these errors anymore.

Happy coding! :)

Solution 13 - Android

If changing target sdk version doesn't help then if you have any dependency with version 3.0.2 then change it to 3.0.1.

e.g change

androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'

to

androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.1'

Solution 14 - Android

A better solution is explained in the official explanation. I left the answer I have given before under the horizontal line.

According to the solution there:

Use an external tag and write down the following code below in the top-level build.gradle file. You're going to change the version to a variable rather than a static version number.

ext {
    compileSdkVersion = 26
    supportLibVersion = "27.1.1"
}

Change the static version numbers in your app-level build.gradle file, the one has (Module: app) near.

android {
    compileSdkVersion rootProject.ext.compileSdkVersion // It was 26 for example
    // the below lines will stay
}

// here there are some other stuff maybe

dependencies {
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    // the below lines will stay
}

Sync your project and you'll get no errors.


You don't need to add anything to Gradle scripts. Install the necessary SDKs and the problem will be solved.

In your case, install the libraries below from Preferences > Android SDK or Tools > Android > SDK Manager

enter image description here

Solution 15 - Android

Add this line under the dependencies in your gradle file

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhashTag_bibuView Question on Stackoverflow
Solution 1 - AndroidemadabelView Answer on Stackoverflow
Solution 2 - AndroidHarsh JainView Answer on Stackoverflow
Solution 3 - AndroidHarishView Answer on Stackoverflow
Solution 4 - AndroidUray FebriView Answer on Stackoverflow
Solution 5 - AndroiddesgraciView Answer on Stackoverflow
Solution 6 - AndroidSan AskarulyView Answer on Stackoverflow
Solution 7 - AndroidManika AgrawalView Answer on Stackoverflow
Solution 8 - AndroidMaizied Hasan ShuvoView Answer on Stackoverflow
Solution 9 - AndroidJeffyView Answer on Stackoverflow
Solution 10 - AndroidRealdinhoView Answer on Stackoverflow
Solution 11 - AndroidMohammed H. HannoushView Answer on Stackoverflow
Solution 12 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 13 - AndroidHarshadView Answer on Stackoverflow
Solution 14 - AndroidOnur TunaView Answer on Stackoverflow
Solution 15 - AndroidPavithaView Answer on Stackoverflow