'How to get Current Build Type in Gradle

My Question is very direct and easy to understand.

Question

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?

Sample Code

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

In Summary

Is there a way for me to get exactly the build type in progress within myTask{}?



Solution 1:[1]

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.

Update

This answer by this question helped the questioner to settle the problem.

Solution 2:[2]

This worked for me

applicationVariants.all { variant ->
    def variantType = variant.buildType.name
    println "Variant type: $variantType"
    if (variantType == "debug") {
       // do stuff
    }
}

Solution 3:[3]

You should getBuildConfigFields().get("MY_BUILD_TYPE").getValue())

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

GL

Solution 4:[4]

If you want to suffix the buildtype name to the versionname (like me) just add this line to the version name:

debug {
    versionNameSuffix "-debug"
}

This way you can identify the build type in the build name. And it works without declaring anything else.

Solution 5:[5]

Correct way for getting the current buildType being used during build in Kotlin programming language for android platform (logic is the same for 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)
                       }
         }
      }
   }
}

Solution 6:[6]

I'm getting build type in this way:

BuildConfig.BUILD_TYPE

If you need to check what is the current build type, create an enum class in your utils package and use it in your if statement:

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

Your if/when statement:

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

or through when:

when(BuildConfig.BUILD_TYPE) {
   Environment.RELEASE.value -> { //TODO }
   Environment.LOCAL.value -> { // TODO }
   // etc.
}

Solution 7:[7]

I checked other answers, nothing works. What's below will help. In your build.gradle (:app):

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

dependencies {
    ...
}

Be aware, the code that you put inside will not be executed when you change the build variant, but when you build app.

Solution 8:[8]

You can get the exact build type by parsing your applicationVariants:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ashish Kshirsagar
Solution 3 Braian Coronel
Solution 4 jobbert
Solution 5 Jaki
Solution 6 Edgar Khimich
Solution 7
Solution 8