Ktlint — Improve your Kotlin code quality with lint checks
As developers we always strive to ensure that our app meets its functional requirements by building tests, while this on its own is powerful it’s important we should also ensure our code has no structural problems by running the code through lint.
A lint tool helps find poorly structured code that can impact the reliability and efficiency of your app and make your code harder to maintain.
Why Lint your code base
Every developer tends to have an opinion on style. If you’ve ever worked on an open source project, you’ll probably see developers who prefer spaces over tabs, those who prefer if statements to have braces even if its a single line of code that goes there , and even while kotlin doesn’t require the semi-colon, you’ll still see those with old habits die hard.
That’s where automatic code formatting comes in. When you’re working alone on a side project it doesn’t matter whether your formatting is consistent. But try working on a codebase with more developers! Everyone has different opinions, and a lot of times these opinions come to a head in a code review, resulting in returned PRs, wasted time, and maybe even missed deadlines.
Jetbrains provides an official kotlin coding convention that contains the current coding style for the Kotlin language. But not everyone can possibly have all these rules handy at a go. And like some people still, they would just ignore it all together.
Introducing klint
Ktlint
— is an anti-bikeshedding Kotlin linter with a built-in formatter. Ktlint
tries to capture (reflect) official code style from kotlinlang.org and Android Kotlin Style Guide and then automatically apply these rules to your codebase. That way, developers can focus on shipping features, fixing bugs, and writing clean code, rather than arguing over semi-colon placement. Thus keeping your code readable and your code reviews manageable.
Setting up Ktlint for your android project
- Create a
gradle
file calledktlint.gradle
in the root directory of your project.
- Apply
ktlint.gradle
to the individual modules of the project. At the top of thebuild.gradle
file for that module add the following.
This will apply the configuration from ktlint.gradle
to that module of the project.
- You can also apply the configuration to the entire module of the project if you are working on a multi-module project. Add this to the project level
build.gradle
under theallprojects
section so it is applied to all modules of the project.
Updating an existing project to follow the style guide
If you are not already linting your project and you want to start applying the style guide to the entire project.
Install
ktlint
- From command line:
Now inside the projects root directory you can run any of the following commands on the terminal to update android studio’s formatting.
ktlint --apply-to-idea-project
# or if you want to be compliant with Android Kotlin Style Guide
ktlint --apply-to-idea-project --android
Alternatively you can configure the Android studio editor and IDE to properly format your kotlin code from here.
Hope you’re feeling productive already.
Further reading
https://ktlint.github.io/ — ktlint official website