如何将Android Gradle依赖项(实现、测试实现…)提取到项目根目录中的方法中?
在app-level如何将Android Gradle依赖项(实现、测试实现…)提取到项目根目录中的方法中?,gradle,dependencies,Gradle,Dependencies,在app-levelbuild.gradle中,我经常包含以下依赖项 dependencies { implementation "androidx.appcompat:appcompat:$appCompatVersion" implementation "androidx.cardview:cardview:$cardVersion" implementation "com.google.android.material:material:$materialVersion"
build.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"
...
}
//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
}
它使该文件变得越来越长,因此我考虑将所有这些依赖项移动到项目级build.gradle
,例如(仅举一个例子):
(之所以选择项目级而非应用程序级,是因为项目中可能有多个模块,因此项目级是最佳位置)
然后在应用程序级别build.gradle
中调用
dependencies {
ext.includeUnitTestDeps()
...
}
注意:我不太熟悉Groovy/Gradle语法,因此不确定它是否有效(事实上我尝试过,但它不允许在
ext
中定义这样的方法)。但是如果你知道任何解决办法,请帮助我。非常感谢。我可以想出两种方法
1-将依赖项添加到所有子项目(在父项目中)
2-将依赖项添加到特定子项目(在父项目中)
3-使用方法添加依赖项
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"
...
}
//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-将依赖项定义为父项中的列表
subprojects {
dependencies {
implementation 'com.google.guava:guava:23.0'
testImplementation 'junit:junit:4.12'
}
....
}
// 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'
.....
}
}
// 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)
}
}
下面的链接详细解释了此方法,该链接使用外部文件定义这些依赖项
你也可以组合3。四,。方法,比如定义一个具有依赖项的列表,调用一个迭代并在该列表中添加依赖项的函数。
如果可以,我会使用第一种或第二种方法。(也可能有其他方法来实现这一点。)谢谢@Silver裹尸布。你的解决方案真的很有帮助。这就是我一直在寻找的。