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.


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.
SubscribeDownload Open Source Kotlin Starter ProjectStep 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: