Integrating detekt in the Workflow

Learn how to integrate the powerful detekt tool in Android app development to help detect and prevent code smells during the development process. By Harun Wangereka.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Integrating detekt With GitHub Actions

GitHub Actions is GitHub's platform for automation workflows. With GitHub actions, you can add a detekt action. You can set it to run when someone pushes or creates a pull request on your said branches.

To enable actions, go to any of your GitHub projects and navigate to the actions tab as shown below:

Enabling GitHub Actions

Tap Set up a workflow yourself. You'll see the workflow editor. Inside the editor, add:

## 1
name: detekt
## 2
on:
 push:
   branches:
   - main
 pull_request:
   branches:
   - main
## 3
jobs:
 detekt:
   ## 4
   runs-on: ubuntu-latest
   steps:
      - name: "checkout"
        uses: actions/checkout@v2
      - name: "detekt"
        uses: natiginfo/action-detekt-all@1.17.0

Here's what the code above does:

  1. Creates a workflow named detekt
  2. Specifies when your workflow will run. In this case, the workflow runs when there's a push or a pull request on the main branch.
  3. Defines the jobs to de done by the work flow. You only have detekt as your job.
  4. Specifies a runner and steps for your job. You also add a third-party action, called detekt action. It runs detekt checks to ensure your code follows the set practices before merging to the main branch.

Save your workflow and create a pull request. For this sample project, the pull request fails with the following error:

Failing GitHub Actions

From the Github Actions you'll see:

GitHub Actions Report

The workflow fails and shows the same report as you were seeing on your terminal. This is a very useful feature for detekt, as it ensures any changes added to the main branch don't have code smells. The pull request creator has to first address the issues before merging the pull request.

Note: If you're not familiar with GitHub Actions, take a look at this Continuous Integration for Android tutorial.

You've seen how to add detekt on GitHub Actions. Next, you'll learn how to add detekt on Android Studio.

Integrating detekt With Your IDE

To add the detekt plugin to Android Studio, navigate to Preferences/ Settings ▸ Plugins ▸ Search detekt. You'll see the plugin as in the image below:

detekt IDE plugin

Click Install to install the plugin. Once the installation completes, exit this screen. Navigate to Preferences/Settings ▸ Tools ▸ detekt and check Enable Detekt.

detekt plugin configuration options

detekt IDE plugin also has these optional configuration options:

  • Configuration file path: This is your detekt.yml.
  • Baseline file: Path to your custom baseline.xml.
  • Plugin Jars: Path to jar file that has your custom rules, if you want detekt to report them.

With this, the IDE will detect errors on the go as you code!

Where to Go From Here?

Download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.

Congratulations! You've learned so much about detekt and its features. Now you know how you can add it to your project, create custom rules or even integrate it on the workflow in GitHub Actions or your IDE. :]

We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!