簡體   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