Android Studio 是否可以在android gradle中将git仓库声明为依赖关系?

brccelvz  于 2022-11-16  发布在  Android
关注(0)|答案(6)|浏览(117)

我想使用mavencentral的主版本的库。
是否可以在android gradle中将git仓库声明为依赖关系?

n53p2ov0

n53p2ov01#

对我来说最好的办法是:
https://jitpack.io
步骤1.添加JitPack存储库以在存储库末尾构建.gradle:

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

步骤2.在表单中添加依赖项

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

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

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

1aaf6o9v2#

或者,您可以将储存库注册为子模块,如下所示:

git submodule add my_sub_project_git_url my-sub-project

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

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

注意上面的my-appmy-sub-project都是文件夹名称,并且所述文件夹需要包含build.gradle文件。

子文件夹支持“:“(而不是/),例如:

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

最后,将项目compile作为应用程序的build.gradle文件中的依赖项,如下所示:

buildscript {
  // ... Not here ...
}
    
// ...
    
dependencies {
  api project(':my-sub-project')

  // Or for older Gradle versions:
  // ```
  // compile project(':my-sub-project')
  // ```
}

但如果您正在文件夹中:

api project(':my-folder:my-sub-project')

另外,在克隆项目时,只需添加--recursive选项,git就会自动克隆根仓库及其所有子模块。

git clone --recursive my_sub_project_git_url

希望能帮上忙。

snz8szmq

snz8szmq3#

Gradle中现在有一个新特性,可以让您从git添加源代码依赖项。
首先需要在settings.gradle文件中定义repo,并将其Map到模块标识符:

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

如果您使用的是Kotlingradle,则需要使用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'
        }
    }
}

注意事项:

hfwmuf9z

hfwmuf9z4#

我认为Gradle不支持将git存储库添加为依赖项。

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

我假设您希望库repo在主项目repo的文件夹之外,这样每个项目都将是独立的git repo,您可以独立地提交到库和主项目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')
}
thigvfpy

thigvfpy5#

我找到的最接近的东西是https://github.com/bat-cha/gradle-plugin-git-dependencies,但我不能让它与android插件一起工作,一直试图从maven拉,即使在git repos加载。

vhipe2zx

vhipe2zx6#

@Mister Smith的answer几乎对我有效,唯一的区别是,不是以String的形式传递存储库URI,而是必须以URI的形式传递,即:

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

我用的是Gradle 6.8。

相关问题