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

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