Android: Code formatting in Kotlin with Ktlint

Tram Ho

When we talk about lint, many developers think it is related to the code style guide. Wow !, but not only that

Linting is the process of analyzing code for potential errors.

As an Android developer, we use the kotlin language to develop our applications, but how to make our code clean and easy to read? There may still be many of us who are still influenced by Java Code Style or some who follow the Kotlin Style Guide

Ktlin may be our assistant, it was created to be a standard Kotlin writing format with simplicity, expansion, and an active community. So how do we use Ktlin in our project?

We should read the documentation on Ktlint , there are many ways to install Ktlint, in this article I just introduced 1 way


Add Plugin for your build.grade.kts

Then set up the properties in Ktlin

Add ktlin to the subproject

Detailed examples you can refer to the following example


After confige is complete you can open the terminal and run the command ./gradlew ktlintCheck to see the error warnings found

When running ./gradlew ktlintCheck it will display some formatting errors. Not only that, it will show you the exact error you encountered, you can automatically fix the error by running the command: ./gradlew ktlintFormat


Share the news now

Source : Viblo