就像这段代码:
dependencies {
compile ('com.wdullaer:materialdatetimepicker:3.2.2') {
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
}
}
在android app build.gradle
文件中,当我想依赖远程库时,如何使用排除组语法排除多个组?
虽然上面的代码是正确的方法,但它有点复杂。有更简单的方法吗?
答案 0 :(得分:3)
基本上'排除'只是属于'ModuleDependency'类的方法,它接受'group'和'module'的'Map',并且没有办法传递更多。
但是,在这种情况下,您可以使用'Groovy'功能,并从列表中为每个'组'调用'ModuleDependency'上的'exclude'方法并传递当前'group'。看看下面的近似代码。
compile() { dep ->
[group1, group2].each{ group -> dep.exclude group: group }
}
答案 1 :(得分:2)
将此解决方案与Groovy combinations(笛卡尔产品)结合使用,以从所有配置中排除某些列表:
/**
* These artifacts must be excluded from all configurations and dependencies.
*/
final excludedModules = [
'module-to-exclude-1', 'module-to-exclude-2'
]
/**
* Exclude dependencies from all configurations.
* This configuration solves an issue
* when the same transitive dependency is included by different libraries.
*/
configurations {
[all, excludedModules].combinations { config, moduleToExclude ->
config.exclude module: moduleToExclude
}
}
答案 2 :(得分:1)
示例在这里
compile group: 'org.jitsi', name: 'libjitsi', version: '1.0-9-g4b85531', {
[new Tuple('org.opentelecoms.sdp', 'sdp-api'),
new Tuple('junit', 'junit')].each {
exclude group: "${it.get(0)}", module: "${it.get(1)}"
}
}