gradle - 如何将Android Gradle依赖项(实现,testImplementation ...)提取到项目根目录中的方法中?

标签 gradle dependencies

在应用级 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"
  ...
}

它使该文件变得越来越长,因此我考虑将所有此类依赖项移至项目级别 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. 方法,例如定义具有依赖项的列表、调用迭代的函数并在该列表中添加依赖项。
如果可以的话,我会使用第一种或第二种方法。 (也可能有其他方法可以实现这一点。)

关于gradle - 如何将Android Gradle依赖项(实现,testImplementation ...)提取到项目根目录中的方法中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55418892/

相关文章:

android - 无法使用ContentResolver查询方法,NoSuchMethodError

javascript - D3 依赖轮未正确渲染

android - 构建 gradle : Could not find method packagingOptions() for arguments root Project "fasterDev"

c# - 在我的项目中查找 namespace 依赖项的工具?

java - 使用maven项目测试另一个项目

java - 如何防范 Play Framework 中的交叉构建注入(inject)和其他依赖管理威胁?

gradle - 在gradle的buildscript中访问属性

java - MACOSX - java.lang.NoSuchMethodError : org. apache.http.impl.conn.CPool.setValidateAfterInactivity(I)V

gradle - 将生成的第三方许可证复制到 Assets

android - 更新Android Studio RC 2后出现Gradle Build错误-android-sdk-linux'不存在