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à.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…