• ABOUT
  • NEWSLETTER
  • Contact
  • COFFEE
Recent posts
  • How to quickly write and run Kotlin script – practical guide
    • 15 September 2022
  • YearCompass – fix your New Year’s resolutions
    • 29 December 2021
  • How to make writing easier
    • 12 December 2021
  • Jetpack Compose – pros and cons of using it in production
    • 9 September 2021
  • Why I changed Kindle for a reader with e-ink and Android
    • 14 May 2021
  • Adding new words to learn with Android devices
    • 12 May 2021
Krzysztof Marczewski Krzysztof Marczewski
  • ABOUT
  • NEWSLETTER
  • Contact
  • COFFEE
  • Development

How to configure and use detekt on a daily basis

  • Krzysztof Marczewski
  • 27 March 2021
  • 6 minute read
Total
0
Shares
0
0
0
0

In a multi-module Android project

illustration by Estera

Recently I was configuring detekt in our multi-module project. To make it easier to pass knowledge about using it I wrote this article. Feel free to use it as documentation inside your project, to speed up using linter by new developers.

I divided it into three parts. The first part will give you Gradle setup for detekt in a multi-module android project. The second part is about the initial setup that every developer needs to do locally in order to use detekt. In the third part, I will talk about practical use-cases and some hints to use them daily.

Part I. Multi-module detekt setup

What is detekt?

“A static code analysis tool for the Kotlin programming language.” — in other wordsif your code has errors, bugs, stylistic, formatting errors, you will see an error with description.

detektAll

Configuring basic setup for detekt is a pretty straightforward task, as documentation provides clear steps on how to do it. But in projects with multiple modules, I found it hard to implement it so easily.

Therefore I thought it would be beneficial to share a gist of the Groovy build.gradle setup with you:

Using detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:1.16.0" is optional, but it will give you additional formatting checks and autoCorrect from ktlint, which is very useful in my opinion.

config

Detekt needs a configuration file (detekt.yml by default) in order to follow your guidelines. Once detekt in Gradle is set up, you can use built-in detektGenerateConfigtask to get a default configuration file for future improvements.

generating baseline

Putting detekt into a new project is easier, as you’re starting with clean code. On the other side, placing it into the older project will most probably create hidden issues. In order to start using detekt and prevent detektAll task from failing with current issues (based on the previously mentioned config), you may generate a baseline file — baseline.xml by default. This task is usually used once. In that way, we’re starting with a “clean” setup, and fix these issues during regular coding.

Again detekt comes with the task for creating a baseline. But for multi-module setup you will need a custom task that will do checks for each module. Here’s an additional snippet (you need variables from the first snippet too).

Part II. Basics and local configuration

Possible failure may look like this:

In this example, detekt found 1281 issues. Of course, you will see each issue in a separate line. For example:

You can also see possible errors right in IDE, but we will talk about this later (as you need a separate plugin for it)

Task

To check manually if your code is fine, you need to run detektAll task. You can do it in two ways:

  • Gradle panel (Project name → Tasks → Verification → detektAll). Hint: right-click detektAll and assign shortcut (for example cmd + shift + D).
  • command line (go to the project folder and run ./gradlew detektAll)

BTW: Default Gradle task for detekt is just detekt, but as I was creating a custom task that is running on each module I named it detektAll, so I will use it as a convention here. So in our case detekt task will not work properly.

Cool, now you know how to use it, but there’s more to it. With pre-push githook, you can tell git, to check if there are still some issues you’ve missed before pushing code.

Githook!

With that hook if you will try to push your changes, and if detekt will detect issues, it won’t let you push them until they will be resolved. Let’s see what you can expect:

  1. If you will push through the command line you will see what you should fix
  2. If you will push through Android Studio, it will show you generic info about detekt failure. To check what’s wrong in details, you will need to run detektAll Gradle task (reminder: use shortcut that was assigned earlier)

Setting up githook

  1. If you don’t have .git/ folder inside your project run git init command inside of it. It will reinitialize the existing Git repository.
  2. Go to .git/hooks/ folder.
  3. Create pre-push file.
  4. Paste below snippet (source: documentation) and save file/
#!/usr/bin/env bash
echo "Running detekt check..."
OUTPUT="/tmp/detekt-$(date +%s)"
./gradlew detektAll > $OUTPUT
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
  cat $OUTPUT
  rm $OUTPUT
  echo "***********************************************"
  echo "                 Detekt failed                 "
  echo " Please fix the above issues before committing "
  echo "***********************************************"
  exit $EXIT_CODE
fi
rm $OUTPUT
  1. You need to make .git/hooks/pre-push executable otherwise it will be ignored. To do so, go to .git/hooks/ and run chmod +x pre-push .

CI

If for some reason, you will bypass pre-push, our CI should catch it anyway. Though i’s good to catch it locally first as it’s faster, and you’re not polluting CI with unnecessary builds.

Suppose you don’t have it in your project yet, I encourage you to add a job to your CI with detekt. Configuring steps for running detekt in your builds is platform-specific, so it won’t be covered here.

Part II. How to live in peace with detekt:

Plugin

Now you know how to use detekt task, but it will be nice to see issues while coding without the need to run task each time.

