gradle - 如何将Android Gradle依赖项(实现,testImplementation ...)提取到项目根目录中的方法中?
问题描述
在 app-levelbuild.gradle
中,我经常包含以下依赖
dependencies {
implementation "androidx.appcompat:appcompat:$appCompatVersion"
implementation "androidx.cardview:cardview:$cardVersion"
implementation "com.google.android.material:material:$materialVersion"
implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
implementation "androidx.annotation:annotation:$androidXAnnotations"
...
}
它使该文件变得越来越长,因此我考虑将所有此类依赖项移至 project-level build.gradle
,例如(仅示例):
ext {
includeUnitTestDeps() {
implementation "androidx.appcompat:appcompat:$appCompatVersion"
implementation "androidx.cardview:cardview:$cardVersion"
implementation "com.google.android.material:material:$materialVersion"
implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
implementation "androidx.annotation:annotation:$androidXAnnotations"
}
}
(之所以到项目级而不是应用级是因为我们可能在项目中有多个模块,因此项目级是最好的地方)
然后在应用程序级别build.gradle
我们调用
dependencies {
ext.includeUnitTestDeps()
...
}
注意:我对 Groovy/Gradle 语法不太熟悉,因此不确定它是否有效(事实上我尝试过,但它不允许在 中定义这样的方法ext
)。但是,如果您知道任何解决方案,请帮助我。非常感谢。
解决方案
我能想到几种方法
1 - 将依赖项添加到所有子项目(在父项目中)
subprojects {
dependencies {
implementation 'com.google.guava:guava:23.0'
testImplementation 'junit:junit:4.12'
}
....
}
2 - 将依赖项添加到特定的子项目(在父项目中)
// this will add dependencies to project a, b, and c
// when You add a new subproject, You have to add it to here also
// If You these need dependencies available in it
configure([project(':a'), project(':b'), project(':c')]) {
dependencies {
implementation 'com.google.guava:guava:23.0'
.....
}
}
3 - 使用方法添加依赖项
//in parent
// define a method to add dependencies
// sub projects who need these dependencies will call this method
def addDependencies(subProject) {
subProject.dependencies.add("implementation", "com.google.guava:guava:23.0")
subProject.dependencies.add("implementation", "org.apache.commons:commons-lang3:3.8.1")
// add others
}
// in child
dependencies {
addDependencies(this)
// You can add other dependencies here If this child has any
}
4 - 将依赖项定义为父项中的列表
// parent
ext.appDependencies = [
[configuration: "implementation", dependency: "org.apache.commons:commons-lang3:3.8.1"],
[configuration: "implementation", dependency: "com.google.guava:guava:23.0"]
]
// child
dependencies {
rootProject.appDependencies.each {
add(it.configuration, it.dependency)
}
}
在下面的链接中有这个方法的详细解释,它使用外部文件来定义这些依赖关系。
https://hackernoon.com/android-how-to-add-gradle-dependencies-using-foreach-c4cbcc070458
您还可以组合 3. 和 4. 方法,例如定义具有依赖项的列表、调用迭代的函数并在该列表中添加依赖项。如果可以的话,我会使用第一种或第二种方法。(也可能有其他方法可以实现这一点。)
推荐阅读
- wordpress - 仅在产品类别过滤器中显示与主要类别相关的类别,而不是在 woocommerce 中显示全部
- reactjs - 页面加载不到一秒钟(带有“正在加载...”),整个页面消失
- docker - curl 在 docker 容器中不起作用
- nginx - 重定向后需要配置nginx保留url参数
- mysql - 运行 mysql_secure_installation 后 mysql 的奇怪行为
- docker - 如何运行 sleep 命令来 distroless
- json - 找不到“object”类型的不同支持对象“[object Object]”。NgFor Angular 9
- c# - 即使图像未更改,如何更新数据库记录
- batch-file - 无法将 Robocopy 与名称中带有特殊字符的文件夹一起使用
- shell - awk FPAT 在 AppleScript do shell 脚本中不起作用吗?