This project demonstrates how to implement and integrate custom Detekt rules into a Kotlin Multiplatform (KMP) project. It specifically focuses on enforcing Design System constraints to ensure consistency across the codebase.
The goal of this demo is to show how static analysis can prevent developers from bypassing the design system (e.g., using hardcoded colors, spacing, or typography) and guide them toward using the project's official tokens.
The project includes a :detekt-rules module with the following custom rules under the design-system-rules ruleset:
| Rule ID | Description | Recommended Alternative |
|---|---|---|
DirectColorUsage |
Prohibits direct use of Color literals or constructors (e.g., Color.Red, Color(0xFF...)). |
MaterialTheme.colorScheme |
DirectSpacingUsage |
Prohibits hardcoded .dp values for layout spacing. |
Project spacing tokens |
DirectTextStyleUsage |
Prohibits hardcoded .sp values or TextStyle constructor calls. |
MaterialTheme.typography |
If a specific line requires a deviation from the design system, you can use the // design-ignore comment:
val customPadding = 13.dp // design-ignore/composeApp: Shared Compose Multiplatform code for Android and iOS./detekt-rules: The Kotlin module containing the custom Detekt rule implementations and tests./iosApp: iOS-specific entry point and SwiftUI code.detekt.yml: Configuration file where the custom rules are enabled and configured.
To run detekt and check for design system violations:
./gradlew :composeApp:detektThe custom rules are registered in DesignSystemRuleSetProvider.kt and enabled in the root detekt.yml:
design-system-rules:
active: true
DirectColorUsage:
active: true
DirectSpacingUsage:
active: true
allowedValues: ['0', '1', '2']
DirectTextStyleUsage:
active: trueTo use these rules in the :composeApp module (or any other module), they are added as a detektPlugins dependency in the build.gradle.kts:
dependencies {
detektPlugins(project(":detekt-rules"))
}- Use the Android Studio run configuration or run:
./gradlew :composeApp:assembleDebug
- Open the
iosAppdirectory in Xcode or use the IDE's run configuration or change the configuration on Android Studio toiosAppand run.
Built with Kotlin Multiplatform and Compose Multiplatform.