<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chip Cerio on Medium]]></title>
        <description><![CDATA[Stories by Chip Cerio on Medium]]></description>
        <link>https://medium.com/@alistair.cerio?source=rss-e2c16c101539------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*27LpbRTqQoMAP3ZzAAtKfw.jpeg</url>
            <title>Stories by Chip Cerio on Medium</title>
            <link>https://medium.com/@alistair.cerio?source=rss-e2c16c101539------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 07:40:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alistair.cerio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android, ktlint, and pre-commit Git Hook]]></title>
            <link>https://medium.com/@alistair.cerio/android-ktlint-and-pre-commit-git-hook-5dd606e230a9?source=rss-e2c16c101539------2</link>
            <guid isPermaLink="false">https://medium.com/p/5dd606e230a9</guid>
            <category><![CDATA[git-hooks]]></category>
            <category><![CDATA[linter]]></category>
            <category><![CDATA[ktlint]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[formatter]]></category>
            <dc:creator><![CDATA[Chip Cerio]]></dc:creator>
            <pubDate>Fri, 26 Jul 2019 07:11:16 GMT</pubDate>
            <atom:updated>2019-08-06T00:03:16.179Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HQ-6NweU9RkbT8660WDrcQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@rachaelgorjestani?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Rachael Gorjestani</a> on <a href="https://unsplash.com/search/photos/automate-process?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>When I do code reviews, I try to avoid nitpicking especially related to code formatting. Tools should validate code formats, not people. This tool - <a href="https://github.com/pinterest/ktlint">ktlint</a> 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.</p><p>All right, let’s get started!</p><h4>ktlint setup</h4><p>Let’s create a file ktlint.gradle at the root of the project.</p><pre>repositories {<br>    jcenter()<br>}</pre><pre>configurations {<br>    ktlint<br>}</pre><pre>dependencies {<br>    ktlint &quot;com.pinterest:ktlint:0.33.0&quot;<br>}</pre><pre>task ktlint(type: JavaExec, group: &quot;verification&quot;) {<br>    description = &quot;Check Kotlin code style.&quot;<br>    classpath = configurations.ktlint<br>    main = &quot;com.pinterest.ktlint.Main&quot;<br>    args &quot;src/**/*.kt&quot;<br>}</pre><pre>task ktlintFormat(type: JavaExec, group: &quot;formatting&quot;) {<br>    description = &quot;Fix Kotlin code style deviations.&quot;<br>    classpath = configurations.ktlint<br>    main = &quot;com.pinterest.ktlint.Main&quot;<br>    args &quot;-F&quot;, &quot;src/**/*.kt&quot;<br>}</pre><p>Here, we’re creating 2 new gradle tasks <strong>ktlint</strong> and <strong>ktlintFormat</strong>. Then we add these tasks on top of the default android gradle tasks. We do this by modifying the app/build.gradle file.</p><pre>apply plugin: &#39;com.android.application&#39;<br>...<br>apply from: &#39;../ktlint.gradle&#39;</pre><pre>android {<br>    ...<br>}</pre><pre>check.dependsOn ktlint</pre><p>To check if these tasks are added,</p><figure><img alt="ktlint and ktlintFormat tasks" src="https://cdn-images-1.medium.com/max/824/1*OA8Md0zob7Hyse5OEfakVw.png" /><figcaption>ktlint and ktlintFormat tasks</figcaption></figure><p>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 <strong>git</strong> to check the format before you commit your changes.</p><h4>Add pre-commit git hook</h4><p>Git hooks are programs you can place in a <strong>hooks</strong> 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 <strong>pre-commit</strong> hook.</p><h4>Setup pre-commit git hook</h4><p>Let’s create a file pre-commit at the root of the project.</p><pre>#!/bin/bash<br><br>git stash -q --keep-index<br><br>echo &quot;Running git pre-commit hook&quot;<br><br>./gradlew ktlint<br><br>RESULT=$?<br><br>git stash pop -q<br><br># return 1 exit code if running checks fails<br>[ $RESULT -ne 0 ] &amp;&amp; exit 1<br>exit 0</pre><p>This script runs before every git commit you make. This is the perfect place to run <strong>ktlint</strong>. Then let’s create another gradle task that installs this pre-commit git hook before building/assembling your Android project.</p><h4>Add installGitHook task</h4><p>Let’s add a project-level gradle task. Modify the build.gradle of the root project.</p><pre>allProjects {<br>    repositories {<br>        ...<br>    }<br>}</pre><pre>task installGitHook(type: Copy) {<br>    from new File(rootProject.rootDir, &#39;pre-commit&#39;)<br>    into { new File(rootProject.rootDir, &#39;.git/hooks&#39;) }<br>    fileMode 0777<br>}</pre><pre>tasks.getByPath(&#39;:app:preBuild&#39;).dependsOn installGitHook</pre><p><strong>installGitHook</strong> task runs during <strong>preBuild</strong>. preBuild is the perfect place to run actions before you start <strong>build</strong>ing your Android project.</p><p>And that’s it! No more nitpicking about code styles. Let <strong>ktlint</strong> do it for you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5dd606e230a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>