android - 带有模块排除的 gradle 依赖属性

标签 android gradle

我试着写中央依赖文件对于我的多模块项目。

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' 只是映射的语法糖,本质上你调用方法 excludeMap 作为参数)

或者您可以完全更改为使您的中央定义闭包用于应用相应的依赖项,例如

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/

相关文章:

java - ListView 项目不垂直显示从数据库检索的列

android - 在 Android 5 Lollipop 上使用 USB-MTP 访问目录时,MediaScannerConnection#scanFile 将目录转换为文件

android - java.lang.NullPointerException无法解决

Android Studio 3.0 金丝雀 - 无法解析 : org. apache.httpcomponents :httpclient:4. 0.1

github - 如何通过gradle任务将jar发布到Internet上的某个地方?

java - android字符串比较问题?

java - Android 搜索界面——显示多个 SearchView

使用 `implementation`时Gradle依赖冲突

android - configurations.compile 为空

gradle - 在多项目 gradle 构建中运行所有微服务