Naz_Jnr
Naz_Jnr

Reputation: 580

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

Upvotes: 29

Views: 34687

Answers (10)

Won Jong Un
Won Jong Un

Reputation: 11

Try like this in your gradle : It works fine for me

//get current build all params as string
def buildParams = getGradle().getStartParameter().toString().toLowerCase();

applicationVariants.all { variant ->
    variant.outputs.all {
        def typename = variant.buildType.name.toLowerCase();

        //and check build state in all variants
        if(buildParams.contains(typename)) {
            // finally, you get what you want.
        }
    }
}

Upvotes: 0

Dumi Jay
Dumi Jay

Reputation: 1245

Here's the approach I used to detect the runtime build type without declaring any variables at build time.

def isCurrentBuildType(buildType) {
  return gradle.getStartParameter().taskNames.find({ it.endsWith(buildType) }) != null
}

print(isCurrentBuildType("Release")) //prints true if the current build type is release.

Note that the first letter should be capital (e.g. assembleRelease, bundleRelease)

Upvotes: -2

Daniil Movsesyan
Daniil Movsesyan

Reputation: 1

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.

Upvotes: 0

Edhar Khimich
Edhar Khimich

Reputation: 1654

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

Upvotes: 0

Jaki
Jaki

Reputation: 11

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

Upvotes: 0

jobbert
jobbert

Reputation: 3497

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.

Upvotes: 0

Kirtikumar A.
Kirtikumar A.

Reputation: 4204

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

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

Upvotes: -2

Ashish Kshirsagar
Ashish Kshirsagar

Reputation: 79

This worked for me

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

Upvotes: 2

Braian Coronel
Braian Coronel

Reputation: 22867

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

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

GL

Upvotes: 0

UnlikePluto
UnlikePluto

Reputation: 3369

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.

Upvotes: 23

Related Questions