将GitHub库添加为Android Studio项目的依赖项

57

我正在尝试从https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/QuickStart-ABC实现ActionBar-PullToRefresh。 我刚刚从Eclipse切换到Android Studio,所以对AS和Gradle都不太熟悉。

chrisbanes在网站上写道:

将ActionBar-PullToRefresh添加到项目中的最简单方法是通过Gradle,您只需要将以下依赖项添加到build.gradle中:

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

这是否意味着我不需要下载库,Gradle会处理它,以便我始终拥有最新版本?我只是不知道要把上面的代码放在哪里。我有两个gradle.build文件,一个在我的根目录下,看起来像:

// 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.+'
    }
}

而我项目中的那一个看起来像:

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')
    }
}

我需要在哪里添加存储库吗?


现在默认是jcenter而不是Maven Central。 - Suragch
3个回答

52

当你把这行代码放在你的项目build.gradle文件中的dependencies部分时,它就可以工作了:

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

另外,添加:

repositories {
    mavenCentral()
}

所以:

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会自动为您下载所需的资源。


5
不要将它放在顶层的build.gradle文件中-那里的“repositories”和“dependencies”块告诉Gradle在哪里找到用于构建系统本身的Android-Gradle插件。将其放置在应用程序模块目录中的build.gradle文件中。 - Scott Barta
1
@ScottBarta 当 Github 项目没有发布 Maven 构件时,我能否这样做?也就是说,这可以作为手动配置库项目的替代吗? - Dheeraj Bhaskar
4
抱歉,不可能实现。 - nhaarman
你怎么知道 Gradle 的链接,当 Git 部分甚至没有分享它的 Gradle 链接。 - ralphgabb
1
@ralphspoon 当项目没有分享链接时,可能是因为它没有链接。您也可以在 http://search.maven.org/ 上搜索该项目。 - nhaarman
显示剩余2条评论

33

使用https://jitpack.io/

allprojects {
    repositories { 
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
dependencies {
    compile 'com.github.User:Repo:Tag'
}

如果你遇到了“无法解析”的错误,请查看这个SO答案:https://dev59.com/zVwY5IYBdhLWcg3wEEN2#33059275 - Blake Regalia

-1

这是关于 Kotlin DSL(build.gradle.kts)的:

repositories {
    // Other repositories...

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

您可以在以下两个位置之一插入上述代码块:

  • 传统方式:在顶层的 build.gradle.kts 文件中
  • 新方式:在 settings.gradle.kts 文件中的 dependencyResolutionManagement { 块内;
    有关此新功能的更多信息,请访问 Gradle 用户指南:集中式存储库声明

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接