基于(主)项目模块风格分级子项目外部库依赖关系



我有两个模块,一个是应用程序和一个我使用的两个外部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和I需要包括一个基于正在编译的味道。

使用此配置:

defaultConfig {
       publishNonDefault true
}
configurations {
    fOneDebugCompile
    fOneReleaseCompile
    fTwoDebugCompile
    fTwoReleaseCompile
}

解决方案非常简单,因为存在重复的依赖项和主项目选择适当的依赖项可以使用"提供的"语句在解析依赖关系时插入子模块。

在这种情况下,可以替换依赖项中的"compile"语句模块库(子模块)中带有"提供"的部分:

模块库:

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')
}

最新更新