Gradle get current flavor for a specific task

Nunes D. picture Nunes D. · Sep 18, 2015 · Viewed 10.5k times · Source

I'm trying to deal with google-services.json and different flavors. The documentation says that we need the file in the root folder.

I got a task that can easily copy the file from the flavor folder to the root folder:

task CopyToRoot(type: Copy) {
    def appModuleRootFolder = '.'
    def srcDir = 'src'
    def googleServicesJson = 'google-services.json'

    outputs.upToDateWhen { false }
    def flavorName = android.productFlavors.flavor1.name

    description = "Switches to $flavorName $googleServicesJson"
    delete "$appModuleRootFolder/$googleServicesJson"
    from "${srcDir}/$flavorName/"
    include "$googleServicesJson"
    into "$appModuleRootFolder"
}

Then, in the afterEvaluate I force

afterEvaluate {
    processFlavor1DebugGoogleServices.dependsOn CopyToRoot
    processFlavor1ReleaseGoogleServices.dependsOn CopyToRoot
}

This works only for 1 flavor (defined statically). How to do this for every flavor? I got 4 flavors. How to get the current flavor that is being compiled?

[UPDATE 1] - Also tried:

task CopyToRoot(type: Copy) {
    def appModuleRootFolder = '.'
    def srcDir = 'src'
    def googleServicesJson = 'google-services.json'

    outputs.upToDateWhen { false }
    def flavorName = android.productFlavors.flavor1.name

    android.applicationVariants.all { variant ->
        def flavorString = variant.getVariantData().getVariantConfiguration().getFlavorName()
        println('flavorString: ' + flavorString)

        description = "Switches to $flavorString $googleServicesJson"
        delete "$appModuleRootFolder/$googleServicesJson"
        from "${srcDir}/$flavorString/"
        include "$googleServicesJson"
        into "$appModuleRootFolder"
    }
 }

But this doesn't copy the correct file. Any help?

Answer

FallasB picture FallasB · Feb 3, 2016

A way to go is to create a folder named "google-services" in each flavor, containing both the debug version and the release version of the json file :

enter image description here

In the buildTypes section of your gradle file, add this :

    applicationVariants.all { variant ->
        def buildTypeName = variant.buildType.name
        def flavorName = variant.productFlavors[0].name;

        def googleServicesJson = 'google-services.json'
        def originalPath = "src/$flavorName/google-services/$buildTypeName/$googleServicesJson"
        def destPath = "."

        copy {
            if (flavorName.equals(getCurrentFlavor()) && buildTypeName.equals(getCurrentBuildType())) {
                println originalPath
                from originalPath
                println destPath
                into destPath
            }
        }
    }

It will copy the right json file at the root of your app module automatically when you'll switch build variant.

Add the two methods called to get the current flavor and current build type at the root of your build.gradle

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() ) {
        println matcher.group(1).toLowerCase()
        return matcher.group(1).toLowerCase()
    }
    else
    {
        println "NO MATCH FOUND"
        return "";
    }
}

def getCurrentBuildType() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

        if (tskReqStr.contains("Release")) {
            println "getCurrentBuildType release"
            return "release"
        }
        else if (tskReqStr.contains("generateDebug")) {
            println "getCurrentBuildType debug"
            return "debug"
        }

    println "NO MATCH FOUND"
    return "";
}

Based on this answer