1

I already saw this question, but it is not helping me. First of all, I tried to add google play services in my project using:

dependencies{
compile 'com.google.android.gms:play-services:6.5.87'
}

It was showing me error: enter image description here

Then I updated my studio to 1.0.1 and gradle to 1.0.0. And then I again synced the project with gradle. And it worked! It showed me another option despite of two options shown in above screenshot. It was "Install the library"(something like that). I clicked it and it popped up a dialog, and I installed the library(it was like downloadind using SDK manager and not like gradle downloads).

Now, I tried to download this library using:

compile('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT@aar') {
        transitive = true
    }

And it gives me error: enter image description here

My android repository is updated: enter image description here

Also, my internet connection is working fine. I tried to sync project many times, but same error all the time. I am not running gradle in offline mode: enter image description here

How to fix this? And what is the permanent solution? And why is all this happening?

Community
  • 1
  • 1
berserk
  • 2,690
  • 3
  • 32
  • 63
  • The business about play-services doesn't seem relevant to your question. – Scott Barta Dec 18 '14 at 19:03
  • @OneWay That are about SDK updates. – berserk Dec 18 '14 at 19:04
  • Ok, let me update. @OneWay – berserk Dec 18 '14 at 19:05
  • @ScottBarta Does that mean it was the fault of library path? – berserk Dec 18 '14 at 19:07
  • 1
    That library doesn't seem to exist in Maven Central, and accessing SNAPSHOT versions of libraries is problematic as they're by definition unpublished. Whatever instructions you got that told you to include the dependency this way are probably misleading. – Scott Barta Dec 18 '14 at 19:08
  • 2
    Same build script snippet, same library version, same error message. Just because the answer wasn't accepted by the original asker doesn't mean it's not a duplicate. At a minimum it's a reminder that it's always good to research for prior answers before asking a SO question; the chances that you're the first person to ever encounter a given error are slim. With that research in hand, even if the answer isn't complete, you may be able to figure it out on your own (and supply a better answer to the question), or at least ask a more detailed question of your own. – Scott Barta Dec 18 '14 at 19:16
  • @ScottBarta Actually, the question was not limited to only swipe library. I actually meant by scenarios in which I get this kind of error while adding library. Like why I was getting the same error in play services lib? And how was I supposed to know that there was a problem in swipe library, as I already suffered from same situation before(GPS library)?? – berserk Dec 18 '14 at 19:26

1 Answers1

6

I found this question: Studio failed to download library from gradle repository which describes the exact same error, and that question had this bit of build script that you need to add to the build file that has the dependency statement in question:

repositories {
    maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}

When I do this, it works for me.

As to the general question of why this happens (and the better question of why the solution is different for different libraries):

Gradle, the build system that Android Studio uses, has the ability to automatically download library dependencies from the Internet. By and large this is a big boon for developers, because instead of having to manually download archive files, put them in the right place in your project, check them into source control, and repeat the process for new versions, now you just have to add a line of build script and the build system takes care of the housekeeping for you. The major downsides are Internet connectivity woes, which affect different developers to different degrees, and some added confusion about what it means when you get an error.

How does Gradle know where to download dependencies? Most Gradle build scripts contain a block that looks like this:

repositories {
    jcenter()
}

or it may be mavenCentral() instead of jcenter(). This tells the build system to look in the JCenter or Maven Central global repositories (and JCenter is in a simplistic way of thinking about it a value-added mirror of MavenCentral); these contain archives of many versions of many, many, many libraries and are very convenient to use.

You can specify other repositories as well. This swipelistview library hasn't been uploaded to Maven Central, so the developer has made a repository for it available via a URL: if you add that URL to your repositories block, it will look for it there.

I was worried about the fact that you're accessing a SNAPSHOT version of the library -- these are supposed to be unpublished by definition. But adding a dependency on the snapshot version of the library in my test project worked for me, and looking around that URL in a web browser reveals that there's only a "1.0-" (trailing dash included) version of the library, so there's some subtletly there I'm missing; if you know more, please edit my answer or comment.

In any event, there are a couple caveats to this explanation. Some libraries aren't on Maven Central or on any Internet-accessible archive (at least they're not officially published by Android), but are instead published as part of the Android SDK download and maintained via the SDK manager. The Android support libraries and Google libraries fall under this category. If you get errors about those not being found, you have to fix it via the SDK manager.

How does the build system know to look in the SDK for those, since you didn't tell it via the repositories block? This behavior is hardcoded into the Android Gradle plugin.

The other caveat is that there's a detail that trips up a lot of people, which is that you actually have two repositories blocks, though with the usual Android Studio setup they're often in different files. One is in a buildscript block, which usually lives in the top-level build.gradle file and looks like this:

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

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

The other often also lives in the top-level build.gradle, but you can augment it with another block in your module's build.gradle file. The top-level one looks like this:

allprojects {
    repositories {
        jcenter()
    }
}

and a module-level one would look like one of the previous examples in this answer. What do all of these mean?

The buildscript block tells Gradle where to find build system plugins. These are plugins that enhance the functionality of the build system itself but don't say anything about your actual project. In Android projects, the Android Gradle plugin is in this category, and unlike the Android/Google libraries, this one does live on Maven Central. The repositories block (in coordination with the dependencies block, which is not the same as the dependencies block for your project, keep reading) in buildscript tells the build system where to go look for these plugins.

The allprojects block in the top-level build file tells the build system to apply the bit of contained script to all build files in the project. In this example, it's telling it to add a repositories block pointing to JCenter to all subprojects. This is a convenience so you don't have to copy/paste it into multiple build files in your modules.

In your modules, you also have a repositories block, which in conjunction with the allprojects thingy, tells the build system where to go to get library dependencies for your project, as was previously discussed.

Community
  • 1
  • 1
Scott Barta
  • 79,344
  • 24
  • 180
  • 163