Execute Task Before Android Gradle Build

Execute task before Android Gradle build?

You can do it in this way:

task build << {
println 'build'
}
task preBuild << {
println 'do it before build'
}
build.dependsOn preBuild

Thanks to that task preBuild will be automatically called before build task.

If you want to run preBuild in configuration phase (previous example run preBuild in execution phase) you can do it in this way:

task build << {
println 'build'
}
build.doFirst {
println 'do it before build'
}

More about gradle build lifecycle can be read here http://www.gradle.org/docs/current/userguide/build_lifecycle.html.

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

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
}
}

Run task before compilation using Android Gradle plugin

The proper way to run a task before Java compilation on Android is to make a compilation task for each variant depend on your task.

afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(generateSources)
}
}

Creating a task that runs before all other tasks in gradle

You can make every Task who's name is NOT 'initializer' depend on the 'initializer' task. Eg:

task initializer {
doLast { println "initializer" }
}

task task1() {
doLast { println "task1" }
}

// make every other task depend on 'initializer'
// matching() and all() are "live" so any tasks declared after this line will also depend on 'initializer'
tasks.matching { it.name != 'initializer' }.all { Task task ->
task.dependsOn initializer
}

task task2() {
doLast { println "task2" }
}

Or you could add a BuildListener (or use one of the convenience methods eg: Gradle.buildStarted(...))



Related Topics



Leave a reply



Submit