Execute task for all product flavors in android gradle project

Here is what I am trying to do -
I have a task which should run before android’s process<flavorName><buildType>Resources task for which dependency is set as below :

myTask >> {
// use the output of mergeResources task and do something with it before execution of process task
}

tasks.whenTaskAdded { task ->
//if task name matches process<flavorName><buildType>Resources
task.dependsOn myTask
}

This works well in case I have only one flavor. But in case of more than one flavor, this happens only for the last flavor which is added.

I want myTask to execute for all the flavors. How can I achieve this ?