Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle get current flavor for a specific task

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?

like image 497
Nunes D. Avatar asked Sep 18 '15 17:09

Nunes D.


People also ask

How do I get the current flavor in Gradle?

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

How does Gradle know what to build?

The core model is based on tasks Once the task graph has been created, Gradle determines which tasks need to be run in which order and then proceeds to execute them. Almost any build process can be modeled as a graph of tasks in this way, which is one of the reasons why Gradle is so flexible.

What is a Gradle task?

In Gradle, Task is a single unit of work that a build performs. These tasks can be compiling classes, creating a JAR, Generating Javadoc, and publishing some archives to a repository and more. It can be considered as a single atomic piece of work for a build process.


1 Answers

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

like image 121
FallasB Avatar answered Oct 27 '22 01:10

FallasB