Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-project build: execute one task before any project builds

In a multi-project Android build, like

root +--- build.gradle +--- settings.gradle +--- subproject1 \--- build.gradle \--- subproject2 \--- build.gradle

I would like to execute a task that generates code, before any of the projects (subproject1,subproject2) build. The code generating task is there once for all projects. I would like to put it into the root build.gradle. Also in the root build.gradle, that all projects (allprojects) depend on the code generating task.

task code_generating_task << {
  println "I generate code here"
}
preBuild.dependsOn code_generating_task

Does not work, because preBuild is not defined in the root build.gradle.

like image 323
Roland Puntaier Avatar asked Sep 01 '25 02:09

Roland Puntaier


1 Answers

It is fine to declare a common/transverse task directly in the root project's build script like you did. In order to create dependencies between each subproject's preBuild task and this common code_generating_task task you can write the following block in the root project build script:

gradle.projectsEvaluated {
    subprojects{
        // TODO : add a check on 'preBuild' task existence in this subproject.
        preBuild.dependsOn code_generating_task
    }
}
like image 78
M.Ricciuti Avatar answered Sep 02 '25 17:09

M.Ricciuti