Gradle build error: Failed to resolve:

Viewed 21350

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
}
7 Answers

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.

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.

if your project is Flutter,

  1. clean project [by 'flutter clean' command]
  2. In project.gradle file add [ google() ]
  3. in Android studio: File Menu -> Invalidate...

project.gradle:

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