如何在build.gradle.kts中定义变量以排除多个传递依赖项

时间:2019-01-26 18:53:32

标签: gradle kotlin gradle-kotlin-dsl

我知道在一个基于groovy的build.gradle中,您可以按照here所述定义多个排除项:

dependencies {
    def withoutStuff = { 
        exclude group: 'com.android.support', module: 'support-v4' 
        exclude group: 'com.android.support', module: 'support-v13'
        exclude group: 'com.android.support', module: 'design-v13' 
    }

    // For Material Datepicker
    compile deps.datePicker, withoutStuff
}

但是现在我们如何在基于kotlin的build.gradle.kts文件中执行此操作?

2 个答案:

答案 0 :(得分:1)

好吧,我经过大量的搜索,追踪和发现错误后才发现问题。在依赖关系上方,您可以定义:

val withoutStuff = fun ExternalModuleDependency.() {
    exclude(group = "com.android.support", module = "support-v4")
    exclude(group = "com.android.support", module = "support-v13")
    exclude(group = "com.android.support", module = "design-v13")
}

,然后在依赖关系块中可以执行以下操作:

dependencies {

    implementation(deps.datePicker, withoutStuff)
    ...
}

希望能帮助其他人,并期待其他答案。

答案 1 :(得分:1)

或者,如果您要全局设置排除对象,则可能会起作用:

// android {} 

configurations {
    implementation {
        exclude(group = "androidx.core", module = "app")
        exclude(group = "androidx.core", module = "core")
        exclude(group = "androidx.appcompat", module = "app")
        exclude(group = "com.google.firebase", module = "firebase-iid")
        exclude(group = "androidx.test", module = "monitor")
    }
}

// dependencies {}

灵感来自:https://stackoverflow.com/a/52165253/3763032