Gradle get current flavor for a specific task
Asked Answered
E

3

9

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?

Encomiastic answered 18/9, 2015 at 17:30 Comment(1)
@CommonsWare any alternative solution? How to do it dynamically?Encomiastic
G
5

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

Gomorrah answered 3/2, 2016 at 9:27 Comment(0)
E
1

I found my answer. Google finally supports different google-services.json per flavor. You just need to update the plugin to com.google.gms:google-services:2.0.0. There's no need to copy the json file to the app folder, just put your google-services.json different files inside your build flavors directories.

Encomiastic answered 22/5, 2016 at 23:11 Comment(0)
G
0
def getCurrentProductVariant() {
    def yourTaskName = "XXXX"
    def taskName = project.gradle.startParameter.taskNames.find { it.contains(yourTaskName) }
    if (taskName != null) {
        def variantParts = taskName.split(/(?<!^)(?=[A-Z])/)[-2..-1].collect { it.toLowerCase() }
        return new ProductVariant(buildType: variantParts[1], productFlavor: variantParts[0])
    }
    return null
}

class ProductVariant {
    String buildType
    String productFlavor
}
Geomancer answered 11/7, 2023 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.