需求背景:
在日常开发中,我们多用gradle进行项目构建,有一个app项目中有两个Module,其中A是application module,B是一个library module,同事A编译依赖B,且都有debug和release两个版本。请问:如果实现A和编译版本的统一,即编译A的debug版本也编译B的debug版本,编译A的release版本也编译B的release版本。
实现效果图如下:
实现方法:
1.将所有被依赖的library的build.gradle中添加
android {
publishNonDefault true //表示该Module不使用默认配置
}
在上图中application依赖了3个,则在3个module的build.gradle中均添加这句话。
2.在application module的build.gradle中,将其所有依赖的library都进行如下添加:
dependencies {
releaseCompile project(path: ':library', configuration: 'release')
debugCompile project(path: ':library', configuration: 'debug')
}
根据上图中的示例,则在application module中添加
dependencies {
releaseCompile project(path: ':lib-zxing', configuration: 'release')
debugCompile project(path: ':lib-zxing', configuration: 'debug')
releaseCompile project(path: ':supertoasts', configuration: 'release')
debugCompile project(path: ':supertoasts', configuration: 'debug')
releaseCompile project(path: ':material-calendarview-library', configuration: 'release')
debugCompile project(path: ':material-calendarview-library', configuration: 'debug')
}
3.重新build gradle即可实现上述需求。