How to add ktlint and spotless to an Android project

Oscar de la Hera Gomez
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “ktlint and spotless.”

A step by step guide on creating a ktlint and spotless enabled Kotlin Android project in Android Studio. Open Source GitHub repository included.

Step One: Install Dependencies

A screenshot of terminal showing you how to install ktlint using homebrew.

For ktlint and spotless to work, your machine must have java installed as well as ktlint.

A | Make sure Java is installed and can be located

A screenshot of terminal showing that Java could not be located.

Your terminal will look like this if it cannot locate Java.

In Terminal, run the following line:

java --version

If you could not locate a Java runtime, run the following line to install java using brew.

brew install java; brew install oracle-jdk --cask

Once you're done, run java --version again to make sure it registered.

B | Install ktlint

A screenshot of terminal showing you how to install ktlint using homebrew.

In Terminal, run the following line:

brew install ktlint

Step Two: Upgrade Project Build Gradle

A screenshot of Android Studio showing the completed build.gradle.kts of our starter project after applying the steps below.

Please note that the spotless plugin and class path versions are not the latest and may be different in the code provided. Please use Android Studio to use the latest version available for the plugin and class path.

Open the project level build.gradle.kts and make the following changes.

A | Add the dependencies

A screenshot of Android Studio showing the build.gradle.kts file. Highlighted is the location of the file and the dependency code which is added in this step.

Please note that the spotless plugin and class path versions are not the latest and may be different in the code provided. Please use Android Studio to use the latest version available for the plugin and class path.

Add the Spotless dependency using code similar to that below.

B | Add the plugin

A screenshot of Android Studio showing the build.gradle.kts file. Highlighted is the location of the file and the plugin code which is added in this step.

Please note that the spotless plugin and class path versions are not the latest and may be different in the code provided. Please use Android Studio to use the latest version available for the plugin and class path.

Add the spotless plugin using code similar to that below

id("com.diffplug.spotless") version "6.19.0" apply false

A screenshot of Android Studio showing the build.gradle.kts file. Highlighted is the location of the file and the code that links the project to the spotless gradle which is added in this step.

Please note that the spotless plugin and class path versions are not the latest and may be different in the code provided. Please use Android Studio to use the latest version available for the plugin and class path.

Finally, add the code below to the build.gradle.kts.

This code links the project to a Spotless gradle that sets the rules, which will be created in the next step.

Complete solution

If you are looking for the complete solution provided in our starter project, use the gist below.

Step Three: Create Spotless Gradle

A screenshot of Android Studio showing the spotless.gradle file. We highlighted that it is found at the root of the project. It shows the code that is available below.

Create a new file at the root of your project called spotless.gradle and paste in the code below.

Note that there is no .kts extension.

To learn more about what you can do with this gradle file, consult the guide below.

Step Four: Sync Gradle

A screenshot of Android Studio showing the spotless.gradle file. Highlighted is the "Sync Now" button that appears on a bar, informing you that the grades must be synced in order for changes to take effect. Press the "Sync Now" button.

Press Sync Now to sync the Gradles.

Step Five: Test

A screenshot of Android Studio with the Terminal open, showing that the test of the Spotless check succeeded.

In the Android Studio Terminal, run the following line:

./gradlew spotlessApply

If it is successful it will look like the image above.

If not, an error will appear similar to the image shown below.

A screenshot of Android Studio with the Terminal open, showing that the test of the Spotless check failed along with the message that it gives - letting you know how to fix the issue.

Troubleshooting: Invalid JAVA_HOME directory?

A screenshot of Terminal in Android Studio showing the JAVA HOME Invalid Directory Error.

To fix the invalid JAVA_HOME directory, please consult the tutorial below.

Looking to add Spotless and ktlint to pre-commit git hooks?

Follow our guide below to create a pre-commit git hook script that applies spotless and ktlint to guarantee code consistency across your app.

If you wish to add it to your project so that it is accessible to your team in a way that they recieve updates, follow 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