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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Integrating detekt in the Workflow
30 mins
- Getting Started
- Understanding detekt
- Adding detekt To Your Project
- Adding to New Projects
- Running detekt Terminal Command
- Adding to Existing Projects
- Looking at detekt Rule Sets
- Configuring detekt
- Adding a Rule Set
- Breaking More Rule Sets
- Suppressing Issues
- Writing Custom detekt Rules
- Adding Your Own Rule to detekt
- Testing Your Custom Rule
- Using the Custom Rule in Your Project
- Looking at Custom Processors to detekt
- Integrating detekt With GitHub Actions
- Integrating detekt With Your IDE
- Where to Go From Here?
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:
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:
- Creates a workflow named detekt
- 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.
- Defines the jobs to de done by the work flow. You only have detekt as your job.
- 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:
From the Github Actions you'll see:
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.
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:
Click Install to install the plugin. Once the installation completes, exit this screen. Navigate to Preferences/Settings ▸ Tools ▸ detekt and check Enable Detekt.
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!