Configuring and running Ktlin on Android Studio
Hello Folks!! The goal of this post is help you to configure and run locally the static code analysis called Ktlint on Android Studio.
This post has the premise that you are using Gradle and Kotlin as your primary programming language.
According to the official Ktlint’s website, it is an anti-bikeshedding Kotlin linter with a built-in formatted that contains the following features to help you to format and organize your code style.
Features
- No configuration. Which means no decisions to make, nothing to argue about, and no special files to manage.
While this might sound extreme, keep in mind thatktlint
tries to capture (reflect) official code style from kotlinlang.org and Android Kotlin Style Guide. - Built-in formatter. So that you wouldn’t have to fix all style violations by hand.
- Customizable output.
plain
,json
andcheckstyle
reporters are available out-of-the-box. It's also easy to create your own. - A single executable jar with all dependencies included.
I got the above content from the official Ktlint website into features section .
Finally, If you need more information about the benefit of using Ktlint you should go to the official link and checks it out all greats features provided by it.
Contextualization
This post is going to be separated into three sections. The first one is going to lead you to configure the ktlint plugin into Android Studio, the second one to format your code and the third to run and found out issues reported by it.
Configuring the Ktlint
To achieve this goal let’s go use the plugin jlleitschuh/ktlint-gradle once it creates automatically all basic tasks to run Ktlint into Gradle.
First of all, you need to configure the classpath in top-level build.gradle file to use the plugin jlleitschuh/ktlint-gradle.
classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"
Don’t forget to check it out the latest version of plugin on the official website.
Then, still on the top-level build.gradle file you need to add the repository maven as a dependency of the project.
repositories {
...
maven {
url "https://plugins.gradle.org/m2/"
}
}
Next, still on the top-level build.gradle file you must apply this dependency to all modules into allProjects tag.
allprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint"
...
}
Finally, you must sync your project on Android Studio to get all new dependencies. To do it you can go to File->Sync project with gradle files
on Android Studio or click on the sync shortcut button as will be shown in the next picture.
Automatically formatting the code by klint task
To achieve this goal you must run the task ktlintFormat on the terminal and check it out the results.
./gradlew ktlintFormat
I’d recommend you to run this task before go to the next section as it going to try to format your code using ktlint rules.
If you had any problems after ran this task, then you can use the parameter stacktrace to get more details about the error.
./gradlew ktlintFormat --stacktrace
Checking code issues by ktlint task
To check if exists any issue regarding ktlint, you must run the task ktlintCheck on your terminal.
./gradlew ktlintCheck
If you had any problems after ran this task, then you can use the parameter stacktrace to get more details about the error.
./gradlew ktlintCheck --stacktrace