Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current flavor in gradle

I have two product flavors for my app:

productFlavors {     europe {         buildConfigField("Boolean", "BEACON_ENABLED", "false")     }      usa {         buildConfigField("Boolean", "BEACON_ENABLED", "true")     } } 

Now I want to get the current flavor name (which one I selected in Android Studio) inside a task to change the path:

task copyJar(type: Copy) {     from('build/intermediates/bundles/' + FLAVOR_NAME + '/release/') } 

How can I obtain FLAVOR_NAME in Gradle?

Thanks

like image 843
Giulio Bider Avatar asked Jun 03 '15 13:06

Giulio Bider


People also ask

How to get flavor name in gradle?

gradle. getStartParameter(). getTaskRequests(). toString() contains your current flavor name but the first character is capital.

How do I get the current 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 { ... }

What is BuildConfig flavor?

BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig. FLAVOR.

What is a Buildtype in gradle and what can you use it for?

Discussion. A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.


2 Answers

How to get current flavor name

I have developed the following function, returning exactly the current flavor name:

def getCurrentFlavor() {     Gradle gradle = getGradle()     String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()      Pattern pattern      if( tskReqStr.contains( "assemble" ) )         pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")     else         pattern = Pattern.compile("generate(\\w+)(Release|Debug)")      Matcher matcher = pattern.matcher( tskReqStr )      if( matcher.find() )         return matcher.group(1).toLowerCase()     else     {         println "NO MATCH FOUND"         return ""     } } 

You need also

import java.util.regex.Matcher import java.util.regex.Pattern 

at the beginning or your script. In Android Studio this works by compiling with "Make Project" or "Debug App" button.

How to get current build variant

def getCurrentVariant() {     Gradle gradle = getGradle()     String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()      Pattern pattern      if (tskReqStr.contains("assemble"))         pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")     else         pattern = Pattern.compile("generate(\\w+)(Release|Debug)")      Matcher matcher = pattern.matcher(tskReqStr)      if (matcher.find()){         return matcher.group(2).toLowerCase()     }else{         println "NO MATCH FOUND"         return ""     } } 

How to get current flavor applicationId

A similar question could be: how to get the applicationId? Also in this case, there is no direct way to get the current flavor applicationId. Then I have developed a gradle function using the above defined getCurrentFlavor function as follows:

def getCurrentApplicationId() {     def currFlavor = getCurrentFlavor()      def outStr = ''     android.productFlavors.all{ flavor ->          if( flavor.name==currFlavor )             outStr=flavor.applicationId     }      return outStr } 

Voilà.

like image 166
Poiana Apuana Avatar answered Sep 23 '22 23:09

Poiana Apuana


I use this

${variant.getFlavorName()}.apk 

to format file name output

like image 29
Timtcng.sen Avatar answered Sep 22 '22 23:09

Timtcng.sen