63

I am having this issue in Android studio.

Error:Failed to resolve: com.android.support:appcompat-v7:27.+
<a href="install.m2.repo">Install Repository and sync project</a><br><a href="open.dependency.in.project.structure">Show in Project Structure 
dialog</a>

My Android Studio is full of error android studio cannot identify libraries. The whole screen looks like this.

image

This is my Gradle code:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.1"

    defaultConfig {
        applicationId "com.example.hp.temp"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:27.+'
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Heli Shah
  • 783
  • 1
  • 8
  • 11

2 Answers2

120

Find root build.gradle file and add google maven repo inside allprojects tag

repositories {
        mavenLocal()
        mavenCentral()
        maven {                                  // <-- Add this
            url 'https://maven.google.com/' 
            name 'Google'
        }
    } 

It's better to use specific version instead of variable version

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

If you're using Android Plugin for Gradle 3.0.0 or latter version

repositories {
      mavenLocal()
      mavenCentral()
      google()        //---> Add this
} 

and inject dependency in this way :

implementation 'com.android.support:appcompat-v7:27.0.0'
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
  • `implementation 'com.android.support:appcompat-v7:27.0.0'` How do I decide what version to specify? Still gives dex error. `v7:27.0.1` or `v7:27.0.2` – Rahul Bali Dec 27 '17 at 14:44
  • 2
    I always prefer to use latest version, current latest is `27.0.2`, enable multidex if you're getting dex error. – Abhishek Aryan Dec 27 '17 at 15:20
  • `Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex` Continuously getting this error. I have done many fixes, but nothing helps. Also, When I set `CompileSdkVersion 26`, it compiles but app crashes in the phone. I have also enabled the multidex support, still the same issue. – Rahul Bali Dec 28 '17 at 17:48
  • @RahulParashar ask a new question with complete `build.gradle` file of android module, without knowing all your injected library I can't help you. – Abhishek Aryan Dec 29 '17 at 05:31
  • @Aryan I found that also including "google()" in the "allprojects" of your project gradle will complete the build, in addition to the answer above – angryITguy Jan 22 '18 at 02:49
  • @giulio repo link can only be added in `repositories` tag which is part of `allprojects` or `buildscript` tag in root `build.gradle` file. – Abhishek Aryan Jan 22 '18 at 03:00
  • Adding repositories does not solve the problem for me. – Andi Oct 16 '18 at 14:00
57

If you are using Android Studio 3.0 or above make sure your project build.gradle should have content similar to-

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

    }
}

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

Note- position really matters add google() before jcenter()

And for below Android Studio 3.0 and starting from support libraries 26.+ your project build.gradle must look like this-

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

check these links below for more details-

1- Building Android Apps

2- Add Build Dependencies

3- Configure Your Build

D_Alpha
  • 4,039
  • 22
  • 36