如何包含基于构建环境变量的库依赖项?

问题描述 投票:0回答:1

我想在 gradle 构建文件中导入 Android 依赖项,并且我想为我的所有环境构建变体定义一个版本。

dependencies {
    implementation "com.example.example:test:$version"
}

但是我想根据构建环境变体为此依赖项定义一个特定版本,例如:

if (variant == "Eval") {
    version = '1.0.6'
} else if (variant == "Integration") {
    version = '1.0.7'
} else {
    version = '1.0.8'
}

如何实现此功能?

我已经尝试过这个,但不起作用:

android {
    buildTypes {
        applicationVariants { variant ->
            def variantName = variant.name
            if (variantName == "Eval") {
                version = '0.1.6'
            } else if (variantName == "Integration") {
                version = '0.1.7'
            }
        }
    }
}
dependencies {
    implementation "com.example.example:test:$version"
}
android gradle build environment-variables build.gradle
1个回答
0
投票

我创建自定义构建变体或自定义产品风格。我可以使用

${variantName}Implementation

android {
    buildTypes {
        eval1 {}
        eval2 {}
    }
}


dependencies {
    eval1Implementation("com.example.example:test:1.0.0")
    eval2Implementation("com.example.example:test:2.0.0")
}

© www.soinside.com 2019 - 2024. All rights reserved.