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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/08/2023 at 13:29
Last Updated on 06/08/2023 at 15:36
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.

SubscribeDownload Open Source Kotlin Starter Project

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.

Learn more about the Gradle Build Listener

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.

How to sync an Android project with its Gradle files in Android Studio

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:

How to run a Gradle Kotlin DSL pre-build task in Android Studio

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe