Detekt Convention Plugin
Presumption, prerequisite and good reads đź“–
- The code snippets shared here use Gradle Version Catalog and Kotlin Gradle DSL
- Gradle Convention Plugin
- Moving from
buildSrc
tobuild-logic
Herding Elephants - Square Developers - Detekt documentation
Detekt 🔍
Detekt is a static code analysis tool, but it does a lot more than that, from being able to add your own custom rule to formatting your code (internally uses ktlint). It’s especially useful tool for when you are working on a large project or with a team and you need to ensure uniformity across code development setup.
Adding detekt âž•
To add detekt to our project we need to do the following things…
We need to define related dependencies in libs.versions.toml
Let’s now add the following to Project’s build.gradle.kts
And in build.gradle.kts
of our convention plugin. This will allow us to use DetektExtension
class and other classes in detekt plugin which allows us to configure detekt.
Detekt Convention Plugin 👨‍💻
Before we dive into the code, let’s understand what a convention plugin is.
In simple words convention plugin is gradle’s way of abstracting a responsibility and sharing build logic across gradle module, this way we are able to keep build gradle files of our module concise and ensure that we are not writing repetitive code.
Convention plugin can be used for anything, from adding dependencies to customizing build flavours. Now in Android is a great project to explore if you would like to see how convention plugins can make your life easy.
But for now, let’s check our DetektConventionPlugin.kt
We have successfully created our first plugin, but this plugin is not usable yet, one last step before we put it to use.
Registering Plugin âš“
We need to register this plugin, let’s go back to our convention plugin’s build.gradle.kts
file and do that.
🎉 tada… our plugin is ready for use, let’s add it in project’s build.gradle.kts
and then add it to all the modules where we want to leverage detekt (for example let’s take app
module) so in app/build.gradle.kts
Running detekt ▶️
If we have done the above steps correctly then our ./gradlew tasks
should return a task named detekt
.
Now let’s run detekt task, we can do that by running the following command and it should run detekt for us.
Note: It is possible while setting up this plugin you may run into some issues around compatibility, make sure you refer this compatibility table
Configuring detekt’s behaviour 🎯
Now let’s generate detekt config file, this file is responsible for detekt’s behaviour in your project. This file contains all the rules and their configuration which detekt is going to abide by.
To generate this yml
file run the following command
Next step is to ensure the configuration YAML file’s path is known to detekt plugin, we can do that by ensuring
that config.setFrom(..<path of config files seperated by ','>)
contains that path in our DetektConventionPlugin
.
If you run into a lot of warning from detekt and you want to suppress them, you can do that by generating a baseline file
Let’s ensure the baseline file’s path is known to detekt plugin, we can do that by ensuring
that baseline = file(<path of baseline file>)
has the correct path in our DetektConventionPlugin
.
🎉With that we have finished the integration of detekt in our project.🎉
Closing note 🎤
Detekt is customizable and has a lot of capabilities, with it being actively developed and open source, the future seems bright for Detekt. You can also write a simple workflow file to run detekt on every PR or before generating a build or even add a pre-commit hook.
Detekt documentation is done very well and can be really insightful for someone trying to integrate it or trying to customize it.
You can check out my integration of detekt in this repository