Search code examples
androidgradleandroid-gradle-pluginmulti-project

Gradle multi project recursive task for dependencies


Configuration:

I have multi project stricture like that

project1:
  implementation(1st_lvl_module1)
  implementation(1st_lvl_module2)
project2:
  implementation(1st_lvl_module1)
  implementation(1st_lvl_module2)
  implementation(1st_lvl_module3)
project3:
  implementation(1st_lvl_module2)

1st_lvl_module1:
  implementation(2nd_lvl_module1)
  implementation(2nd_lvl_module2)
1st_lvl_module2:
  implementation(2nd_lvl_module2)
1st_lvl_module3:
  implementation(2nd_lvl_module2)
  implementation(2nd_lvl_module3)

2nd_lvl_module1
2nd_lvl_module2
2nd_lvl_module3

Issue:

I want execute some task for all projects (eg. gradle test). It works as required for top level call. But I want to execute it for each project independently and here I have an issue.

If I do call for gradle project1:test it will be executed only for project1 and does't include 1st_lvl_module1 which also implemented 2nd_lvl_module1 and 2nd_lvl_module2 and 1st_lvl_module2 with 2nd_lvl_module2


  • I tried to implement custom task E.g
tasks.register("testWithDependencies") { task ->
        task.dependsOn("test")
        configurations.forEach {
            it.dependencies.findAll { it instanceof ProjectDependency }.forEach {
                dependsOn ":${it.name}:test"
            }
        }
    }

And with this way it works as well for first level implimentations. gradle project1:testWithDependencies will execute test task for project1, 1st_lvl_module1 and 1st_lvl_module2 but still ignore 2nd_lvl_module1 2nd_lvl_module2.

  • If I do execute for this task eg.
tasks.register("pd") { task ->
    configurations.forEach {
        println("Config name: ${it.name}")
        it.dependencies.findAll { it instanceof ProjectDependency }.forEach {
            def depProject = ((ProjectDependency)it).getDependencyProject()
            println("${depProject.name}")
            depProject.configurations.forEach {
                println("---Config name: ${it.name}")
            }
        }
    }
}

My project1 contains implementation configuration but all 1st_lvl_module* doesn't. Actually configurations list for submodules looks very poor.


Question:

Does someone have the same issue with multi module subproject structure? Or maybe easiest way for recursive call is exist?


Solution

  • Solution: File eg. testWithDependency.gradle with follow content:

    tasks.register("testWithDependencies") { task ->
            task.dependsOn("test")
            configurations.forEach {
                it.dependencies.findAll { it instanceof ProjectDependency }.forEach {
                    dependsOn ":${it.name}:testWithDependencies"
                }
            }
        }
    

    Should be applied in the end (when dependencies of project will be validated) of gradle.build for each project and module.

    This solution also can be scalable for multiple tasks.