Gradle includeBuild 与实施项目

Gradle includeBuild vs implementation project

Gradle 构建系统中 includeBuild(...)implementation(project(...)) 之间的主要区别是什么?阅读文档后我真的看不出用例的区别:

  1. https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:project_dependencies
  2. https://docs.gradle.org/current/userguide/composite_builds.html#separate_composite

我想做的是:为两个单独的项目共享相同的代码库:数据 类 (kotlix.serialization)、外部数据库 dao、接口。它不是一个完整的库,只是一些代码片段。

如何在 Intellij 中连接两个项目以便类型提示起作用?

我也遇到了同样的问题。阅读第一个 link,下一段说:

Local forks of module dependencies

A module dependency can be substituted by a dependency to a local fork of the sources of that module, if the module itself is built with Gradle. This can be done by utilising composite builds. This allows you, for example, to fix an issue in a library you use in an application by using, and building, a locally patched version instead of the published binary version. The details of this are described in the section on composite builds.

所以,按照我的说法应该是实施方案。

P.S。代码完成在我的一个子项目上工作,但在另一个子项目上没有。我仍在努力弄清楚

复合构建(通过使用includeBuild)是一种在自治Gradle项目之间创建依赖关系的方法。
项目导入,是一种在同一 Gradle 项目中的两个模块之间创建依赖关系的方法。

Composite Build 更强大,也意味着在多个项目之间分解 gradle 配置的新方法,而您传统上使用 buildSrc 技术。 我找到了这个 "Structuring Large Projects" article to be easier to read than the "Composite Builds" 文档。

可以在 Gradle sample_structuring_software_projects.

中找到一个展示复合构建功能的优秀示例项目

项目依赖案例

树看起来像这样:

settings.gradle.kts
module1/build.gradle.kts
module2/build.gradle.kts

并且您正在像这样在 module1/build.gradle.kts 中声明依赖关系:

dependencies{
   implementation(project("com.domain:module2))
}

只有当两个项目都声明为公共根项目的子模块时,才会解决依赖关系。

这意味着你有一个像这样的根 settings.gradle.kts :

rootProject.name = "rootProject"
include(":module1")
include(":module2")

复合构建案例

项目不需要有共同的“保护伞”根项目。 每个项目都是一个完全独立的项目。

一个项目可以简单地声明对另一个项目的依赖(甚至目标项目都不知道)。

树:

project1/build.gradle.kts
project1/settings.gradle.kts
project2/build.gradle.kts
project2/settings.gradle.kts

project1/settings.gradle.kts中:

rootProject.name = "project1"
includeBuild("../project2") //No more ':' as it is not a module

project2/settings.gradle.kts中:

rootProject.name = "project2"

project1/build.gradle.kts 中像这样:

dependencies{
   implementation("com.domain:project2")
}