我试着写中央依赖文件对于我的多模块项目。
ext {
supportVersion = '25.4.0'
junitVersion = '4.12'
supportDependencies = [
design: "com.android.support:design:${supportVersion}",
fragment: "com.android.support:support-fragment:${supportVersion}",
recyclerView: "com.android.support:recyclerview-v7:${supportVersion}"
]
...
}
在模块中使用它们
compile supportDependencies.design
compile supportDependencies.fragment
compile supportDependencies.recyclerView
但有些依赖项有
exclude
待编译androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
当我尝试写属性时
testingDependencies = [
espresso: ('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
]
我收到编译时语法错误
是否可以在要在模块中编译的扩展中创建具有排除功能的库属性?
最佳答案
当然你会得到一个语法错误。这是您必须使用的 Groovy 语法。在您的第一个示例中,您只需定义一个 Map
String
String
的 key 值,然后使用这些字符串。
你可以e。 G。而不是 Map<String, String>
定义一个 Map<String, List>
并在列表中将依赖项放在首位,将排除项放在第二位为List<Map>
然后遍历该列表,添加排除项。 ( group: 'com.android.support', module: 'support-annotations'
只是映射的语法糖,本质上你调用方法 exclude
用 Map
作为参数)
或者您可以完全更改为使您的中央定义闭包用于应用相应的依赖项,例如
testingDependencies = [
espresso: {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
exclude group: 'com.android.support', module: 'support-annotations'
}
}
]
configure dependencies, testingDependencies.espresso
或类似的东西。
关于android - 带有模块排除的 gradle 依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968157/