This plugin will highlight issues in IDE, so you will see what you should fix right after a mistake occurs.

Setting up detekt plugin

  • Install the plugin: https://plugins.jetbrains.com/plugin/10761-detekt .
  • Once installed, configure it based on the screenshot below, and put configuration file + baseline file paths. They’re usually placed in config/detekt/detekt.yml && config/detekt/baseline.xml .

Basics

First thing first — resolve issues right after they appear.

Unfortunately, there may come a time when the issue is too large to fix it easily/quickly. In that case, you have 2 options:

Suppress warning

Let’s say we have a long function that needs to remain long for some reason.

To hide that warning, and bypass this one particular case, you could add @Suppress("CopyHereExactNameOfError") in this example @Suppress("LongMethod") .

Take in mind that suppressing is not a good solution. It should be only a TEMPORARY solution, so after putting suppress, you should probably create a technical debt task to resolve that issue properly.

You can find more details about suppressing in the official documentation

Change config

Sometimes a solution may be to talk to other team members and change the constraints.

Autocorrect hack

Some straightforward formatting issues can be fixed automatically during running detekt task. In order to take advantage of that feature, if you’re using my snippet you can pass flag to detektAllcommand.

./gradlew detektAll -PdetektAutoFix=true

It will auto-fix (if possible) formatting issues after running task.

Thanks Tom Koptel for hint with passing flag to the the Gradle task!

Exclude specific

If some packages are legacy or cause problems with detekt, and they won’t be fixed for some reason, you can add that package to excluded exceptions in detektAlltask. Nevertheless, it’s necessary to make this decision together with other team members.

IDE setup

Android Studio setup should match detekt config. In our case we’ve built upon a basic configuration file with few tweaks:

maxLineLength

Our detekt is configured to error when the line length is longer than 150 characters. Set up Android Studio, to match those constraints.

Wildcard imports

We’re not allowing for wildcard imports except java.utils and kotlinx.android.synthetic

There is more

Nothing from these configurations is set in stone and can be changed reactively. The above documentation is just a basic setup for further modifications.

As you’re probably an Android Developer, you may be interested in reading my recent post — Android Studio Productivity Course — with a huge amount of useful shortcuts, tips, and tricks.

As I said in the introduction: feel free to use the above documentation inside your project and speed up using linter by new devs. 

Tell me if you found it useful.

Resources:

detekt, gradle options

Total
0
Shares
0
0
0
0
Previous Article
  • Productivity

Changing industry and why you should not be scared about it

  • Krzysztof Marczewski
  • 22 February 2021
View Post
Next Article
  • Productivity
  • Tools

Adding new words to learn with Android devices

  • Krzysztof Marczewski
  • 12 May 2021
View Post
You May Also Like
View Post
  • Development

Jetpack Compose – pros and cons of using it in production

  • Krzysztof Marczewski
  • 9 September 2021
View Post
  • Development

Android Studio Productivity Course

  • Krzysztof Marczewski
  • 6 January 2021
View Post
  • Development
  • Productivity

Code reviews done right

  • Krzysztof Marczewski
  • 6 January 2021

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Table of Contents Hide
  1. Part I. Multi-module detekt setup
    1. What is detekt?
    2. detektAll
    3. config
    4. generating baseline
  2. Part II. Basics and local configuration
    1. Task
    2. Githook!
    3. Setting up githook
    4. CI
  3. Part II. How to live in peace with detekt:
    1. Plugin
    2. Setting up detekt plugin
    3. Basics
    4. Suppress warning
    5. Change config
    6. Autocorrect hack
    7. Exclude specific
    8. IDE setup
    9. maxLineLength
    10. Wildcard imports
  4. There is more
    1. Resources:
Recent posts
  • How to quickly write and run Kotlin script – practical guide
    • 15 September 2022
  • YearCompass – fix your New Year’s resolutions
    • 29 December 2021
  • How to make writing easier
    • 12 December 2021
  • Jetpack Compose – pros and cons of using it in production
    • 9 September 2021
  • Why I changed Kindle for a reader with e-ink and Android
    • 14 May 2021
Newsletter
About
Krzysztof Marczewski
Android Developer @AngryNerds delighted with Kotlin, Compose, and multiplatform. Non-fiction bookworm that loves minimal UI, smart UX, and clever copywriting.
Twitter Feed
It's beautiful. https://t.co/UGZKU4BrDf
57 days ago
  • Reply
  • Retweet
  • Favorite
Wanna create a text file .txt quickly with right-click in Finder on macOS? There is a great free app for it:… https://t.co/6tTdWoP0Ye
71 days ago
  • Reply
  • Retweet
  • Favorite
Until #macOS Monterey process of erasing mac in order to sell it or clean was pretty inconvenient. But now you can… https://t.co/fr5Q3FoOjW
147 days ago
  • Reply
  • Retweet
  • Favorite
I came across a #screenshot tool from @shottr_cc. 🖼 At first, I wanted to write that it is useful for mobile devs a… https://t.co/kBwNlXBMs3
147 days ago
  • Reply
  • Retweet
  • Favorite
Follow
© 2021 by Krzysztof Marczewski

Input your search keywords and press Enter.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT