Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute task before android gradle build?

Tags:

android

gradle

is it possible for Gradle to execute a task before calling

gradle build 

Something like precompile. Someone please help. Is something like this possible and how?

like image 753
HellOfACode Avatar asked Aug 30 '13 11:08

HellOfACode


People also ask

Does Gradle build run all tasks?

You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

What does Gradle use to determine the order in which task can be run?

Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.


2 Answers

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.

like image 134
pepuch Avatar answered Sep 21 '22 17:09

pepuch


For those who are wondering how to do this in an Android project, this worked for me:

task myTask << {   println "here's a task" } preBuild.dependsOn myTask 
like image 28
nlawson Avatar answered Sep 17 '22 17:09

nlawson