ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Official Kotlin Style Guide with Ktlint

--

Hello everyone! đź‘‹ In this story I will show how to enforce the Kotlin Style Guide with Ktlint, a Kotlin Code Style Static Analysis Tool.

Ktlint will remove the work that involves following the Kotlin Style Guide.

When added to CI (in my case, CircleCI) it will also act as a safety net in case you try to merge code that isn’t following the Kotlin Style Guide.

Ktlint provides a built-in formatter task called ktlintFormat and it can also update your Android Studio formatting.

Using Ktlint will also save discussion time. From Ktlint’s GitHub.

No configuration. Which means no decisions to make, nothing to argue about and no special files to manage.

To implement Ktlint let’s start by creating a ktlint.gradle file in the root of our project.

We then add it to the top level build.gradle under the allprojects section so it is applied to all of our modules.

That’s it! Now we can run these 2 tasks from gradle:

  • ktlint will ensure that the code is following the official Kotlin Style Guide, it will fail otherwise. Remember to add it to your CI.
  • ktlintFormat will find & try to fix all of the code that isn’t following the Kotlin Style Guide.

To update Android Studio’s formatting I either install Ktlint and run this command or I update it manually.

To end with, check out the PR below 👇 to see the changes that applying Ktlint involved. Remember to add Ktlint to your CI and to format either in one go or incrementally by modules.

Remember to follow, share & hit the 👏 button if you’ve liked it! (:

GitHub | LinkedIn | Twitter

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (3)

Write a response