How to insert my task into a pre-defined build (Android build)

I am new to gradle and need some help.

I need to insert a task that will do some my own work into a pre-existing build, the Android Studio build. I want my task to be executed before an existing task in the build for example, the compileDebugJavaWithJavac task.

My thought is to modify the dependencies of the tasks to do this:

  • get the dependsOn of the compileDebugJavaWithJavac and save it to variable
  • set the dependsOn of myTask to the variable
  • set the dependsOn of the compileDebugJavaWithJavac to myTask

Is this the right way to do it in gradle?

I tried to add the below to the build file, but I get compileDebugJavaWithJavac is unknown property. However, the compileDebugJavaWithJavac can be resolved if I use it in myTask. Do I need to do something before using the compileDebugJavaWithJavac?

def origDepdencies = tasks.compileDebugJavaWithJavac.getDependsOn();
or
def origDepdencies = compileDebugJavaWithJavac.getDependsOn();

Thank you in advanced.

Can someone familiar with the topic provide some suggestion?

Thanks

Hi Jason,

You’re right that you want to modify the dependencies of the task in order to insert your task into the task graph. However, what you’ve suggested isn’t quite the right way to go about it. Here’s some sample code that does what you want.

// looks the same in Groovy and in Kotlin
tasks.register("myTask") {
    // a task action
    doLast {
        println("my task do last")
    }
}

// Android tasks are generated during configuration, so we wrap this in afterEvaluate {}
// Groovy DSL
afterEvaluate {
    tasks.named("compileDebugJavaWithJavac").dependsOn "myTask"
}

// Kotlin DSL
afterEvaluate {
    tasks.named<AndroidJavaCompile>("compileDebugJavaWithJavac") {
        dependsOn("myTask")
    }
}

Thanks Tony!

I am doing it on a simple Java project to iron out the mechanism. I have a couple of problems.

Issue 1.
I have add the below code to the build.gradle file

tasks.register(“myTask”){
doLast{
println(“my task 1”)
}
}

afterEvaluate {
tasks.named(“compileJava”).dependsOn “myTask”
}

I got the error below. I have tried surround the “myTask” with () but got the same error. It appears to me the dependsOn is not a method of the task.

BUILD FAILED in 0s
Could not find method dependsOn() for arguments [myTask] on provider(task compileJava, class org.gradle.api.tasks.compile.JavaCompile) of type org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreatingProvider_Decorated.

Issue 2.
I used IntelliJ as my IDE. I change the task name in the build.gradle file, but the task name in the Gradle view still shows the old name. I tried to refresh it, close the project, clean the project etc, but I can’t get the new name shown in the view. How do I get it refreshed?

If I set a break point and evalute the tasks.named(“compileJava”), I got the detailMessage of “Task with name ‘:compileJava’ not found in root project ‘MyProject’.”

Is my reference to the task incorrect? I have tried with “:compileJava” too, but I got the same error. When I run the build without my code, I do see the task in the console.

Task :compileJava UP-TO-DATE