Android - Integrate ktlint check with Git CI

Automate lint checks on Git CI with ktlint, a Kotlin code analysis tool by Pinterest. Streamline your coding process!
Jan 5 2022 · 2 min read

Introduction 

This article is part of the series related android git CI/CD setup. So far we have learned to configure Git CI for unit testing, generate build and bundle, automate Jacoco code coverage verification, and also how to automate app deployment on play store using Fastlane.

In this article, we’re going to add ktlint project by automating lint check on Git CI.

Part 1: Android Git CI setup

Part 2: Android code coverage using Jacoco

Part 3: Android continues integration using Git CI and Fastlane

We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!

Let's Get Started!

Ktlint is a static code analysis tool maintained by Pinterest. Linter and formatted for Kotlin code.

An anti-bikeshedding Kotlin linter with built-in formatter

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 that ktlint tries to capture (reflect) official code style from Coding conventions guide and Android Kotlin Style Guide.
  • Built-in formatter. So that you wouldn’t have to fix all style violations by hand.
  • Customizable output. plain (+ plain?group_by_file), json, html and checkstyle reporters are available out-of-the-box. It's also easy to create your own.
  • A single executable jar with all dependencies included.

All Right, Let’s get started

We’re going to use jlleitschuh/ktlint-gradle plugin to run Ktlint into Gradle.

In your root-level build.gradle file add following line

plugins {
    id 'org.jlleitschuh.gradle.ktlint'
}

Sync your project to get all new dependencies.

ktlintCheck

To check if your code has some formatting errors or not, open the terminal and write the below command:

./gradlew ktlintCheck

When you run this command you may find warning like this

/home/settings/Settings.kt:110:33 Missing space after //
/home/settings/Settings.kt:113:33 Missing space after //
/home/settings/Settings.kt:118:21 Missing space after //
/home/settings/Settings.kt:121:9 Missing newline before ")"
/home/settings/Settings.kt:134:1 Unexpected blank line(s) before "}"

You can check a few standard rules of ktlint check here.

ktlintFormat

Run the below command in the terminal to fix formats and lint errors which we found by running the previous command.

./gradlew ktlintFormat

Also, some errors can not be fixed automatically, so you have to fix them manually.

Now Let’s automate ktlint check by integrating it in our Git CI.

stages:
  - test
  - lint-check
    ...
lint-check:
  stage: lint-check
  before_script:
    - chmod +x ./gradlew
    - export GRADLE_USER_HOME=$PWD/.gradle
  script:
    - ./gradlew ktlintCheck

Conclusion 

Pretty Simple!! Isn’t it?

So, this is how you can use ktlint in your project for code formatting by integrating it in Git CI to automate ktlint check.

In the next article, we’ll learn how to add custom ktlint rules and how to automate lint formatting on Git CI. Stay tuned…

Thanks for your support!

Keep Linting, Keep Learning :)


Code, Build, Repeat.
Stay updated with the latest trends and tutorials in Android, iOS, and web development.
radhika-s image
Radhika saliya
Mobile App Developer | Sharing knowledge of Jetpack Compose & android development
radhika-s image
Radhika saliya
Mobile App Developer | Sharing knowledge of Jetpack Compose & android development
canopas-logo
We build products that customers can't help but love!
Get in touch
background-image

Get started today

Let's build the next
big thing!

Let's improve your business's digital strategy and implement robust mobile apps to achieve your business objectives. Schedule Your Free Consultation Now.

Get Free Consultation
footer
Follow us on
2024 Canopas Software LLP. All rights reserved.