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!
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
ktlint
tries to capture (reflect) official code style from Coding conventions guide and Android Kotlin Style Guide.plain
(+ plain?group_by_file
), json
, html
and checkstyle
reporters are available out-of-the-box. It's also easy to create your own.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.
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.
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
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 :)
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 ConsultationGet 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