我设置了一个 Android 项目,以便使用模块系统作为有选择地“插件”功能的一种方式。
项目层次结构如下所示:
- 应用程序
- list
- Java
- Assets
- 资源
- 模块1
- list
- Java
- Assets
- 资源
模块2
- list
- Java
- Assets
- 资源
...
基本上我想做的是有选择地包含这些 module1
和 module2
以便我可以决定在构建时包含哪些模块。
我已经通过添加实现了这一点
编译项目(:module1)
到build.gradle
并设置settings.gradle文件如下:
包括':app', ':module1'
但真正的问题是:老实说,我不知道为什么我必须修改这两个文件。
从技术上讲,build.gradle
文件描述了模块之间的依赖关系,这意味着如果我包含 :module1,这是否意味着我将其包含在构建中?
有没有办法对单个文件进行更改,它会处理所有事情,而不是更新这两个文件?
最佳答案
您可以使您的 settings.gradle
文件动态化,然后项目只需要位于可预测的路径中。将来要包含一个新项目,您只需创建文件夹结构并添加一个 build.gradle
文件。
$ cat settings.gradle
// search for directories at the same level as the `settings.gradle` file
def moduleDirs = settingsDir.listFiles(new FileFilter() {
@Override
boolean accept(File pathname) {
// if is a directory and directory contains a `build.gradle` file add to `moduleDirs` list
return pathname.isDirectory() && pathname.listFiles(new FileFilter() {
@Override
boolean accept(File file) {
return file.name == 'build.gradle'
}
}).size() == 1
}
})
// now that we know all the modules create a project for each
// modules will define their own relationship (in their `build.gradle`)
// here we just declare the name and location of each
for (def moduleDir : moduleDirs) {
include ":$moduleDir.name"
project(":$moduleDir.name").projectDir = moduleDir
}
不完全是单一文件解决方案,但是您现在不必再次触摸 settings.gradle
。
将来如果有人想创建一个消费者,他们可以依赖构建的 jar
输出(即 maven repo 存储的 jars)或将他们的项目添加到文件结构中。他们可以从那里像往常一样在 build.gradle
projectA/build.gradle:
包含 projectA && projectB
但不包含 projectC..projectZ
// ...
dependencies {
compile project(':projectB')
}
projectC/build.gradle:
包含 projectC && projectZ
但是 projectA ||项目B
// ...
dependencies {
compile project(':projectZ')
}
关于java - 在构建时有选择地在 Android 项目中包含模块的单行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215793/