繁体   English   中英

gradle依赖属性与模块排除

[英]gradle dependency property with module exclude

我尝试为我的多模块项目编写中央依赖文件

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值,然后使用这些字符串。

例如,您可以代替Map<String, String>定义Map<String, List>并在列表中将依赖项放在第一位,在第二位将excludes作为List<Map> ,然后遍历该列表,添加不包括在内。 group: 'com.android.support', module: 'support-annotations'仅是地图的语法糖,从本质上讲,您使用Map作为参数调用方法exclude

或者,您可以完全更改以制作用于应用各个依赖项的中央定义闭包,例如

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

或类似的东西。

通过添加中央依赖文件解决了我的问题

ext {   
   espressoVersion = '3.0.1'

   testingDependencies = [
        espresso: "com.android.support.test.espresso:espresso-core:${espressoVersion}"
   ]
}

和在模块中

androidTestCompile(testingDependencies.espresso, {
    exclude group: 'com.android.support', module: 'support-annotations'
})

所以我仍然有一个文件使用了依赖版本,并且可以在所有模块中简单地更新版本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM