Gradle build error: Failed to resolve:

2019-04-09 05:34发布

问题:

I just downloaded Android Studio and created a new project and I'm getting gradle build errors:

Failed to resolve: com.android.support.test.espresso-core:2.2.2

and

Failed to resolve: com.android.support.appcompat-v7:25.3.1

This error was resolved reinstalling the SDK Tools + Repository + API when launching android studio as admin.

I've installed API Level 25 which what I want to build on and have downloaded the SDK Build-Tools. I have also already download the support repository

Here's my app file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.3'
    defaultConfig {
        applicationId "com.jtsalas.mirrorcontrol"
        minSdkVersion 25
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_7
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

build.gradle:

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

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

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

allprojects {
    repositories {
        jcenter()
    }
}

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

回答1:

From the SDK manager, make sure you have both the Android Support Repository and Google Repository installed and up to date. You should then be able to find the relevant artifacts in sub folders of your /extras/android/m2repository directory



回答2:

From your error it seems that you are not including espresso libraries. The solution to this is adding espresso core library which is part Android Testing support library which is hosted in the google's Maven repository think this as kind of git repository but for dependencies.

So we tell the gradle build system to look in the Maven repository for dependencies by specifying its URL.

This is done by adding Maven url in the application level build.gradle file under repositories block

repositories {
    jcenter()
    maven{
        url "https://maven.google.com"
    }
}

and in the module level build.gradle file mention the dependencies that you want from the maven repository by mention their name as follows:

dependencies{
//other dependencies go here

//testing dependencies
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
}

That is the reason for including Maven repository url in the app level build.gradle file, hope this helps.



回答3:

In project.gradle file, the allprojects root align this way:

allprojects {
  repositories {
      google()
      jcenter()
      maven { url "https://jitpack.io" }
   }
}

jitpack is used as the dependency for multiple libraries, if you're not using any sort of library that don't requires it then not include maven line.



回答4:

Well, I don't know the perfect answer but..... how about comparing with my SDK Tools?



回答5:

I solved it by uninstalling Android Studios and deleting old versions of Android Studios in my C:\Users[Username] and reinstalled Android Studio as administrator.



回答6:

It seems like you updated android studio and opening previous project in it.The simplest way is create new project and copy 1. compileSdkVersion 26 2. buildToolsVersion "26.0.1" 3. targetSdkVersion 26 4. compile 'com.android.support:appcompat-v7:26.+' and paste them in appropriate places in app level build gradle. it will ask to update to take advantages .. allow it to update. best luck ... It worked for me.