如何在 Gradle 中获取当前构建类型 [英] How to get Current Build Type in Gradle
问题描述
我的问题非常直接且易于理解.
My Question is very direct and easy to understand.
问题
在 Gradle 中,有什么方法可以在运行时获取当前构建类型.例如,在运行 assembleDebug 任务时,build.gradle 文件中的任务能否根据此任务与调试构建变体相关这一事实做出决定?
In Gradle, is there any way I can get the current build type at runtime. For example, when running an assembleDebug task, can tasks within the build.gradle file make decisions based on the fact that this task is related to the debug build variant?
示例代码
apply plugin: 'com.android.library'
ext.buildInProgress = ""
buildscript {
repositories {
maven {
url = url_here
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
configurations {
//get current build in progress here e.g buildInProgress = this.getBuildType()
}
android {
//Android build settings here
}
buildTypes {
release {
//release type details here
}
debug {
//debug type details here
}
anotherBuildType{
//another build type details here
}
}
}
dependencies {
//dependency list here
}
repositories{
maven(url=url2_here)
}
task myTask{
if(buildInProgress=='release'){
//do something this way
}
else if(buildInProgress=='debug'){
//do something this way
}
else if(buildInProgress=='anotherBuildType'){
//do it another way
}
}
总结
有没有办法让我在 myTask{} 中准确获取正在进行的构建类型?
Is there a way for me to get exactly the build type in progress within myTask{}?
推荐答案
您可以通过解析您的 applicationVariants
来获得确切的构建类型:
You can get the exact build type by parsing your applicationVariants
:
applicationVariants.all { variant ->
buildType = variant.buildType.name // sets the current build type
}
一个实现可能如下所示:
A implementation could look like the following:
def buildType // Your variable
android {
applicationVariants.all { variant ->
buildType = variant.buildType.name // Sets the current build type
}
}
task myTask{
// Compare buildType here
}
Also you can check this and this similar answers.
更新
This answer by this question helped the questioner to settle the problem.
这篇关于如何在 Gradle 中获取当前构建类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!