Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Code Formatting in Kotlin using ktlint

  • Last Updated : 04 Jan, 2022

As we all know about the kotlin language which is recommended by google particularly for android app development, and of course, it made the life of android app developers easier. But if you are a beginner in this field then you may not know that you need to write the codes in the desired format. and it is definitely helpful for developers, but if you are not familiar with how to write clean code using Kotlin then don’t worry you need not learn something to achieve this, instead, you just have to use ktlint that is used for the same purpose.

ktlint: ktlint is an anti-bikeshedding Kotlin linter with a built-in format. In simple words, it checks our code styling and also helps us to format our code and make it better for understanding.

The benefits of using it

  • Ktlitn can save your time
  • It can save your energy (because you don’t have to manually check your code styling)
  • It can simplify your process

What can we do using ktlint?

Before this, let’s talk about some important things like ktlint breaks down into two things.

  1. linting tool: The lining tool is based on the kotlin standard style guide, and it will validate and make sure that your code adheres to that style guide.
  2. formatter: If ktlint detects that there are issues in your code you can then run the formatter and have ktlint try to automatically fix those issues for you.

How can we implement it?

  • Add ktlint to your project and more specifically we are going to be integrating ktlint with the ktlint Gradle plugin, this is a separate third party plugin on top of the basic ktlint tool and it makes working with ktlint very easy by providing out of the box Gradle tasks with, which to run ktlint commands.
  • There are several options, but the easiest way is to make use of the ktlint-Gradle plugin which provides out of the box Gradle tasks for ktlint’s tools

Adding ktlint to your Android project

  • For Adding the ktlint-gradle plugin just Add the ktlint-gradle plugin to your root-level build.gradle file
  • For Apply the plugin to subprojects just Apply the ktlint-gradle plugin to any Gradle modules that you would like to check
  • Verify added Gradle tasks as Check that ktlintCheck and ktlintFormat tasks have been added for your various build targets

Adding the ktlint-gradle plugin

If using a version of Gradle which supports the plugins DSL, you can add ktlint-gradle to your project with the following code:

Kotlin




// root-level build.gradle
plugins {
    id "org.jlleitschuh.gradle.ktlint" version "7.1.0"
}
 
// root-level build.gradle
buildscript {
  repositories {
    maven {
    }
  }
  dependencies {
    classpath "org.jlleitschuh.gradle:ktlint-gradle:7.1.0"
  }
}

Apply the ktlint-Gradle plugin to all subprojects

You’ll want to apply the ktlint-gradle plugin to the various modules within your project. You can do this using the allProjects{} block in the root-level build.gradle file.

Kotlin




// root-level build.gradle
allprojects {
    ...
    apply plugin: "org.jlleitschuh.gradle.ktlint"
}

Test added Gradle tasks 

Finally, you’ll want to test that the ktlint Gradle tasks are now available for use. You can do it in these ways

  • run ./gradlew tasks from the command line and look for any ktlint tasks
  • try to run ./gradlew ktlintCheck from the command line
  • use the Gradle tool window in IntelliJ or Android Studio to see if the tasks are listed

To actually check your code’s formatting, run the following command from the command line: 

./gradlew ktlintCheck

This will run through your project and report back any errors which are found using the default ktlint-gradle plugin configuration.

To automatically fix any errors which are reported by ktlintCheck, you can run the following command from the command line: 

./gradlew ktlintFormat

This will attempt to auto-fix any errors and will report back any issues that could not automatically be fixed.

When everything is correctly formatted, you should see something like the following image when you run ktlintCheck. ktlint is an open-source project that is maintained by Pinterest and you can find more info. Click here.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!