Android, ktlint, and pre-commit Git Hook

Chip Cerio
3 min readJul 26, 2019

--

Photo by Rachael Gorjestani on Unsplash

When I do code reviews, I try to avoid nitpicking especially related to code formatting. Tools should validate code formats, not people. This tool - ktlint should do this for you in your local development environment. A perfect place to run this is in your local machine before you push your changes to a git repository.

All right, let’s get started!

ktlint setup

Let’s create a file ktlint.gradle at the root of the project.

repositories {
jcenter()
}
configurations {
ktlint
}
dependencies {
ktlint "com.pinterest:ktlint:0.33.0"
}
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "src/**/*.kt"
}
task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
main = "com.pinterest.ktlint.Main"
args "-F", "src/**/*.kt"
}

Here, we’re creating 2 new gradle tasks ktlint and ktlintFormat. Then we add these tasks on top of the default android gradle tasks. We do this by modifying the app/build.gradle file.

apply plugin: 'com.android.application'
...
apply from: '../ktlint.gradle'
android {
...
}
check.dependsOn ktlint

To check if these tasks are added,

ktlint and ktlintFormat tasks
ktlint and ktlintFormat tasks

You should now be able to check and format your code using these tasks. To improve your development further, we’re going to add a script that tells git to check the format before you commit your changes.

Add pre-commit git hook

Git hooks are programs you can place in a hooks directory to trigger actions at certain points in git’s execution. There are few items of certain points of git’s execution and the one that we’re interested about is the pre-commit hook.

Setup pre-commit git hook

Let’s create a file pre-commit at the root of the project.

#!/bin/bash

git stash -q --keep-index

echo "Running git pre-commit hook"

./gradlew ktlint

RESULT=$?

git stash pop -q

# return 1 exit code if running checks fails
[ $RESULT -ne 0 ] && exit 1
exit 0

This script runs before every git commit you make. This is the perfect place to run ktlint. Then let’s create another gradle task that installs this pre-commit git hook before building/assembling your Android project.

Add installGitHook task

Let’s add a project-level gradle task. Modify the build.gradle of the root project.

allProjects {
repositories {
...
}
}
task installGitHook(type: Copy) {
from new File(rootProject.rootDir, 'pre-commit')
into { new File(rootProject.rootDir, '.git/hooks') }
fileMode 0777
}
tasks.getByPath(':app:preBuild').dependsOn installGitHook

installGitHook task runs during preBuild. preBuild is the perfect place to run actions before you start building your Android project.

And that’s it! No more nitpicking about code styles. Let ktlint do it for you.

--

--