首页 > 解决方案 > 在不同文件中配置 Gradle 依赖项

问题描述

我正在使用位于 build.gradle (Project) 所在目录的外部 dependencies.gradle 文件。我正在使用以下命令

build.gradle (项目)

apply from: 'dependencies.gradle'

依赖项.gradle

ext {
SUPPORT_LIB_VERSION = '28.0.0'
dependencies = [
        ANNOTATIONS: "com.android.support:support-annotations:$SUPPORT_LIB_VERSION"
]}

build.gradle(应用程序)

api rootProject.ext.dependencies.ANNOTATIONS

上面的代码工作得很好。我想知道如何使用相同的方法排除组或模块?让我写下我卡住的地方

dependencies = [
        espresso: ("com.android.support.test.espresso:espresso-contrib:3.0.2") {
            exclude group: 'com.android.support', module: 'appcompat'
            exclude group: 'com.android.support', module: 'support-v4'
            exclude group: 'com.android.support', module: 'recyclerview-v7'
        }
]

我收到此错误

No signature of method: java.lang.String.call() is applicable for argument types: (dependencies_83n19kvhft5hx8evun34kydx1$_run_closure1$_closure2) values: [dependencies_83n19kvhft5hx8evun34kydx1$_run_closure1$_closure2@759d33fd] Possible solutions: wait(), any(), wait(long), take(int), any(groovy.lang.Closure), each(groovy.lang.Closure)

标签: androidgradleandroid-gradle-plugin

解决方案


如果要排除某些数据,则需要在build.gradle文件中进行:

依赖项.gradle

dependencies = [
    espresso: "com.android.support.test.espresso:espresso-contrib:3.0.2"
]

构建.gradle

testImplementation(rootProject.ext.dependencies.espresso) {
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}

因为implementation(object) { //Closure... }是一个有 2 个参数的方法:对象和动作。你可以看到这个文档。


推荐阅读