Execute task before Android Gradle build?
Asked Answered
C

7

81

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?

Chittagong answered 30/8, 2013 at 11:59 Comment(0)
P
88

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.

Priggery answered 30/8, 2013 at 12:1 Comment(6)
how to execute batch file after APK is generated at this location "\app\build\outputs\apk" ? My requirement is like once xyz.apk generated at "\app\build\outputs\apk" location I want to move this file to "D:/Xyz" location automatically.Tritheism
The explanation for the second example is wrong. doFirst will add the closure as the first task action for the build task. Task actions are executed in the execution phase.Screwdriver
The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.Alphorn
got an error: Could not get unknown property 'preBuild' for root project in Android Studio 3.1.3. Could you show me the complete gradle files?Horseleech
Can I call this let's say before a gradlew assemble task and if so how?Dusa
Is there a way to write this using the java api instead of groovy? I am asking because I am developing a gradle plugin that should run kapt{variantName}kotlin task to process some annotations before a build happens. I have tried dependsOn but it doesn't seem to work.Puma
W
37

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
Wynne answered 18/5, 2015 at 20:43 Comment(2)
Is it possible to call this before assembleDebug?Trachyte
@Trachyte check please my answer above how to thisMonoacid
M
14

There is one more way how to do this

task myTask << {
    println "here's a task"
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
    task.dependsOn myTask 
}
Monoacid answered 20/1, 2017 at 12:47 Comment(5)
I'm trying to do this for the preBuild task but it doesn't seem to be taking.Pirn
It doesn't seem that preBuild actually comes through whenTaskAdded. I don't see it when I print the task.name in this closure.Pirn
if (task.name == 'preDebugBuild' || task.name == 'preReleaseBuild') works for me instead of preBuild.Pirn
@Pirn What is your question?Monoacid
Sorry not really a question. just a spew of ramblings. For future-readers, the whenTaskAdded mechanism does not seem to work for the preBuild task. It seems this task does not invoke this closure. I used the above examples to define dependents on preBuild, but used whenTaskAdded to add dependents on other tasks, such as validateReleaseSigning.Pirn
M
8

This is for Kotlin DSL (build.gradle.kts):

tasks.register/* OR .create */("MyTask") {
    doLast {
        println("I am the task MyTask")
    }
}

tasks.build {
    dependsOn("MyTask")
}

// OR another notation
// tasks.named("build") {
//     dependsOn(tasks["MyTask"])
// }

For more information see Gradle documentation: Adding dependencies to a task.

Milne answered 18/2, 2022 at 16:47 Comment(0)
C
4

The left shift operator << was removed in Gradle 5.

In my case I had an Android project using a Java sub project and this worked:

task myTask {
    doLast {
        println 'do it before build'
    }
}

assemble.dependsOn myTask

Regarding the initial question this should be the syntax now:

task myTask {
    doLast {
        println 'do it before build'
    }
}
build.dependsOn myTask
// or for Android
preBuild.dependsOn myTask
Cyzicus answered 2/9, 2020 at 22:20 Comment(4)
This gives me this error in gradle 7.2: could not get unknown property 'build' for root project 'core' of type org.gradle.api.ProjectMcclary
@alex-spurling What is the name of the gradle file? Is it build.gradle? Is defining `defaultTasks 'build' help?Cyzicus
Yes, the name of the gradle file is build.gradle and no, adding defaultTasks 'build' in my build file does not help.Mcclary
With lower versions it works? Maybe some different build plugins are used or the order of the inclusion is a problem.Cyzicus
S
3

In Gradle 5.4.x

// File: app/build.gradle
// See: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
task ruby(type:Exec) {
    workingDir '../'
    executable = '/usr/bin/env'
    args = ["ruby", "--version"]
}
preBuild.dependsOn ruby
Stiffen answered 27/6, 2019 at 20:39 Comment(0)
K
1

If the task to be run is already defined (for example publishToMavenLocal), you can add it into your gradle build task with:

build.dependsOn publishToMavenLocal
Kantor answered 8/5, 2019 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.