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
gradle. getStartParameter(). getTaskRequests(). toString() contains your current flavor name but the first character is capital.
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 { ... }
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.
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.
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.
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 "" } }
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à.
I use this
${variant.getFlavorName()}.apk
to format file name output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With