Search code examples
gradleandroid-gradle-plugingradlew

Create Gradle task that contains several tasks


Is it possible to create a gradle task that runs several tasks? My goal would be to to have a command cleanAndTestAll that would be executed like:

./gradlew cleanAndTestAll

and would be the equivalent of doing:

./gradlew clean :unit:test :app:connectedAndroidTestPlayDebug

Solution

  • One way is to define a wrapper task that depends on the tasks you want to run. For example adding the following to the root build.gradle :

    task cleanAndTestAll(dependsOn: [ clean, ':unit:test', ':app:connectedAndroidTestPlayDebug']) { }
    

    This task will trigger the two other tasks. and give output like the following:

    15:31:38: Executing external task 'cleanAndTestAll'...
    :clean
    :app:connectedAndroidTestPlayDebug 
    :unit:test
    :cleanAndTestAll 
    
    BUILD SUCCESSFUL
    

    If you want to enforce an ordering between the tasks, you could do something like:

    task cleanAndTestAll(dependsOn: [clean, ':unit:test', ':app:connectedAndroidTestPlayDebug']) { }
    tasks.getByPath(':app:connectedAndroidTestPlayDebug').mustRunAfter tasks.getByPath(':unit:test')
    

    Find out more about gradle tasks at: https://docs.gradle.org/current/userguide/more_about_tasks.html