我的build.gradle文件,stagingstableproduction以及默认的构建类型debugrelease中都有味道。对于每个文件,我都有不同的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')
    }
}

08-18 11:29
查看更多