代码之家  ›  专栏  ›  技术社区  ›  Naz_Jnr

如何在Gradle中获取当前构建类型

  •  24
  • Naz_Jnr  · 技术社区  · 6 年前

    我的问题很直接,也很容易理解。

    问题

    在Gradle中,是否有任何方法可以在运行时获取当前构建类型。例如,运行装配时 调试 任务,可以在生成中执行任务。gradle文件根据此任务与调试构建变量相关的事实做出决策?

    示例代码

    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
         }
    }
    

    总而言之

    有没有办法让我准确地获得 我的任务{} ?

    8 回复  |  直到 6 年前
        1
  •  20
  •   UnlikePluto    6 年前

    您可以通过分析 applicationVariants :

    applicationVariants.all { variant ->
        buildType = variant.buildType.name // sets the current build type
    }
    

    实现可能如下所示:

    def buildType // Your variable
    
    android {
        applicationVariants.all { variant ->
            buildType = variant.buildType.name // Sets the current build type
        }
    }
    
    task myTask{
        // Compare buildType here
    }
    

    您也可以检查 this this 类似的答案。

    使现代化

    This 答复人 this 这个问题帮助发问者解决了这个问题。

        2
  •  1
  •   Ashish Kshirsagar    5 年前

    这对我有用

    applicationVariants.all { variant ->
        def variantType = variant.buildType.name
        println "Variant type: $variantType"
        if (variantType == "debug") {
           // do stuff
        }
    }
    
        3
  •  0
  •   Braian Coronel    5 年前

    你应该 getBuildConfigFields().get("MY_BUILD_TYPE").getValue())

    https://stackoverflow.com/a/59994937/5279996

    德国劳埃德船级社

        4
  •  0
  •   jobbert    4 年前

    如果要将buildtype名称作为versionname的后缀(如我),只需在版本名称中添加此行:

    debug {
        versionNameSuffix "-debug"
    }
    

    这样,您可以在生成名称中标识生成类型。它可以在不声明任何其他内容的情况下工作。

        5
  •  0
  •   Jaki    3 年前

    获取在android平台Kotlin编程语言中构建期间使用的当前构建类型的正确方法(Java的逻辑相同)

    project.afterEvaluate {
       this.android().variants().all {
    
          this.assembleProvider.configure {
    
             this.doLast{
                val variant = this@all
    
                variant.outputs
                           .map
                           .forEach{
                               //do something with current buildType, or build flavor or whatever
                               println(variant.flavorName)
                               println(variant.buildType)
                           }
             }
          }
       }
    }
    
        6
  •  0
  •   Edhar Khimich    3 年前

    我以这种方式获取构建类型:

    BuildConfig.BUILD_TYPE
    

    如果需要检查当前生成类型,请在utils包中创建一个枚举类,并在If语句中使用它:

    enum class Environment(val value: String) {
       RELEASE("release"),
       LOCAL("local"),
       STAGING("staging"),
       DEBUG("debug")
    }
    

    您的if/when语句:

    if (BuildConfig.BUILD_TYPE == Environment.RELEASE.value) {
         //TODO
    } else if(...)
    

    或通过以下时间:

    when(BuildConfig.BUILD_TYPE) {
       Environment.RELEASE.value -> { //TODO }
       Environment.LOCAL.value -> { // TODO }
       // etc.
    }
    
        7
  •  0
  •   Daniil Movsesyan    3 年前

    我检查了其他答案,没有任何效果。 下面的内容将有所帮助。 在您的构建中。gradle(:应用程序):

    tasks.all { Task task ->
        if (task.name == "preDebugBuild") {
            doFirst {
                //for debug build
            }
        } else if (task.name == "preReleaseBuild") {
            doFirst {
                //for release build
            }
        }
    }
    
    dependencies {
        ...
    }
    

    请注意,当您更改构建变量时,您放入的代码不会执行,而是在您构建应用程序时执行。

        8
  •  -2
  •   Kirtikumar A.    4 年前

    您可以通过分析应用程序变体来获得确切的生成类型:

    applicationVariants.all { variant -> buildType = variant.buildType.name // sets the current build type }