32

I know about this questions:

Failed to resolve: com.android.support:cardview-v7:26.0.0 android

Could not resolve com.android.support:appcompat-v7:26.1.0 [duplicate]

And I've read this documentations:

Migrate to Android Plugin for Gradle 3.0.0

Migrating to Android 8.0

So I'm posting this question believing its not a duplicate.

I've installed Android Studio 3.0.1. I didn't have any previous version before and I started a new project.

Every setting in Gradle file have been set by Android Studio itself and I've checked them all.

These are the file contents:

build.gradle (Module:app)

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "com.example.administrator.as301test"
            minSdkVersion 16
            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.0.2'
        implementation 'com.android.support:design:26.1.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }

Top level build.gradle

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

I use google() in repositories as here clearly says:

  // If you're using a version of Gradle lower than 4.1, you must instead use:
     // maven {
     //     url 'https://maven.google.com'
     // }

My gradle version is 4.1 so I don't need above code.

But I still get this error as the others have been asking about:

Error:Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:26.1.0.

Why should I get this error when I'm creating a new project in AS 3.0.1 and it has been set all the necessary settings?

Update

@Rohit's answer somehow solved the issue but I still get same error for some other dependencies.

Could not resolve com.android.support.test:runner:1.0.1

Could not resolve com.android.support.test.espresso:espresso-core:3.0.1

Could not resolve junit:junit:4.12

Ghasem
  • 14,455
  • 21
  • 138
  • 171

14 Answers14

27

Finally I fixed the problem by modifying build.gradle like this:

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 26
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
}

I've removed these lines as these will produce more errors:

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

Also I had same problem with migrating an existing project from 2.3 to 3.0.1 and with modifying the project gradle files like this, I came up with a working solution:

build.gradle (module app)

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.1"

    defaultConfig {
        applicationId "com.mobaleghan.tablighcalendar"
        minSdkVersion 16
        targetSdkVersion 27
    }

dependencies {
    implementation 'com.android.support:appcompat-v7:25.1.0'
    implementation 'com.android.support:design:25.1.0'
    implementation 'com.android.support:preference-v7:25.1.0'
    implementation 'com.android.support:recyclerview-v7:25.1.0'
    implementation 'com.android.support:support-annotations:25.1.0'
    implementation 'com.android.support:support-v4:25.1.0'
    implementation 'com.android.support:cardview-v7:25.1.0'
    implementation 'com.google.android.apps.dashclock:dashclock-api:2.0.0'
}

Top level build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171
10

enter image description heregoto Android->sdk->build-tools directory make sure you have all the versions required . if not , download them . after that goto File-->Settigs-->Build,Execution,Depoyment-->Gradle

choose use default gradle wapper (recommended)

and untick Offline work

gradle build finishes successfully for once you can change the settings

If it dosent simply solve the problem

check this link to find an appropriate support library revision

https://developer.android.com/topic/libraries/support-library/revisions

Make sure that the compile sdk and target version same as the support library version. It is recommended maintain network connection atleast for the first time build (Remember to rebuild your project after doing this)

Harsh
  • 363
  • 4
  • 14
8

try this :

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
defaultConfig {

        targetSdkVersion 26
    }

}


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

It has worked for me

Ghasem
  • 14,455
  • 21
  • 138
  • 171
ROHIT LIEN
  • 497
  • 3
  • 8
  • I'm not sure, but I guess this somehow worked for some errors. Any suggestion for `Could not resolve com.android.support.test:runner:1.0.1`? – Ghasem Nov 23 '17 at 08:19
  • 1
    This makes sense as 26.1.0 is not released yet as per https://developer.android.com/studio/releases/build-tools.html. It seems to be a bug in AS 3.0.1 – garnet Dec 01 '17 at 14:01
  • @AlexJolig , com.android.support.test:runner:1.0.0 or other versions – ROHIT LIEN Nov 21 '19 at 08:11
5

I tried all of the above and nothing worked for me.

