Skip to content

project rulesMobilekotlin

Kotlin Android Rules

Project rules for Android in Kotlin: Jetpack Compose UI, ViewModel with StateFlow, Hilt injection, structured coroutines and tests.

npx workspacetemplates add kotlin-android-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file19 lines · 1.2 KBFile
# Project rules: Android with Kotlin

## Architecture
- One feature per package with `ui`, `domain` and `data` layers. Composables render a single immutable `UiState`; they send events up as lambdas.
- A `ViewModel` exposes `StateFlow<UiState>` built with `stateIn` or `MutableStateFlow` and a `.asStateFlow()` view. One-off effects go through a `Channel` or are modeled as state.
- Repositories return `Flow` or suspend results and hide Room, Retrofit and DataStore details.

## Compose
- Hoist state. Keep composables stateless and previewable. Collect with `collectAsStateWithLifecycle()`.
- Use `remember` and `derivedStateOf` deliberately; give lazy list items stable `key`s. Take a `Modifier` parameter as the first optional one.

## Injection and coroutines
- Use Hilt with constructor injection. Inject `CoroutineDispatcher`s instead of hard-coding `Dispatchers.IO`.
- Launch from `viewModelScope` or `lifecycleScope`; never `GlobalScope`. Rethrow `CancellationException`.

## Quality
- Keep secrets out of source; read them from `local.properties` or CI.
- Unit-test ViewModels with `runTest` and Turbine; test composables with `createComposeRule`.
- Run `./gradlew ktlintCheck lint testDebugUnitTest assembleDebug` before finishing.

What it does

These rules describe a modern Android codebase: Compose screens that render state, ViewModels that own it and repositories that hide data sources. An agent following them produces features shaped the same way every time.

They also set the coroutine and testing habits that stop leaks and flaky tests, such as injected dispatchers and lifecycle-aware collection.

Example

you › Add a settings screen with a dark mode toggle

agent › The agent adds a SettingsViewModel with StateFlow over DataStore, a stateless SettingsScreen, Hilt wiring and a Turbine test for the toggle.

Questions

Does it require Hilt?
It prefers Hilt, but the constructor injection rule holds with Koin or manual wiring as well.
Why inject dispatchers?
Tests can then swap in a test dispatcher and run without real threads or delays.
  • Flutter Rules

    Project rulesflutter

    Free
  • React Native Expo Rules

    Project rulesreact-native

    Free
  • SwiftUI iOS Rules

    Project rulesswift

    Free
  • Flutter State Management

    Skillflutter

    Free