How to get Current Build Type in Gradle

18,275

Solution 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

This worked for me

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

Related videos on Youtube

Naz_Jnr
Author by

Naz_Jnr

Updated on June 04, 2022

Comments

  • Naz_Jnr
    Naz_Jnr almost 2 years

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

  • user1841702
    user1841702 almost 4 years
    where did you execute this, in app/build.gradle?
  • Ashish Kshirsagar
    Ashish Kshirsagar almost 4 years
    Yes. It needs to be in app level gradle.
  • Stealth Rabbi
    Stealth Rabbi almost 4 years
    I run this, and I get an error: ` Could not get unknown property 'applicationVariants' `
  • Ashish Kshirsagar
    Ashish Kshirsagar almost 4 years
    I am not sure what is your app gradle looks like. I suspect it can be due to lower gradle version or you may be executing this for a library. For library use 'libraryVariants' instead of 'applicationVariants'. Gradle supports such extensions. More info here google.github.io/android-gradle-dsl/current/…
  • chitgoks
    chitgoks about 3 years
    is it possible to get the flavor name in the root level build.gradle?