Adding Klint to my Android apps

First of all, what is ktlint ❤️?

As it’s documentation says:

ktlint is an anti-bikeshedding Kotlin linter with built-in formatter.

Basically, is a code analysis tool for Kotlin, maintain by Pinterest.

I find this tool easy to integrate to any…


This content originally appeared on DEV Community and was authored by DEV Community

First of all, what is ktlint ❤️?

As it's documentation says:

ktlint is an anti-bikeshedding Kotlin linter with built-in formatter.

Basically, is a code analysis tool for Kotlin, maintain by Pinterest.

I find this tool easy to integrate to any Android project and also easy to maintain. Let's go to the point and start with the configuration.

First we need to create a new gradle file in my case I'm going to name it: ktlint.gradle, no need to overthink the name, but you can name it the way you wanted. Place it at the same level of your build.gradle(:app) and paste the next content:

configurations {
    ktlint
}

dependencies {
    ktlint "com.pinterest:ktlint:0.44.0"
}
task ktlint(type: JavaExec, group: "verification") {
    description = "Check Kotlin code style."
    main = "com.pinterest.ktlint.Main"
    classpath = configurations.ktlint
    args "src/**/*.kt"
}

check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    main = "com.pinterest.ktlint.Main"
    classpath = configurations.ktlint
    args "-F", "src/**/*.kt"
}

First, we can see on the dependencies section the last version available of ktlitn: as today is com.pinterest:ktlint:0.44.0.

Then we can see two gradle tasks ktlint and ktlintFormat. The first task ktlint is going to check your kotlin code and report any style issue. The second task ktlintFormat is going to fix any kotlin code style by its own.

Now, we need to add our file to our gradle scrip modules so, let go to the build.gradle(:app) and let's adding it in the plug in section. e.g.


plugins {
   .... 
}

apply plugin: "kotlin-kapt"
apply plugin: "dagger.hilt.android.plugin"
apply plugin: "androidx.navigation.safeargs.kotlin"
apply from: "../ktlint.gradle"


android {
....
}

And don't forget yo Sync your project!

How can I run it ?

To make it work, you just need to run the commands or tasks in the terminal.

./gradlew ktlint

or

./gradlew ktlintFormat

And that's it, Hope you find it helpful!

Refs.
Klint Page
Klint Github


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-04T23:32:19+00:00) Adding Klint to my Android apps. Retrieved from https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/

MLA
" » Adding Klint to my Android apps." DEV Community | Sciencx - Friday March 4, 2022, https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/
HARVARD
DEV Community | Sciencx Friday March 4, 2022 » Adding Klint to my Android apps., viewed ,<https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/>
VANCOUVER
DEV Community | Sciencx - » Adding Klint to my Android apps. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/
CHICAGO
" » Adding Klint to my Android apps." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/
IEEE
" » Adding Klint to my Android apps." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/. [Accessed: ]
rf:citation
» Adding Klint to my Android apps | DEV Community | Sciencx | https://www.scien.cx/2022/03/04/adding-klint-to-my-android-apps/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.