1

Is there a way we could get the selected build variant on the Gradle file when we compile?

I would like to use inside the dependencies task the following code:

buildVariant = awesome code to get the buildVariant selected flag 
if (buildVariant == A)
compile project(':moduleA')
else
compile project(':moduleB')

2 Answers2

0

Click on Build Variant which is at the left bottom side of Android Studio

It has a list of all build variants available

enter image description here

Amey Jahagirdar
  • 455
  • 1
  • 4
  • 14
0

Groovy supports java code so you can use something like this:

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 "";
}
}

source: How to get current flavor in gradle

Community
  • 1
  • 1
madless
  • 84
  • 6