DEV Community

Cover image for Supercharge your Kotlin Project
aseem wangoo
aseem wangoo

Posted on • Updated on

Supercharge your Kotlin Project

In case it helped :)
Pass Me A Coffee!!

This article shares some tips on things to include inside your kotlin project. We will cover briefly:

  1. Ktlint
  2. Detekt
  3. Github workflow

Note: This article assumes the reader knows about Kotlin

Ktlint

Linting is the process of analyzing code for potential errors and one of the ways we can do that in Kotlin is using Ktlint. As per the documentation:

Ktlint is an anti-bikeshedding Kotlin linter

There are two ways to integrate Ktlint in a kotlin project. 

jlleitschuh/ktlint-gradle
jeremymailen/kotlinter-gradle
Enter fullscreen mode Exit fullscreen mode

We will be using ktlint-gradle, which is a wrapper over ktlint.

Setup

  • Head over to the project-level build.gradle
  • Inside the buildscripts add the latest version in ext and add the dependency under dependencies
ext {
   // https://github.com/JLLeitschuh/ktlint-gradle/releases
   ktlintPluginVersion = "10.2.0"
}
dependencies {
    classpath 'com.android.tools.build:gradle:7.0.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"

    ## KTLINT DEPENDENCY
    classpath "org.jlleitschuh.gradle:ktlint-gradle:$ktlintPluginVersion"    
}
Enter fullscreen mode Exit fullscreen mode

Inside the repositories section, add this

repositories {
    google()
    mavenCentral()
    ## ADDED THIS FOR KTLINT
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}
Enter fullscreen mode Exit fullscreen mode

We create a separate file for configuring/customizing ktlint. Create a directory buildscriptsat the project level and inside it, create a new file called ktlint.gradle

apply plugin: "org.jlleitschuh.gradle.ktlint"
ktlint {
    // https://github.com/pinterest/ktlint/releases
    version = "0.42.1"
    reporters {
        reporter "plain"
        reporter "checkstyle"
        reporter "html"
    }
    outputColorName = "RED"
}
Enter fullscreen mode Exit fullscreen mode

Finally, we apply this configuration to all our subprojects by adding the below inside our project-level build.gradle

subprojects {
    apply from: "../buildscripts/ktlint.gradle"
}
Enter fullscreen mode Exit fullscreen mode
  • Click Sync Now If everything is successful, we should see a gradle task called ktlintformat 

ktlintFormat Gradle task
ktlintFormat Gradle task

Note: In case you are not able to see your gradle tasks, check this post

  • After running the task ktlintformat it outputs the results, (in case of errors, it will show the file which has the details of the files)

ktlintformat errors
ktlintformat errors

Click on the .txt file and correct the errors accordingly.

Note: You will probably have a lot of file changes, since ktlintformat checks all your project files and corrects them as per the kotlin standard.

Detekt

Detekt is a static code analysis tool for the Kotlin programming language. It operates on the abstract syntax tree provided by the Kotlin compiler.

It analyzes the Kotlin code with multiple rule sets and flags the code that breaks any of its rules.

Setup

  • Head over to the project-level build.gradle
  • Inside the buildscripts add the latest version in ext and add the dependency under dependencies
ext {
   // https://github.com/detekt/detekt/releases
   detektVersion = "1.17.0"
}
dependencies {
    classpath 'com.android.tools.build:gradle:7.0.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"

    ## DETEKT DEPENDENCY
    classpath "io.gitlab.arturbosch.detekt:detekt-gradle-    plugin:$detektVersion"  
}
Enter fullscreen mode Exit fullscreen mode

We create a separate file for configuring/customizing detekt. Create a directory buildscriptsat the project level (ignore if you created in the above step) and inside it, create a new file called detekt.gradle

apply plugin: "io.gitlab.arturbosch.detekt"

detekt {
    config = files("${rootProject.projectDir}/config/detekt/detekt.yml")

    reports {
        html.enabled = true
        xml.enabled = true
        txt.enabled = true
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: The config file detekt.yml doesn’t exist yet, we will see in the later step

  • Finally, we apply this configuration to all our subprojects by adding the below inside our project-level build.gradle
subprojects {
    apply from: "../buildscripts/detekt.gradle"
}
Enter fullscreen mode Exit fullscreen mode
  • Click Sync Now If everything is successful, we should see a gradle task called detektGenerateConfig and run it
detekt generateConfig gradle task
detekt generateConfig gradle task
  • After running the task, it generates the config file called detekt.yml 
detekt config yml
detekt config yml

which comprises of all the configurations, detekt comes with. We can customize it according to our preference.

  • Finally, we run the following in our project
./gradlew detekt
Enter fullscreen mode Exit fullscreen mode

This will show you all the issues inside your project, along with the configuration name.

detekt issues in project
detekt issues in project

In case, you want to suppress some issue in a file, for instance, the above issues related to MagicNumber , we can simply go to the file and add

@file:Suppress("MagicNumber")
Enter fullscreen mode Exit fullscreen mode

Github workflow

We saw in the above steps how to integrate ktlint and detekt inside our kotlin project.

In the last step, we will integrate both with our CI, ensuring the main branch, always remains standardized.

  • Add a .github directory inside the project.
  • Create a subdirectory workflows and inside it, create a file called android_build.yml 
name: Android Build

on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1

      - name: Set Up JDK
        uses: actions/setup-java@v1
        with:
          java-version: 1.11

      - name: Build Project
        run: ./gradlew assemble

      - name: Run Tests
        run: ./gradlew test

      - name: Lint Checks
        run: ./gradlew ktlintCheck detekt
Enter fullscreen mode Exit fullscreen mode
  • In the last step, we include the ktlintCheck as well as detekt Since this job runs on push, we will be sure in case the code is according to our conventions.
  • We can also add a branch protection rule for requiring status checks to pass and selecting build

Branch protection rule
Branch protection rule

Source code.

In case it helped :)
Pass Me A Coffee!!

Top comments (0)