57

I am trying to implement ActionBar-PullToRefresh from https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/QuickStart-ABC. I just made the switch from Eclipse to Android Studio so I am totally new to AS and Gradle.

chrisbanes writes on the site:

The easiest way to add ActionBar-PullToRefresh to your project is via Gradle, you just need to add the following dependency to your build.gradle:

dependencies {  
    mavenCentral()
    compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}

Does this mean that I don't have to download the library and Gradle takes care of it so that I always have the latest version? I just don't know where to put the above line. I have two gradle.build files one in my root that looks like:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

and the one in my project which looks like:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.android.support:appcompat-v7:18.0.+'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

Do I have to add a repository somewhere?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
f4b
  • 868
  • 1
  • 8
  • 10
  • [Now the default is jcenter rather than Maven Central](http://stackoverflow.com/a/41847740/3681880) – Suragch Jan 25 '17 at 10:29

3 Answers3

52

It will work when you put this line in your project build.gradle, in the dependencies section:

compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'

Also, add:

repositories {
    mavenCentral()
}

So:

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.android.support:appcompat-v7:18.0.+'
    compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
}

Gradle will download the needed resources automatically for you.

nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • 5
    Also don't put it in the top-level build.gradle file -- the `repositories` and `dependencies` blocks there tell Gradle where to find the Android-Gradle plugin for the build system itself. Put it in the build.gradle file in your application module directory. – Scott Barta Feb 15 '14 at 15:06
  • 1
    @ScottBarta Can I do this when the github project doesn't publish maven artifacts? As in, do this as a replacement to configuring it as a library project manually? – Dheeraj Bhaskar May 20 '14 at 21:42
  • 4
    @dheeraj no, that's not possible. – nhaarman May 20 '14 at 21:43
  • howd you know the gradle link when the git part doest even share its gradle link – ralphgabb Jul 22 '15 at 01:25
  • 1
    @ralphspoon When the project doesn't share the link, it probably doesn't have one. You can also search for the project on http://search.maven.org/. – nhaarman Jul 22 '15 at 06:45
  • If there's no Maven equivalent I would follow Method 2 in this link: https://github.com/MagicMicky/FreemiumLibrary/wiki/Import-the-library-in-Android-Studio – Gonan Jan 15 '16 at 18:57
  • Thank you so much! – Akash Agarwal May 22 '16 at 22:15
33

Use https://jitpack.io/

allprojects {
    repositories { 
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
dependencies {
    compile 'com.github.User:Repo:Tag'
}
Rohodude
  • 495
  • 2
  • 5
  • 18
deviant
  • 3,539
  • 4
  • 32
  • 47
-1

This is for Kotlin DSL (build.gradle.kts):

repositories {
    // Other repositories...

    maven("https://jitpack.io")
    // OR maven { url = uri("https://jitpack.io") }
}

There are two places inside which you can insert the above code block:

Mahozad
  • 18,032
  • 13
  • 118
  • 133