This article shares some tips on things to include inside your kotlin project. We will cover briefly:
- Ktlint
- Detekt
- 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
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 inext
and add the dependency underdependencies
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"
}
Inside the repositories
section, add this
repositories {
google()
mavenCentral()
## ADDED THIS FOR KTLINT
maven {
url "https://plugins.gradle.org/m2/"
}
}
We create a separate file for configuring/customizing ktlint
. Create a directory buildscripts
at 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"
}
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"
}
- Click
Sync Now
If everything is successful, we should see a gradle task calledktlintformat
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)
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 inext
and add the dependency underdependencies
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"
}
We create a separate file for configuring/customizing detekt
. Create a directory buildscripts
at 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
}
}
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"
}
- Click
Sync Now
If everything is successful, we should see a gradle task calleddetektGenerateConfig
and run it
- After running the task, it generates the config file called
detekt.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
This will show you all the issues inside your project, along with the configuration name.
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")
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 calledandroid_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
- In the last step, we include the
ktlintCheck
as well asdetekt
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
Top comments (0)