← Back to list

Ktlint pre-commit hooks and pre-build tasks Android Part 3/3

In this article we will learn to automate Ktlint Checks with Git Hooks and Gradle pre-build tasks: Avoiding Code Style Violations in Code…

KRISHNA K · 2025-12-27 13:13 · 2 claps · 1.5 min read
#android #android-app-development #kotlin #ktlint #git
Open on Medium ↗
Wiki topics: 📱 · Mobile Development 🔓 · Open Source 👗 · Fashion

Ktlint pre-commit hooks and pre-build tasks Android Part 3/3

Integrating Ktlint in precommit hook

Integrating Ktlint in precommit hook

In this article we will learn to automate Ktlint Checks with Git Hooks and Gradle pre-build tasks: Avoiding Code Style Violations in Code Reviews

If you want to jump into the code here is the link to repository with all the workflows 📦 GitHub: AndroidCICD-Mastery

https://github.com/wi-krishna-kudari/github-fastlane-ci-cd

If you haven’t setup Ktlint in your Android Project Follow this 4 min guide to set up Complete Ktlint Github Action workflow.

[embed]Automated Code Quality with Ktlint in GitHub Actions: A Production Guide — Android CI/CD Mastery… Series: Android CI/CD Mastery | Part 1 of 10medium.com

Setting up Ktlint Pre commit hook:

Option 1: using Ktlint built in task

apply the ktlint gradle plugin at the root of your project by removing the ***apply false build.gradle.kts***

plugins {
    ...
    id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
}

now define ktlint options in app/build.gradle.kts

ktlint {
    version.set("1.5.0") // Use Ktlint version, not plugin version
    android.set(true)
    outputToConsole.set(true)
    ignoreFailures.set(false)

    reporters {
        reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.PLAIN)
        reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.CHECKSTYLE)
    }

    filter {
        exclude("**/generated/**")
    }
}

run: ./gradlew addKtlintCheckGitPreCommitHook

Option 2: using pre-commit hook package ( mac os )

install pre-commit package from homebrew

run:brew install pre-commit

create a file names pre-commit-config.yaml

repos:
  - repo: local
    hooks:
      - id: ktlint
        name: ktlint
        entry: ./gradlew ktlintCheck
        language: system
        pass_filenames: false

run: pre-commit install

Optionally you can define custom ktlint rules by creating a .editorconfig file in root of your project.

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

[*.{kt,kts}]
max_line_length = 120
ktlint_standard_no-wildcard-imports = disabled
ktlint_standard_trailing-comma-on-call-site = disabled
ktlint_standard_trailing-comma-on-declaration-site = disabled
ktlint_standard_package-name = disabled
ktlint_function_naming_ignore_when_annotated_with = Composable

Check out Complete Android CI CD Mastery using Github Actions and Fastlane Guide →

[embed]Android CI/CD Mastery: Building a Production-Ready Pipeline with GitHub Actions & Fastlane If you want to jump into the code here is the link to repository with all the workflows 📦 GitHub: AndroidCICD-Masterymedium.com


메타데이터
post_id
8539d3d95dc9
slug
ktlint-pre-commit-hooks-and-pre-build-tasks-android-part-3-3-8539d3d95dc9
url
https://medium.com/@kkrishna22572/ktlint-pre-commit-hooks-and-pre-build-tasks-android-part-3-3-8539d3d95dc9
canonical_url
https://medium.com/@kkrishna22572/ktlint-pre-commit-hooks-and-pre-build-tasks-android-part-3-3-8539d3d95dc9
author_url
https://medium.com/@kkrishna22572
status
ok
fetched_at
2026-07-13 21:33:00