How to run a task at any stage of an app Gradle build


A step by step guide on running a task during any part of the build stage of an App Gradle Kotlin DSL in Android Studio.
Step One: Select the App Gradle

In Android Studio, open the build.gradle.kts, under the app folder.
Find the Android section.
Step Two: Add the build listener

Within the android section, add the script below.
Step Three: Add the Task

Add wherever tasks you wish to run to the relevant part of the build phase.
Please note that the Gradle only seems to detect projectsEvaluated (pre-build) and buildFinished (post-build).
Step Four: Test

Sync your Gradle, and see how the task(s) are ran.
Looking to run a task outside of the App Gradle?
If you are looking to run gradle tasks outside of the App build.gradle.kts, consult the tutorial below:
Any Questions?
We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.