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

Oscar de la Hera Gomez
Two flowers that represent Kotlin and Android Studio. Beneath them sits the text "App Kotlin Gradle DSL Build Task."

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

A screenshot of Android Studio with the App build.gradle.kts file highlighted. We have also highlighted the android section of the gradle.

In Android Studio, open the build.gradle.kts, under the app folder.

Find the Android section.

Step Two: Add the build listener

A screenshot of Android Studio showing the build listener added to the gradle. This code is available below. Make sure you place it within the android section.

Within the android section, add the script below.

Step Three: Add the Task

A screenshot of Android Studio showing the build listener added to the gradle. This code is available below. Make sure you place it within the android section.

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

A screenshot of Android Studio showing the code we added above, along with the sample logs that appear for the projectEvaluated and buildFinished parts of the build listener.

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.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details