Android get Flavors for a specific task - copying google-services.json for every flavor

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 dynamically for every flavor? I got 4 flavors. How to get the current flavor that is being compiled?

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 it doesn’t copy the correct file.