[英]Gradle subproject external library dependency based on (main) project module flavours
我有兩個模塊app和一個庫以及我使用的兩個外部maven庫。
外部庫有細微差別,並根據構建風格和maven分類器進行選擇。
(子項目/模塊)和(主模塊)都基於樣式使用相同的外部庫。
我的問題是我在編譯時無法控制/選擇子項目庫。
模塊應用:
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'
android {
productFlavors {
fOne {}
fTwo {}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:gridlayout-v7:22.2.1'
compile 'com.android.support:support-annotations:22.2.1'
// selecting the library based on the flavour
fOneCompile(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
fTwoCompile(group: 'com.xyz', name: 'SDK', version: '1.0', classifier: 'qa', ext: 'aar')
//<< the library also needs the com.xyz.SDK
fOneCompile project(path: ':library', configuration: "fOneCompile")
fTwoCompile project(path: ':library', configuration: "fTwoCompile")
}
模塊庫:
apply plugin: 'com.android.library'
android {
....
}
repositories{
jcenter()
flatDir{
dirs 'libs'
}
}
configurations {
fOne
fTwo
}
dependencies {
??? what goes here, flavours are not available ???
//the library also needs the com.xyz.SDK
fOneCompile(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
fTwoCompile(group: 'com.xyz', name: 'SDK', version: '1.0', classifier: 'qa', ext: 'aar')
}
模塊庫無法編譯,因為找不到SDK,我需要根據所編譯的樣式添加一個。
解決方案非常簡單,因為存在重復的依賴關系,並且主項目選擇了適當的依賴關系,因此在解決依賴關系時,可以在子模塊中使用“提供的”語句。
在這種情況下,可以用模塊庫(子模塊)中的“提供”來替換依賴項部分中的“編譯”語句:
模塊庫:
apply plugin: 'com.android.library'
android {
....
}
repositories{
jcenter()
flatDir{
dirs 'libs'
}
}
dependencies {
//the library also needs the com.xyz.SDK
provided(group: 'com.xyz', name: 'SDK', version: '1.0', ext: 'aar')
}
使用此配置:
defaultConfig {
publishNonDefault true
}
configurations {
fOneDebugCompile
fOneReleaseCompile
fTwoDebugCompile
fTwoReleaseCompile
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.