Learn Android Development

How to Analyze Your Code with Detekt

Getting the machine to improve your Kotlin Code

Maria Luíza
Mobile App Development Publication

--

Android robot keenly spying

Hello, there! I hope you’re doing well!

In the world of Android development, staying vigilant about code quality is paramount. That’s where Detekt, a powerful static code analysis tool, comes into play.

Detekt enables developers to analyze their codebase, identify potential issues, and optimize their projects for better performance. By leveraging Detekt, you can gain valuable insights to improve the quality and maintainability of their Android applications. In this article, we will explore the capabilities of Detekt and learn how it can significantly enhance our development process.

Implementation

First, we need to import the Detekt library into our project. We can achieve this by adding the Detekt dependency to the project’s Gradle file.

dependencies {
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.18.1"
}

You can see the latest version of Detekt by clicking here.

And apply the plugin to the build.gradle at the Module level.

plugins {
id 'io.gitlab.arturbosch.detekt'
}

Sync the project with the Gradle files, and now you will have access to the Detekt tasks.

To run Detekt, open the terminal in Android Studio and use the following command:

.\gradlew detekt

The Detekt will traverse all of your code, identify the issues, and indicate where and what is wrong.

This is an example:

> Task :app:detekt

LongParameterList - 8/6 - [BarGraph] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\Ba
rGraph.kt:33:13

LongMethod - 159/60 - [BarGraph] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\BarGra
ph.kt:33:5

LongMethod - 103/60 - [HomeContent] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\pre
sentation\ui\screen\HomeContent.kt:32:5

EmptyFunctionBlock - [AnalyticsScreen] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\
presentation\ui\screen\AnalyticsScreen.kt:10:3

TooGenericExceptionCaught - [e] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\data\re
pository\DataStoreRepositoryImpl.kt:38:18

ForEachOnRange - [<anonymous>] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\BarGraph
.kt:120:24

UnusedPrivateMember - [barShape] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\BarGra
ph.kt:62:5

NewLineAtEndOfFile - [HomeContent.kt] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\p
resentation\ui\screen\HomeContent.kt:1:1

WildcardImport - [WelcomeScreen.kt] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\pre
sentation\ui\screen\WelcomeScreen.kt:12:1

MagicNumber - [AtomicTangerineDark] at C:\Users\maria\AndroidStudioProjects\shop\app\src\main\java\com\example\shop\pre
sentation\ui\theme\Color.kt:18:33

NewLineAtEndOfFile - [ExampleUnitTest.kt] at C:\Users\maria\AndroidStudioProjects\shop\app\src\test\java\com\example\sh
op\ExampleUnitTest.kt:1:1

Each “exception” is presented in the Detekt documentation along with instructions on how to fix it.

You may ask, “Do I have to follow the Detekt rules for my code analysis?” Well, the answer is no, you don’t. Detekt allows you to create and customize your own rules regarding the code format.

Back to the terminal, we will create a Detekt file to set up the configurations:

.\gradlew detektGenerateConfig

This will create a detekt.yml file, allowing you to customize it as you wish.

If you pay attention, you will see the active tag under the exceptions. This allows you to pass true or false to enable or disable the format check.

comments:
active: true
AbsentOrWrongFileLicense:
active: false
licenseTemplateFile: 'license.template'
licenseTemplateIsRegex: false
CommentOverPrivateFunction:
active: false
CommentOverPrivateProperty:
active: false
DeprecatedBlockTag:
active: false
EndOfSentenceFormat:
active: false
endOfSentenceFormat: '([.?!][ \t\n\r\f<])|([.?!:]$)'

You can customize it as desired. Access the documentation of Detekt here to explore different types of rules.

Conclusion

Detekt is a powerful code analysis tool that helps improve code quality in Android development. By leveraging Detekt, developers can identify and address issues, optimize performance, and enhance maintainability.

With its customizable rules and comprehensive documentation, Detekt empowers developers to elevate the quality of their Android applications. Start using Detekt today to enhance your development process and ensure code excellence.

The complete code is available in this repository on my GitHub profile.

Happy coding ❤

Please let me know what you think in the comments…

Connect with me 👇

Linkedin

GitHub

A man is wearing a white t-shirt, blue pants, and a yellow turban. He is sitting with a laptop on his legs.

--

--

Maria Luíza
Mobile App Development Publication

I'm an Android developer (She/Her). Feel free to ask me anything about the Android world, learning Kotlin/Java & a bit of Dart.