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.
- 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.
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 {
url "https://plugins.gradle.org/m2/"
}
}
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.
Similar Reads
How to Send SMS in Android using Kotlin? SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article,
4 min read
Read From Files using InputReader in Kotlin Basically, kotlin.io provides a nice, clean API for reading from and writing to files. In this article, we are going to discuss how to read from files using inputReader in Kotlin. One way of doing this is by using inputreader. We will see how to do that in this article. Example There are a lot of wa
5 min read
Working with Anonymous Function in Kotlin In Kotlin, we can have functions as expressions by creating lambdas. Lambdas are function literals that is, they are not declared as they are expressions and can be passed as parameters. However, we cannot declare return types in lambdas. Although the return type is inferred automatically by the Kot
3 min read
Restricting Class Hierarchies in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Static Methods and Companion Objects in Kotlin Unlike Java, Kotlin doesn't support static methods for a class. Most readers will know that static methods do not belong to the object instance but rather to the type itself. In Kotlin, it is advisable to define methods at the package level to achieve the functionality of static methods. Let's defin
6 min read
Singleton Class in Kotlin Singleton Class in Kotlin is also called as the Singleton Object in Kotlin. Singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere. Many times we create the two different objects of the same class, but we have to remember that
4 min read
Dynamic TextView in Kotlin Android TextView is an user interface that is used to display some text to the user. In this article we will be discussing how to programmatically create a TextView in Kotlin . Step by Step ImplementationStep 1: Create a new projectLetâs start by first creating a project in Android Studio. To do so,
2 min read
Send Multiple Data From One Activity to Another in Android using Kotlin There are multiple ways for sending multiple data from one Activity to Another in Android, but in this article, we will do this using Bundle. Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs. To understand this concept we will create a simple
3 min read
How to Write swap Function in Kotlin using the also Function? also is an extension function to Template class which takes a lambda as a parameter, applies contract on it, executes the lambda function within the scope of calling object, and ultimately returns the same calling object of Template class itself. Swapping two numbers is one of the most common things
3 min read
How to Send Data Back to MainActivity in Android using Kotlin? As there are many methods to send the data, but in this article, we will use startActivityForResult() method. Here we need to launch a child activity using startActivityForResult() method. Then from child activity, we can easily send data back to Main Activity. Example: Note: To implement it in jav
2 min read