【发布时间】:2026-01-12 22:35:01
【问题描述】:
我想使用来自 mavencentral 的库的主版本。
android gradle 中是否可以将 git 仓库声明为依赖项?
【问题讨论】:
-
你找到解决办法了吗?
标签: android android-studio android-build android-gradle-plugin
我想使用来自 mavencentral 的库的主版本。
android gradle 中是否可以将 git 仓库声明为依赖项?
【问题讨论】:
标签: android android-studio android-build android-gradle-plugin
对我来说最好的方法是:
步骤 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'
}
【讨论】:
-SNAPSHOTjitpack.io/docs/#snapshots
buildscript
或者您可以像这样将存储库注册为子模块
$ 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
希望对你有帮助。
【讨论】:
':my-app' 是什么?它只提到过一次。它在语法上是什么意思?
现在 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 不支持将 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')
}
【讨论】:
我找到的最接近的东西是https://github.com/bat-cha/gradle-plugin-git-dependencies,但我无法让它与 android 插件一起使用,即使在 git repos 加载后仍试图从 maven 中提取。
【讨论】:
@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。
【讨论】: