Invoke task from other task and passing some arguments?

Hi everyone,

Is it possible to call a task from other task passing some data as parameter, like methods?

Example:

def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'

task copyFlavor1() {
    def flavorName = android.productFlavors.flavor1.name
    tasks.copyFlavorGoogleServicesJson.execute()
}

task copyFlavor2() {
    def flavorName = android.productFlavors.flavor2.name
    tasks.copyFlavorGoogleServicesJson.execute()
}

task copyFlavor3() {
    def flavorName = android.productFlavors.flavor3.name
    tasks.copyFlavorGoogleServicesJson.execute()
}

task copyFlavor4() {
    def flavorName = android.productFlavors.flavor4.name
    tasks.copyFlavorGoogleServicesJson.execute()
}  
  
task copyFlavor5() {
    def flavorName = android.productFlavors.flavor5.name
    tasks.copyFlavorGoogleServicesJson.execute()
}  
   
task copyFlavorGoogleServicesJson(type: Copy) {
    // How to get the flavorName like function parameter ???
    def flavorName = android.productFlavors.flavor1.name

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

How can I pass the flavorName as parameter/argument?

Invoking task from another task is deprecated. Use task dependsOn or mustRunAfter or finalizedBy methods, instead.

For example your task copyFlavor1 can be written as follows.

task copyFlavor1(dependsOn: 'copyFlavorGoogleServicesJson')

To pass parameter is achieved by passing it from property by command parameter

gradle copyFlavor1 -PflavorName1=someValue
ext {
    flavor1 = project.hasProperty('flavorName1') ? project.property('flavorName1') : 'defaultName'
}

(I’m sorry for not having knowledge on com.android.application plugin.)

Thanks for the answer but I don’t want add a parameter to the gradle command.

I simply want to invoke copyFlavor1 that invokes copyFlavorGoogleServicesJson and set the flavorName variable.

You mean task copyFalvor3 copies files whose name comes from android.productFlavors.flavor3.name, right?

If so, create List of flavors, and iterates it with creating tasks. This way doesn’t requires passing arguments.

[android.productFlavors.flavor1,
        android.productFlavors.flavor2,
        android.productFlavors.flavor3].collect {
    it.name
}.eachWithIndex {flavorName, index ->
    task "copyFlavor${index}"(type: Copy) {
        from "${srcDir}/${flavorName}"
        // and so on
    }
}

And this should be defined inside any kind of task? or simply in the root script in the afterEvaluate maybe? Also, how to do it if the flavor names are: flavorBanana, flavorPeach, flavorApple?

So I can do easy this for other flavorNames:

[android.productFlavors.flavorBananas,
    android.productFlavors.flavorPeach,
    android.productFlavors.flavorApple].collect {
it
}.eachWithIndex {flavorName, index ->
    task "copy${flavorName}"(type: Copy) {
        from "${srcDir}/${flavorName}"
        // and so on
    }
}

Or even better, I can do it like this:

android.productFlavors.collect {
it.name
}.eachWithIndex {flavorName, index ->
    task "copy${flavorName}"(type: Copy) {
        from "${srcDir}/${flavorName}"
        // and so on
    }
}