【问题标题】:Is it possible to declare git repository as dependency in android gradle?是否可以在 android gradle 中将 git 存储库声明为依赖项?
【发布时间】:2026-01-12 22:35:01
【问题描述】:

我想使用来自 mavencentral 的库的主版本。

android gradle 中是否可以将 git 仓库声明为依赖项?

【问题讨论】:

  • 你找到解决办法了吗?

标签: android android-studio android-build android-gradle-plugin


【解决方案1】:

对我来说最好的方法是:

https://jitpack.io

步骤 1. 将 JitPack 存储库添加到存储库末尾的 build.gradle:

repositories {
    // ...
    maven { url "https://jitpack.io" }
}

第二步,在表单中添加依赖

dependencies {
    implementation 'com.github.User:Repo:Tag'
}

可以在master分支上构建最新的commit,例如:

dependencies {
    implementation 'com.github.jitpack:gradle-simple:master-SNAPSHOT'
}

【讨论】:

  • 如果你想要最新的master然后使用版本-SNAPSHOTjitpack.io/docs/#snapshots
  • 我猜这是垃圾邮件。但我真的想说jitpack.io 真的真的很酷.......
  • 确保你把它放在主要部分,而不是buildscript
  • 什么是“标签”??
  • @Makalele,“TAG”是一个 git 标签(任何带标签的提交,例如,某个版本)。 docs
【解决方案2】:

或者您可以像这样将存储库注册为子模块

$ git submodule add my_sub_project_git_url my-sub-project

然后将项目包含在您的 settings.gradle 文件中,该文件应如下所示

include ':my-app', ':my-sub-project'

最后,像这样将项目编译为应用程序 build.gradle 文件中的依赖项

dependencies {
  compile project(':my-sub-project')
}

然后,在克隆项目时,您只需添加选项 --recursive 即可让 git 自动克隆根存储库及其所有子模块。

git clone --recursive my_sub_project_git_url

希望对你有帮助。

【讨论】:

  • 我通常不会在 cmets 中写这个,但在这种情况下,我只需要说“天哪,伙计!太酷了,谢谢伙计!” :-)
  • 我同意@MisterSmith,Git 子模块经常回来咬你和其他人..
  • 为了简单的事情做了大量工作。
  • ':my-app' 是什么?它只提到过一次。它在语法上是什么意思?
【解决方案3】:

现在 gradle 中有一个新功能,可让您从 git 添加源依赖项。

您首先需要在settings.gradle 文件中定义repo,并将其映射到模块标识符:

sourceControl {
    gitRepository("https://github.com/gradle/native-samples-cpp-library.git") {
        producesModule("org.gradle.cpp-samples:utilities")
    }
}

如果您使用 Kotlin gradle,则需要使用 URI("https://github.com/gradle/native-samples-cpp-library.git") 而不是 "https://github.com/gradle/native-samples-cpp-library.git"

现在在你的build.gradle 中你可以指向一个特定的标签(例如:'v1.0'):

dependencies {
    ...
    
    implementation 'org.gradle.cpp-samples:utilities:v1.0'
}

或者到特定的分支:

dependencies {
    ...
    
    implementation('org.gradle.cpp-samples:utilities') {
        version {
            branch = 'release'
        }
    }
}

注意事项:

  • 需要 Gradle 4.10 或更高版本
  • 还不支持身份验证

参考资料:

【讨论】:

  • 如果这也适用于 IDE,那就太好了:我尝试在我的 android 项目(使用 Android Studio 3.4.1)中对我的一个库使用 sourceControl,但 IDE 无法做到“gradle sync”和源文件看不到库......但是如果我使用“./gradlew build”通过命令行构建一切正常。
  • 我不确定是不是只有我一个人,但我遇到了很多问题,尤其是当库有数据绑定时。一方面,gradle 脚本会引发 NPE,并且在自动构建 git 存储库时无法找到数据绑定编译器的 sdk 文件夹。 android.googlesource.com/platform/tools/base/+/… 我最终不得不提交我的 local.properties 但是我也遇到了其他问题
  • 我对此不太走运。将近 2 年后,几乎没有文档记录,只有很少的选择,没有很好的例子,而且似乎只适用于“完美”的回购。
  • 如果你的仓库是私有的怎么办?
【解决方案4】:

我认为 Gradle 不支持将 git 存储库添加为依赖项。 我的解决方法是:

  • 声明主项目依赖于文件系统中的另一个项目
  • 提供一种方法来自动克隆声明为依赖项的文件夹中的 git 存储库

我假设您希望库 repo 在主项目 repo 的文件夹之外,因此每个项目将是独立的 git repos,并且您可以独立地对库和主项目 git 存储库进行提交。

假设你希望库项目的文件夹与主项目的文件夹在同一个文件夹中,

你可以:

在* settings.gradle 中,将库存储库声明为项目,因为它在文件系统中的位置

// Reference:  https://looksok.wordpress.com/2014/07/12/compile-gradle-project-with-another-project-as-a-dependency/

include ':lib_project'
project( ':lib_project' ).projectDir = new File(settingsDir, '../library' )

使用gradle-git plugin 从 git 存储库克隆库

    import org.ajoberstar.gradle.git.tasks.*

    buildscript {
       repositories { mavenCentral() }
       dependencies { classpath 'org.ajoberstar:gradle-git:0.2.3' }
    }

    task cloneLibraryGitRepo(type: GitClone) {
            def destination = file("../library")
            uri = "https://github.com/blabla/library.git"
            destinationPath = destination
            bare = false
            enabled = !destination.exists() //to clone only once
        }

在你项目的依赖中,说你项目的代码依赖于git项目的文件夹

dependencies {
    compile project(':lib_project')
}

【讨论】:

  • Gradle 现在支持具有某些限制的源依赖项。看我的回答here
【解决方案5】:

我找到的最接近的东西是https://github.com/bat-cha/gradle-plugin-git-dependencies,但我无法让它与 android 插件一起使用,即使在 git repos 加载后仍试图从 maven 中提取。

【讨论】:

    【解决方案6】:

    @Mister Smith 的 answer 几乎对我有用,唯一的区别是,不是将存储库 URI 作为 String 传递,它需要是 URI,即:

    sourceControl {
        gitRepository(new URI("https://github.com/gradle/native-samples-cpp-library.git")) {
            producesModule("org.gradle.cpp-samples:utilities")
        }
    }
    

    我正在使用 Gradle 6.8。

    【讨论】: