Android Detekt Logic Analysis 🧐🔍
Advanced strategies for detecting hidden logic errors, structural flaws, and code debt in Kotlin Android applications.
⚡ When to Use
- Code Smell Hunting: Identifying long functions, deeply nested loops, or excessive cognitive complexity.
- Architectural Guardrails: Enforcing rules like "No
viewModelScope.launchoutside of ViewModels". - Naming Enforcement: Ensuring all interfaces end with
Interface(if that's the team policy). - Refactoring Strategy: Identifying the most "complex" files that need simplification first.
- CI/CD Quality Gate: Failing a build based on logic violations.
🏗️ Core Categories
1. Complexity
- TooManyFunctions: Aim for < 11 functions per class.
- LongMethod: Aim for < 20 lines per function.
- NestedBlockDepth: Aim for < 4 levels of nesting.
2. Formatting (Logic-Based)
- OptionalUnit: Don't return
Unitexplicitly. - UnusedPrivateMember: Delete unused variables and functions.
- RedundantVisibilityModifier: Keep it clean if defaults are sufficient.
3. Potential Bugs
- EqualsWithHashCode: Always override both together.
- UnreachableCode: Remove code that can't be executed.
- UnsafeCallOnNullableType: Avoid using
!!operator.
4. Style & Naming
- ForbiddenMethodCall: Block usage of certain libraries (e.g.,
java.util.Datein favor ofjava.timeorkotlinx-datetime). - ForbiddenImport: Prevent importing internal or deprecated classes.
🛠️ Essential Commands
Check Logic Errors
./gradlew detekt
Auto-Correct (Basic)
./gradlew detekt --auto-correct
📜 Configuration (detekt.yml)
Use a custom configuration file to override default thresholds:
complexity:
TooManyFunctions:
active: true
thresholdInFiles: 11
thresholdInClasses: 11
thresholdInInterfaces: 11
thresholdInObjects: 11
LongMethod:
active: true
threshold: 20
🚀 Performance & Tips
- Custom Rules: Implement
Rulesubclass to enforce project-specific requirements. - Baseline Files: Use
detekt-baseline.xmlfor large existing projects to prioritize fixing new issues. - IDE Integration: Install the "Detekt" plugin in Android Studio/IntelliJ.
🧪 Verification Checklist
- No Complex Functions: Logic is broken down into small, testable units.
- No Dead Code: All private members are used or deleted.
- Stable APIs: All public APIs follow consistent naming and documentation rules.
- No Deprecated Usage: Modern APIs are used exclusively.