Android Detekt
Overview
This document provides an overview of using Detekt for the Entain Android App. The goal of integrating Detekt is to enhance code quality, maintain consistency, and identify potential issues early in the development process.
Why use Detekt?
While we currently use ktlint for formatting our codebase, it primarily focuses on enforcing consistent code style. However, it lacks powerful static analysis capabilities to catch deeper issues like code complexity, potential bugs, and poorly written code.
By implementing Detekt and adhering to the rules defined in the detekt.yml
configuration file, we aim to:
- Identify potential bugs and design issues early.
- Enforce consistent best practices across the codebase.
- Reduce technical debt by highlighting areas that need refactoring.
Key Benefits
Reduce Inconsistencies in Code Detekt ensures all developers follow the same coding standards, reducing stylistic inconsistencies. This leads to cleaner and more readable code, which is easier to maintain and scale.
Highlight Complex Code Detekt flags overly complex functions or classes and suggests improvements.
Catch Potential Bugs Early Static analysis helps identify common pitfalls and bugs before they make it to production, saving time and reducing the chance of runtime failures.
Encourage Best Practices Detekt enforces best practices by warning against bad design patterns, unused code, and inefficiencies.
How to enable the Detekt Plugin in Android Studio
To enable Detekt directly in Android Studio, install the Detekt IntelliJ Plugin from the marketplace. The plugin will annotate Detekt issues on-the-fly while coding.
Steps:
- Go to File > Settings > Plugins.
- Search for
Detekt
in the Marketplace and install it. - Restart Android Studio.
- Configure the plugin by pointing it to your
detekt.yml
file:
- Navigate to Tools > Detekt > Configure Plugin Settings.
- Specify the path to your configuration file (
config/detekt/detekt.yml
).
Refer to the plugin’s README for more details: Detekt IntelliJ Plugin.
How to run detekt
For the whole project
./gradlew detekt
For a specific module
./gradlew module:submodule:detekt
e.g
./gradlew core:analytics:detekt
./gradlew feature:betslip:presentation:detekt
For Changed Files Only
Use the provided script in scripts/hooks/pre-push-detekt-check to run Detekt only on the files you’ve modified. This significantly reduces execution time From your terminal run the following command:
sh scripts/hooks/pre-push-detekt-check
(See below how to set this up as a pre-push git hook)
Reviewing detekt reports
Detekt generates detailed HTML reports for each module it analyzes. These reports provide a rich visual summary of detected issues, their severity, and recommended fixes. Open the report in any browser to review.
Setting up the git hook(s)
It is recommended to set up pre-commit and pre-push Git hooks to help enforce code quality and maintain consistency in your development process.
Run the following script to set up the hooks:
sh scripts/setup_git_commit_hooks <pre-commit-argument> <pre-push-argument>
Available git-hook Arguments
check
- Runs ktlintCheck to validate your Kotlin code against ktlint rules.
- If violations are detected, the process fails and a list of issues is printed in the Gradle console.
- Note: This command does not modify your code.
format
- Runs ktlintFormat to automatically format your Kotlin code according to ktlint rules.
- Necessary formatting changes are applied to the files automatically.
- Important: After running format, you’ll need to stage and re-commit any modified files.
detekt
- Runs Detekt only on files that have been modified or added compared to the remote branch.
- Prevents code from being pushed to the remote branch if Detekt violations are detected.
- Purpose: Ensures that code issues are resolved locally before reaching the CI pipeline.
Additional Notes
- Using both check and format together for the same hook is unnecessary; prefer format for automatic fixes during development.
- Detekt is optimised to run only on relevant files, minimizing impact on performance while ensuring quality.
- Make sure to re-commit files modified by ktlintFormat before pushing your changes.