44

I'm using Android Studio with an external Android SDK. I have installed the support library and the support repository. The support repository is in:

~/Development/Tools/android/sdk/extras/android/m2repository

When I add a dependency to the support library in the build.gradle file, like:

...

repositories {
    mavenCentral()
}

...

dependencies {
   compile "com.android.support:support-v4:18.0.+"
}

Android Studio cannot find the support libraries (cannot resolve symbol etc) and Gradle also cannot find the libraries:

Gradle: A problem occurred configuring project ':TestAndroidStudio'.
> Failed to notify project evaluation listener.
   > Could not resolve all dependencies for configuration ':TestAndroidStudio:_DebugCompile'.
      > Could not find any version that matches com.android.support:support-v4:18.0.+.
        Required by:
            TestAndroidStudio:TestAndroidStudio:unspecified

How do I specify in Android Studio and/or the build.gradle file the location of the Android support repository?

Jaap van Hengstum
  • 4,494
  • 4
  • 30
  • 34
  • In the `local.properties` file is the the right SDK path? – rciovati Nov 13 '13 at 16:12
  • The only line in my `local.properties` file is: `sdk.dir=/Users/jaap/Development/Tools/android/sdk` which is the path to the external sdk. – Jaap van Hengstum Nov 13 '13 at 16:17
  • Are you sure you have the support library version 18? Now it is 19. – rciovati Nov 13 '13 at 16:50
  • Anyone arriving here because they failed adding 'compile' statement to build.gradle note that there are TWO build.gradle files, and compile must be added to a dependencies section of the file that has no dependencies section (the one with targetSdkVersion = x etc) – Lorne K Apr 19 '18 at 17:49

6 Answers6

28

You are probably hit by this bug which prevents the Android Gradle Plugin from automatically adding the "Android Support Repository" to the list of Gradle repositories. The work-around, as mentioned in the bug report, is to explicitly add the m2repository directory as a local Maven directory in the top-level build.gradle file as follows:

allprojects {
    repositories {
        // Work around https://code.google.com/p/android/issues/detail?id=69270.
        def androidHome = System.getenv("ANDROID_HOME")
        maven {
            url "$androidHome/extras/android/m2repository/"
        }
    }
}
Lorenzo Sciuto
  • 1,655
  • 3
  • 27
  • 57
sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • I'm adding the libs to our local maven artifactory, so it works for everyone. – philipp Nov 01 '14 at 19:26
  • 1
    That should not be necessary anymore as at least I cannot reproduce the bug anymore with the latest Android plugin and build tools (also see my comment to the original bug). – sschuberth Nov 01 '14 at 20:43
  • This solves another issue: adding android annotation support (e.g. @NonNull) to a bare java lib of android app. – DenisGL Jul 20 '16 at 19:45
  • still had this after upgrading to Android Studio 2.3 ... is there any info what was the cause of and how the hook in mechanism works in Android studio?! the only thing I can confirm: I can "fix" it by simply "uninstall then reinstall" the support repository via SDK manager ... no glue why this helped even the files existing and reinstalled again are exactly the same – childno͡.de Apr 03 '17 at 07:22
26

Gradle can work with the 18.0.+ notation, it however now depends on the new support repository which is now bundled with the SDK.

Open the SDK manager and immediately under Extras the first option is "Android Support Repository" and install it

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ngatirauks
  • 779
  • 8
  • 18
  • 1
    Also make sure your "Android SDK Tools" is updated and consider using "19.0.+" as that is the latest. – Michael Barany Nov 26 '13 at 03:33
  • developer.android warned against using + in version numbers [in this document](https://developer.android.com/studio/releases/gradle-plugin.html#updating-plugin) and many other places. – AaA Jul 06 '17 at 02:49
11

Found a solution.

1) Go to where your SDK is located that android studio/eclipse is using. If you are using Android studio, go to extras\android\m2repository\com\android\support\. If you are using eclipse, go to \extras\android\support\

2) See what folders you have, for me I had gridlayout-v7, support-v4 and support-v13.

3) click into support-v4 and see what number the following folder is, mine was named 13.0

Since you are using "com.android.support:support-v4:18.0.+", change this to reflect what version you have, for example I have support-v4 so first part v4 stays the same. Since the next path is 13.0, change your 18.0 to:

"com.android.support:support-v4:13.0.+"

This worked for me, hope it helps!

Update:

I noticed I had android studio set up with the wrong SDK which is why originally had difficulty updating! The path should be C:\Users\Username\AppData\Local\Android\android-sdk\extras\

Also note, if your SDK is up to date, the code will be:

"com.android.support:support-v4:19.0.+"
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
escull638
  • 155
  • 1
  • 9
  • Thanks. I was a bit stuck as well because in the online maven repository the support library is under com.google.android, but here it's under com.android.support. – joe_deniable Jan 01 '14 at 11:04
  • Took me hours to figure out! Turns out it was just a simple change haha, its good that you got sorted! – escull638 Jan 02 '14 at 22:13
  • My SDK folder is in drive E (C is SSD) and `[SDK]\extras\android\m2repository\com\android\support\support-v4` contains folder numbers starting from `13.0.0` up to `26.0.0-alpha1` (38 Folders), repo is still not accessible. – AaA Jul 06 '17 at 02:44
  • developer.android already warned against using + in version numbers [in this document](https://developer.android.com/studio/releases/gradle-plugin.html#updating-plugin) – AaA Jul 06 '17 at 02:49
4

Android Studio 3

Make sure you have the latest version of Android Studio. The support library is included by default when you create new projects. If you are adding the Support Library to a project that doesn't have it, then you just need to add a single line to your app module's build.gradle file, and then sync gradle.

build.gradle

dependencies {
    ...
    implementation 'com.android.support:appcompat-v7:27.1.1'
} 

It should just be that easy, though there may be some things to note:

  • Android Studio should give you a warning nowadays if the support library needs to be updated. Just update the 27.1.1 numbers that I have here to whatever it tells you to. You can also manually check what the latest revision is if you want to.
  • The implementation keyword replaces compile that was used in Android Studio 2.x. (What's the difference?)
  • There are other support library packages that you may need to include depending on what your app uses (like constraint-layout or recyclerview).
  • Make sure that you have the latest updates for everything in the SDK Manager. Go to Tools > SDK Manager.

Documentation

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
2

I used to get similar issues. Even after installing the support repository, the build used to fail.

Basically the issues is due to the way the version number of the jar files are specified in the gradle files are specified properly.

For example, in my case i had set it as "compile 'com.android.support:support-v4:21.0.3+'"

On removing "+" the build was sucessful!!

Vjay
  • 111
  • 9
  • developer.android already warned against using + in version numbers [in this document](https://developer.android.com/studio/releases/gradle-plugin.html#updating-plugin) – AaA Jul 06 '17 at 02:47
-1

Instead of doing this:

compile "com.android.support:support-v4:18.0.+"

Do this:

compile 'com.android.support:support-v4:18.0.+' 

Worked for me