Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 224
Naz_Jnr Avatar asked May 04 '18 16:05

Naz_Jnr


People also ask

How do I find the build type in Gradle?

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

How do I get value from build Gradle?

If you're creating the original value using resValue then you don't have to read it back from resources. You already have the value in your build. gradle script. Just assign it to a local variable and use it to create a resource for each variant.

What is Flavordimensions?

A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant. In your case, you must define one flavorDimension named "type" and another dimension named "organization".

How do you use productFlavors?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.


Video Answer


2 Answers

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.

like image 132
UnlikePluto Avatar answered Oct 11 '22 06:10

UnlikePluto


This worked for me

applicationVariants.all { variant ->
    def variantType = variant.buildType.name
    println "Variant type: $variantType"
    if (variantType == "debug") {
       // do stuff
    }
}
like image 28
Ashish Kshirsagar Avatar answered Oct 11 '22 04:10

Ashish Kshirsagar