我的build.gradle
文件,staging
,stable
和production
以及默认的构建类型debug
和release
中都有味道。对于每个文件,我都有不同的AAR文件,例如,我有一个要用于stagingDebug
的AAR文件,另一个要用于stagingRelease
的文件,以及stableDebug
和所有其他变体的文件。
我可以使用releaseImplementation ('xxxx@aar')
和stagingImplementation ('yyyy@aar')
,但是我不能使用stagingReleaseImplementation ('zzzzz@aar')
有什么方法可以使用flavor + build类型特定的依赖项?
最佳答案
一种解决方案是在顶级build.gradle
文件中定义一个变量:
project.ext.BuildCmdLine = "Release"
android {
applicationVariants.all { variant ->
project.ext.BuildCmdLine= variant.buildType.name // Sets the current build type
}
}
然后,在模块的
build.gradle
文件中,您可以根据project.ext.BuildCmdLine
设置要使用的依赖项:dependencies {
if (project.BuildCmdLine == "Release") {
api(name: 'yourlib-release', ext: 'aar')
} else {
api(name: 'yourlib-debug', ext: 'aar')
}
}