Given a task, recursively walk all tasks it depends on

Given a task, what is the easiest way to find all tasks that it depends on? Looking at the Gradle docs it is still not very clear to me what is the simplest approach to do this from a plugin context.

Background: I’m trying to register some generated sources to Android build variants. It appears that the Android APIs that provide dependency info are internal:

Now I am considering a more broad approach using just Gradle to walk all tasks given a variant’s build task, in order to find cross project tasks that are created by my code gen plugin. Even better, is there a Gradle API to tell me if two tasks depend on each other?

You probably want the TaskExecutionGraph.


gradle.taskGraph.whenReady { taskGraph ->
    if (taskGraph.hasTask('foo')) {
        Task bar = project.tasks['bar']
        Set<Task> barDeps = taskGraph.getDependencies(bar)
1 Like

Unfortunately it looks like getDepedencies is an @Incubating API, so the codegen library may break for users if Gradle changes its behavior. Is there a stable API that can be used to achieve this?