Skip to content Skip to sidebar Skip to footer

Run Custom Task Before All Possible Packaging Tasks In Gradle Android Project

I have a gradle project named data-android that applies the com.android.library plugin. Before the packaging phase I want a custom task of my own (named copySqlMigrations) to be ex

Solution 1:

Gradle has a TaskContainer accessible via (project.)tasks. This container is also a TaskCollection containing all tasks. One can query a subset TaskCollection via the matching method and, thanks to some magic, the new TaskCollection is live. So, whenever a new task gets added to the parent TaskCollection (or the TaskContainer) and it matches the closure of the matching method, the subset TaskCollection will contain it. Together with the TaskCollectionall method one can handle each task following a pattern whenever it is created.

For the problem stated in your question, I wrote and tested the following build file:

task assembleX { }

task copySqlMigrations { }

task assembleY { }

tasks.matching { task ->
    task.name.startsWith('assemble')
}.all { task ->
    task.dependsOn copySqlMigrations
}

task assembleZ { }

You can call each assemble* task and it will call copySqlMigrations as a dependency. Of course, you can modify the matching closure to fit your needs.

Post a Comment for "Run Custom Task Before All Possible Packaging Tasks In Gradle Android Project"