Code Formatting in Kotlin using ktlint - GeeksforGeeks

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.
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
Before this, let’s talk about some important things like ktlint breaks down into two things.
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.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.
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.
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.
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
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
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.gradleplugins {    id "org.jlleitschuh.gradle.ktlint" version "7.1.0"} // root-level build.gradlebuildscript {  repositories {    maven {      url "https://plugins.gradle.org/m2/"    }  }  dependencies {    classpath "org.jlleitschuh.gradle:ktlint-gradle:7.1.0"  }}
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.gradleallprojects {    ...    apply plugin: "org.jlleitschuh.gradle.ktlint"}
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.
clintra
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
GridView in Android with Example
How to Change the Background Color After Clicking the Button in Android?
Android Listview in Java with Example
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
Android Menus
MVP (Model View Presenter) Architecture Pattern in Android with Example