Then I followed Gradle Settings > Build Execution, Deployment > Gradle > Android Studio and checked "Disable embedded Maven repository".

Did a build with this checked and the problem was solved.

Fyko
  • 57
  • 3
  • Wow, this did it for me too. Does anyone know why this is happening? It seems to take longer to build now... I was also using Kotlin in case that helps anyone else. – Rob Rose Jan 20 '18 at 01:33
3

Just go to File\Settings\Gradle. Deselect the "Offline work" box. Now you can connect and download any necessary or missing dependencies

Clement Olaniyan
  • 333
  • 2
  • 11
2

Your android studio may be forgot to put : buildToolsVersion "26.0.0" you need 'buildTools' to develop related design and java file. And if there is no any buildTools are installed in Android->sdk->build-tools directory then download first.

Jayesh Rathod
  • 600
  • 1
  • 5
  • 18
2

well got this answer from another site and don't want to take any credit for this but this solution works like butter.

Go to File\Settings\Gradle. Deselect the "Offline work" box. Now you can connect and download any necessary or missing dependencies.

Aditya Tandon
  • 650
  • 1
  • 5
  • 8
  • What's the value-add over https://stackoverflow.com/a/48848256/101361 already posted here? Your answer part is a verbatim copy of it – laalto Apr 06 '18 at 16:00
  • 1
    ok well i didn't see that one and i got this from another site and thats why i said i got this answer from another site and not taking any credit for this. plus if someone came to this link and not that one then he will be seeing his solution right here. he doesn't have to search anymore or anywhere. – Aditya Tandon Apr 10 '18 at 08:23
2

This work for me. In the android\app\build.gradle file you need to specify the following

compileSdkVersion 26
buildToolsVersion "26.0.1"

and then find this

compile "com.android.support:appcompat-v7"

and make sure it says

compile "com.android.support:appcompat-v7:26.0.1"
Janaka Pushpakumara
  • 4,769
  • 5
  • 29
  • 34
2

OK It's A Wrong Approach But If You Use it Like This :

compile "com.android.support:appcompat-v7:+"

Android Studio Will Use The Last Version It Has.

In My Case Was 26.0.0alpha-1.

You Can See The Used Version In External Libraries (In The Project View).

I Tried Everything But Couldn't Use Anything Above 26.0.0alpha-1, It Seems My IP Is Blocked By Google. Any Idea? Comment

Meisam
  • 358
  • 5
  • 14
1

In my case, this error occur when i tried to use gridView

I resolved it by removing this line from build.grade(Module) file

implementation 'com.android.support:gridlayout-v7:28.0.0-alpha3'
Asim
  • 161
  • 1
  • 12
0

This issue seems to like the following.

How to resolve repository certificate error in Gradle build

Below steps may help:

1. Add certificate to keystore-

Import some certifications into Android Studio JDK cacerts from Android Studio’s cacerts.

Android Studio’s cacerts may be located in

{your-home-directory}/.AndroidStudio3.0/system/tasks/cacerts

I used the following import command.

$ keytool -importkeystore -v -srckeystore {src cacerts} -destkeystore {dest cacerts}

2. Add modified cacert path to gradle.properties-

systemProp.javax.net.ssl.trustStore={your-android-studio-directory}\\jre\\jre\\lib\\security\\cacerts
systemProp.javax.net.ssl.trustStorePassword=changeit

Ref : https://www.cresco.co.jp/blog/entry/2014//

towa
  • 211
  • 1
  • 4
0

Try to clear cache in android studio by File-> Invalidate cache -> invalidate after invalidating build-> clean project Then you can able to build the project

0

this work for me

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

change 26.0.0 to 26.0.1

compile 'com.android.support:appcompat-v7:26.0.1'
Ghasem
  • 14,455
  • 21
  • 138
  • 171
DILSHAD AHMAD
  • 215
  • 2
  • 4
0

this work for me. add configurations.all in app/build.gradle

android {
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:27.1.1'
    }
}
Zhentao
  • 11