Writes idiomatic modern Kotlin for Android: Kotlin 2.x, null safety, sealed and value classes, structured coroutines with injected dispatchers, Flow and StateFlow, Jetpack Compose state and stability, Hilt, Room, KSP, Gradle version catalogs, R8 and tests with MockK and Turbine, plus Android 15 and 16 changes. Use when writing or refactoring Kotlin ViewModels, composables, repositories, DAOs, DI modules or build.gradle.kts files.
---
name: mobile-kotlin
description: "Writes idiomatic modern Kotlin for Android: Kotlin 2.x, null safety, sealed and value classes, structured coroutines with injected dispatchers, Flow and StateFlow, Jetpack Compose state and stability, Hilt, Room, KSP, Gradle version catalogs, R8 and tests with MockK and Turbine, plus Android 15 and 16 changes. Use when writing or refactoring Kotlin ViewModels, composables, repositories, DAOs, DI modules or build.gradle.kts files."
license: MIT
metadata:
author: Ahmad Hussain
version: "1.0.1"
pack: mobile-dev-kit
---
# Kotlin for Android
Write Kotlin the way a senior Android engineer would in a current codebase: null-safe, immutable by default,
structured concurrency everywhere, Compose that skips work it does not need to do, and a build that is
reproducible from the version catalog. Prefer the boring, documented API over a clever one.
## When to use
- Writing new Kotlin code in an Android app: ViewModels, use cases, repositories, composables, DAOs, workers.
- Refactoring Java, callback or RxJava code to coroutines and Flow.
- Migrating XML screens to Jetpack Compose, kapt to KSP, Groovy Gradle to Kotlin DSL.
- Fixing recomposition, lifecycle, coroutine-leak or nullability bugs.
- Updating code for a new `targetSdk` (edge-to-edge, predictive back, foreground service rules).
Not for: architecture choice (use `mobile-mvvm`, `mobile-mvi`), a security audit (use `mobile-security`),
legacy Java maintenance (use `mobile-java`), or reviewing someone else's diff (use `mobile-code-review`).
## Workflow
1. Read the project before writing code.
- `gradle/libs.versions.toml`: Kotlin, AGP, Compose BOM, coroutines, Hilt, Room versions.
- Module `build.gradle.kts`: `minSdk`, `targetSdk`, `compileSdk`, KSP or kapt, Compose enabled.
- Existing conventions: package layout, base classes, dispatcher qualifiers, error types, test style.
2. Match the existing stack. Do not add a library the project does not use without saying so.
Do not change `minSdk`, `targetSdk` or `compileSdk` without asking.
3. Load the reference for the area being touched:
[Coroutines and Flow](references/coroutines-flow.md), [Compose](references/compose.md),
[Hilt and Room](references/hilt-room.md), [Build and tooling](references/build.md),
[Platform changes](references/platform.md), [Testing](references/testing.md).
4. Model first: domain types, UI state, events, errors as sealed or data types. Then data layer, then
ViewModel, then UI.
5. Write the code following the Rules below. Keep functions small, `private` by default, and public API
explicit about types.
6. Add or update unit tests for new domain and data logic, and a Compose test for new interactive UI.
7. Run the build, the tests and the linters (`./gradlew detekt ktlintCheck testDebugUnitTest` or the
project's equivalents). Report failures honestly; do not declare done on a red build.
8. Summarize what changed, any new dependency, and any manifest, permission or SDK impact.
## Baseline stack
| Area | Default | Avoid in new code |
|---|---|---|
| Language | Kotlin 2.x, K2 compiler | Kotlin 1.x language versions, synthetic view imports |
| UI | Jetpack Compose + Material 3 | New XML layouts, `findViewById` |
| State | `StateFlow` + immutable data class | `LiveData` in new ViewModels, mutable public state |
| Async | Coroutines + Flow | `AsyncTask`, raw threads, new RxJava |
| DI | Hilt with constructor injection | Manual singletons, service locators |
| Persistence | Room (suspend + Flow), DataStore | `SharedPreferences` for new code |
| Serialization | kotlinx.serialization | Reflection-based parsers without keep rules |
| Codegen | KSP | kapt where a KSP processor exists |
| Build | Kotlin DSL + `libs.versions.toml` | Hardcoded versions in module files |
## Rules
### 1. No `!!`. Fail with a message or handle the null
```kotlin
// BAD: NullPointerException with no context
val id = savedStateHandle.get<String>("id")!!
// GOOD: invariant violation explains itself
val id = requireNotNull(savedStateHandle.get<String>("id")) { "id nav argument missing" }
// GOOD: absence is a normal case
val name = user?.displayName ?: context.getString(R.string.anonymous)
```
- `checkNotNull` for state invariants, `requireNotNull` for arguments. Java platform types are nullable.
- `lateinit` only for framework-injected fields (`@Inject lateinit var` in an entry point), never for data.
### 2. State is immutable; variants are sealed
```kotlin
// BAD: mutable fields, impossible states (loading and error at once)
class ProfileState { var loading = false; var error: String? = null; var user: User? = null }
// GOOD
sealed interface ProfileUiState {
data object Loading : ProfileUiState
data class Content(val user: User, val isRefreshing: Boolean = false) : ProfileUiState
data class Error(val reason: UiError) : ProfileUiState
}
```
- `when` over a sealed type has no `else`, so a new variant is a compile error, not a silent fallthrough.
- Use `val` and read-only collection types in data classes. Change state with `copy`.
- Guard conditions in `when` (stable since Kotlin 2.2) replace nested `if` inside a branch:
`is ProfileUiState.Content if it.isRefreshing -> ...`.
### 3. Typed IDs use value classes
```kotlin
// BAD: easy to swap arguments, compiler cannot help
fun assign(deviceId: String, userId: String)
assign(user.id, device.id) // compiles, wrong
// GOOD: zero-cost wrapper on the JVM
@JvmInline value class DeviceId(val value: String)
@JvmInline value class UserId(val value: String)
fun assign(deviceId: DeviceId, userId: UserId)
```
- Keep value classes in the domain layer. Map to primitives at DTO and entity boundaries unless the
library supports them (kotlinx.serialization does).
- Validate in `init` when the wrapper carries an invariant: `init { require(value.isNotBlank()) }`.
### 4. Every coroutine has an owner. No `GlobalScope`
```kotlin
// BAD: outlives the screen, cannot be cancelled or tested
GlobalScope.launch { repository.sync() }
// GOOD: tied to the ViewModel
viewModelScope.launch { repository.sync() }
// GOOD: work that must outlive a screen uses an injected application scope or WorkManager
class SyncTrigger @Inject constructor(@ApplicationScope private val scope: CoroutineScope)
```
- Repositories and use cases expose `suspend` functions or `Flow`. They do not launch and return `Unit`.
- `runBlocking` only in `main` functions and legacy bridges, never on the main thread.
### 5. Suspend functions are main-safe; dispatchers are injected
```kotlin
// BAD: hardcoded dispatcher, caller must remember to switch threads
class FileStore { fun read(): ByteArray = file.readBytes() }
// GOOD
class FileStore @Inject constructor(
@IoDispatcher private val io: CoroutineDispatcher,
) {
suspend fun read(): ByteArray = withContext(io) { file.readBytes() }
}
```
- The function that does the blocking work switches context. Callers never wrap it again.
- `Dispatchers.Default` for CPU work, IO dispatcher for blocking IO. Main is only for UI.
### 6. `CancellationException` is always rethrown
```kotlin
// BAD: runCatching and catch-all both swallow cancellation
val result = runCatching { api.fetch() }
try { api.fetch() } catch (e: Exception) { log(e) }
// GOOD
try {
api.fetch()
} catch (e: CancellationException) {
throw e
} catch (e: IOException) {
_state.update { it.copy(error = UiError.Network) }
}
```
Catch the narrowest exception that can be handled. Use `ensureActive()` in long loops.
### 7. Use `supervisorScope` when children fail independently
```kotlin
// BAD: one failed widget cancels all the others
suspend fun loadDashboard() = coroutineScope {
val a = async { api.weather() }
val b = async { api.news() }
Dashboard(a.await(), b.await())
}
// GOOD: each child failure is handled at its await
suspend fun loadDashboard() = supervisorScope {
val a = async { api.weather() }
val b = async { api.news() }
Dashboard(weather = a.awaitOrNull(), news = b.awaitOrNull())
}
```
`awaitOrNull` is a local helper that catches the expected exception types and rethrows cancellation.
Use `coroutineScope` when the result is all-or-nothing.
### 8. Mutable state is private; updates are atomic
```kotlin
// BAD: public mutable state and a read-modify-write race
val state = MutableStateFlow(UiState())
fun select(id: ItemId) { state.value = state.value.copy(selected = id) }
// GOOD
private val _state = MutableStateFlow(UiState())
val state: StateFlow<UiState> = _state.asStateFlow()
fun select(id: ItemId) = _state.update { it.copy(selected = id) }
```
### 9. Cold flows become state once, and UI collects with the lifecycle
```kotlin
// BAD: new upstream per collector, keeps running in the background
val items: Flow<List<Item>> get() = repository.observeItems()
val items by viewModel.items.collectAsState(initial = emptyList())
// GOOD
val items: StateFlow<List<Item>> = repository.observeItems()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
val items by viewModel.items.collectAsStateWithLifecycle()
```
- `WhileSubscribed(5_000)` keeps the upstream alive across configuration changes, stops it in background.
- Views and Fragments collect inside `repeatOnLifecycle(Lifecycle.State.STARTED)`.
- Details on `flowOn`, `catch`, `combine` and SharedFlow: [Coroutines and Flow](references/coroutines-flow.md).
### 10. Composables are stateless; state is hoisted
```kotlin
// BAD: ViewModel passed deep, no modifier, not previewable
@Composable
fun DeviceRow(vm: DevicesViewModel, id: DeviceId) { /* reads vm.state, calls vm.connect */ }
// GOOD
@Composable
fun DeviceRow(
device: DeviceUi,
onConnect: (DeviceId) -> Unit,
modifier: Modifier = Modifier,
) {
Row(modifier.clickable { onConnect(device.id) }) { Text(device.name) }
}
```
- Only the screen-level composable touches the ViewModel. Below it: state in, lambdas out.
- `modifier: Modifier = Modifier` is the first optional parameter and is applied to the root only.
- `rememberSaveable` for user input that must survive rotation and process death.
### 11. Parameters are stable so composables can skip
```kotlin
// BAD: List is an interface the compiler treats as unstable; a new list instance
// every emission forces recomposition of every row
data class FeedUi(val posts: List<PostUi>)
// GOOD
@Immutable
data class FeedUi(val posts: ImmutableList<PostUi>)
// built with posts.map(::toUi).toImmutableList()
```
- Strong skipping is the default since Kotlin 2.0.20: unstable parameters are compared by instance,
stable ones by `equals`. Recreating objects on every emission still defeats skipping.
- Do not sprinkle `@Stable` to silence reports. Annotate only types that keep the contract.
### 12. Effects are keyed by what they read
```kotlin
// BAD: keeps the first deviceId forever
LaunchedEffect(Unit) { viewModel.connect(deviceId) }
// GOOD: restarts when deviceId changes
LaunchedEffect(deviceId) { viewModel.connect(deviceId) }
// GOOD: long-lived effect that must call the latest lambda without restarting
val currentOnTimeout by rememberUpdatedState(onTimeout)
LaunchedEffect(Unit) { delay(30_000); currentOnTimeout() }
```
- Listeners and observers registered in composition go in `DisposableEffect` with `onDispose`.
- No side effects in the composable body. More in [Compose](references/compose.md).
### 13. Dependencies come through the constructor
```kotlin
// BAD: Activity context in a singleton, hidden dependency
object Prefs { lateinit var context: Context }
// GOOD
@HiltViewModel
class SettingsViewModel @Inject constructor(
private val settings: SettingsRepository,
private val savedStateHandle: SavedStateHandle,
) : ViewModel()
@Singleton
class SettingsRepository @Inject constructor(
@ApplicationContext private val context: Context,
)
```
- Bind interfaces with `@Binds` in an abstract module; `@Provides` only for third-party types.
- Scope annotations match lifetime. Details: [Hilt and Room](references/hilt-room.md).
### 14. Room DAOs are suspend or Flow, and schemas migrate
```kotlin
// BAD: blocking DAO, and user data wiped on the next schema change
@Query("SELECT * FROM device") fun all(): List<DeviceEntity>
Room.databaseBuilder(ctx, AppDb::class.java, "app.db").fallbackToDestructiveMigration()
// GOOD
@Query("SELECT * FROM device ORDER BY name") fun observeAll(): Flow<List<DeviceEntity>>
@Upsert suspend fun upsert(device: DeviceEntity)
@Database(entities = [DeviceEntity::class], version = 3, exportSchema = true)
abstract class AppDb : RoomDatabase()
```
- Every version bump ships a `Migration` or `AutoMigration` and a migration test.
### 15. KSP, version catalog, and the Compose compiler plugin
```kotlin
// BAD: kapt and hardcoded versions
plugins { kotlin("kapt") }
dependencies { kapt("com.google.dagger:hilt-compiler:2.51") }
// GOOD
plugins {
alias(libs.plugins.ksp)
alias(libs.plugins.hilt)
alias(libs.plugins.kotlin.compose)
}
dependencies { ksp(libs.hilt.compiler) }
```
- `libs.plugins.kotlin.compose` maps to `org.jetbrains.kotlin.plugin.compose`, versioned with Kotlin.
- Release builds enable R8 and add keep rules for reflection. Details: [Build and tooling](references/build.md).
### 16. Code is ready for the current `targetSdk`
```kotlin
// BAD: content under the status bar when targeting SDK 35+, and onBackPressed is deprecated
// and no longer called when targeting SDK 36 with predictive back
setContent { LazyColumn { items(rows, key = { it.id }) { RowItem(it) } } }
override fun onBackPressed() { if (editing) cancelEdit() else super.onBackPressed() }
// GOOD
enableEdgeToEdge()
setContent {
Scaffold { innerPadding ->
LazyColumn(contentPadding = innerPadding) { items(rows, key = { it.id }) { RowItem(it) } }
}
BackHandler(enabled = editing) { cancelEdit() }
}
```
Foreground service types, large-screen orientation rules and more: [Platform changes](references/platform.md).
### 17. New logic ships with deterministic tests
```kotlin
// BAD: real dispatcher and sleep, slow and flaky
@Test fun loads() { vm.load(); Thread.sleep(1000); assertEquals(3, vm.state.value.items.size) }
// GOOD: virtual time, Turbine for the flow
@Test fun loads() = runTest {
coEvery { repo.items() } returns listOf(a, b, c)
val vm = ItemsViewModel(repo)
vm.state.test {
assertEquals(ItemsUiState.Loading, awaitItem())
assertEquals(3, (awaitItem() as ItemsUiState.Content).items.size)
}
}
```
Main dispatcher rule, fakes vs mocks and Compose tests: [Testing](references/testing.md).
## Anti-patterns
- `!!` "because it cannot be null here". If it truly cannot, the type should say so.
- `GlobalScope`, `runBlocking` on the main thread, or a `CoroutineScope(...)` field nobody cancels.
- `runCatching` or `catch (e: Exception)` around suspend calls without rethrowing cancellation.
- `MutableStateFlow` or `MutableList` exposed from a ViewModel.
- One-off events kept in state that the UI never acknowledges, so the last navigation or toast replays after
rotation. Clear the event once handled (see `mobile-mvvm`).
- ViewModels importing `android.view`, `Context` (non-application), or Compose UI types.
- Passing a ViewModel into leaf composables, or `hiltViewModel()` called below the screen level.
- `LaunchedEffect(Unit)` reading a parameter that changes.
- `derivedStateOf` wrapping a value that changes as often as its input. It adds cost, not savings.
- Lazy lists without `key`, or keyed by index.
- New kapt processors, versions hardcoded in module files, `kotlinOptions {}` instead of `compilerOptions {}`.
- `fallbackToDestructiveMigration()` in release builds.
- Secrets, tokens or API keys in `BuildConfig`, `strings.xml` or source. Escalate to `mobile-security`.
- Logging device identifiers, emails or tokens with `Log.d`. They are personal data.
- Silently bumping SDK levels or adding manifest permissions without calling it out.
## Checklist
- [ ] Project versions, SDK levels and conventions read before writing code
- [ ] No `!!`; nullable inputs handled or failed with `requireNotNull` and a message
- [ ] UI state is an immutable data class or sealed interface; `when` is exhaustive without `else`
- [ ] Typed IDs are value classes where mix-ups are possible
- [ ] Every coroutine has an owner scope; no `GlobalScope`
- [ ] Dispatchers injected; blocking work wrapped in `withContext` at the source
- [ ] `CancellationException` rethrown in every catch around suspend code
- [ ] Mutable flows private; updates use `update {}`
- [ ] Cold flows converted with `stateIn(..., WhileSubscribed(5_000), ...)`; UI uses `collectAsStateWithLifecycle`
- [ ] Composables stateless below the screen; `modifier` parameter first optional and applied to root
- [ ] Composable parameters stable; immutable collections where lists are passed
- [ ] Effects keyed correctly; `rememberUpdatedState` for long-lived lambdas; listeners in `DisposableEffect`
- [ ] Lazy lists have stable `key` and `contentType` for mixed rows
- [ ] Hilt constructor injection; `@ApplicationContext` only; scopes match lifetimes
- [ ] Room DAOs suspend or Flow; migrations and `exportSchema = true`
- [ ] KSP instead of kapt; new dependencies in `libs.versions.toml`
- [ ] R8 keep rules for reflection or serialization added and release build verified
- [ ] Edge-to-edge insets and predictive back handled for the current `targetSdk`
- [ ] Unit tests for new domain and data logic; tests and linters pass
## Related skills
- `mobile-mvvm`, `mobile-mvi` -- where ViewModels, state and events fit in the architecture
- `mobile-design-patterns` -- repository, use case, adapter and factory patterns in Kotlin
- `mobile-code-review` -- reviewing Kotlin changes against these rules
- `mobile-security` -- storage, network, crypto and logging rules for the same code
- `mobile-ci-cd` -- running detekt, ktlint, tests and release builds in CI
- `mobile-java` -- legacy Java code and Java-Kotlin interop
- `mobile-kmp` -- sharing Kotlin code with iOS
- `mobile-ble` -- Android Bluetooth Low Energy rules
- `mobile-testing` -- cross-platform test strategy and screenshot tests
- `mobile-performance` -- Baseline Profiles, Macrobenchmark and Android vitals
don't have the plugin yet? install it then click "run inline in claude" again.
Write Kotlin the way a senior Android engineer would in a current codebase: null-safe, immutable by default, structured concurrency everywhere, Compose that skips work it does not need to do, and a build that is reproducible from the version catalog. Prefer the boring, documented API over a clever one. Use this skill when writing new Kotlin code in an Android app (ViewModels, use cases, repositories, composables, DAOs, workers), refactoring Java or callback code to coroutines and Flow, migrating XML to Compose, or fixing recomposition, lifecycle, coroutine-leak or nullability bugs. Skip this for architecture choice (use mobile-mvvm, mobile-mvi), security audit (use mobile-security), legacy Java maintenance (use mobile-java), or reviewing diffs (use mobile-code-review).
gradle/libs.versions.toml (Kotlin, AGP, Compose BOM, coroutines, Hilt, Room versions), module build.gradle.kts (minSdk, targetSdk, compileSdk, KSP or kapt, Compose enabled), and existing code conventions (package layout, base classes, dispatcher qualifiers, error types, test style).minSdk, targetSdk or compileSdk without asking.libs.versions.toml. Avoid Kotlin 1.x language versions, synthetic view imports, LiveData in new code, mutable public state, AsyncTask, raw threads, new RxJava, manual singletons, service locators, SharedPreferences for new code, reflection-based parsers without keep rules, kapt where KSP exists, hardcoded versions in module files.Read the project before writing code. Check gradle/libs.versions.toml for Kotlin, AGP, Compose BOM, coroutines, Hilt, and Room versions. Examine the module build.gradle.kts for minSdk, targetSdk, compileSdk, whether KSP or kapt is active, and if Compose is enabled. Study existing conventions: package layout, base classes, dispatcher qualifiers, error types, and test style.
Match the existing stack. Do not add a library the project does not use without saying so. Do not change minSdk, targetSdk, or compileSdk without asking.
Load the reference for the area being touched. If writing or refactoring coroutines, Flow, or state management, read Coroutines and Flow. For Compose UI, read Compose. For DI or persistence, read Hilt and Room. For build config or tooling, read Build and tooling. For SDK 15/16 changes, read Platform changes. For tests, read Testing.
Model first: define domain types, UI state, events, and errors as sealed interfaces or immutable data classes. Then code the data layer (repositories, DAOs), then the ViewModel, then the UI.
!! and no ambiguous states (do not allow both loading and error true at once).Write the code following the 17 Rules below. Keep functions small, private by default, and public API explicit about types. Inject all dependencies (Hilt qualifiers, dispatchers, repositories) through constructors. Mark composables stateless below the screen level. Use StateFlow for state, Flow for cold observables. Ensure all coroutines have an owner scope (never GlobalScope). Catch CancellationException and rethrow it. Make state updates atomic with update {}. Hoist composable state.
Add or update unit tests for new domain and data logic. Add a Compose test for new interactive UI. Use runTest for virtual time, mock external dependencies with coEvery, and assert final state with Turbine's test {} for flows. Ensure tests are deterministic (no sleeps, no real dispatchers).
src/test/kotlin, Compose tests in src/androidTest/kotlin, all passing.Run the build and all tests. Execute ./gradlew detekt ktlintCheck testDebugUnitTest (or the project's equivalents). Report failures honestly. Do not declare done on a red build. Fix linter errors, type errors, and test failures before submitting.
Summarize what changed, any new dependency, and any manifest, permission, or SDK impact. If adding a new Hilt module, say so. If bumping targetSdk, note the edge-to-edge or permission changes. If adding a new Room entity, note the schema version and migration.
If the input is nullable (platform type or optional parameter): Use checkNotNull for invariants (programmer error if null) or requireNotNull for arguments (caller error if null). Never use !! without a message. If absence is a normal case, use the Elvis operator (?. and ?:) to provide a default.
requireNotNull with a message.If modeling UI state: Use sealed interfaces or data classes, never mutable fields. If the state can be loading, with content, or in error, define three sealed subtypes. Use when without else to force exhaustive handling. If a state can transition (e.g., from Content to Content with isRefreshing true), use copy to create the new state.
Content(user, isRefreshing = false) and Error(reason).If a function parameter is a simple string or number that can be confused with another (e.g., deviceId vs userId): Wrap it in a value class (zero-cost on the JVM). Keep value classes in the domain layer; map to primitives at DTO/entity boundaries unless the serialization library supports them.
init block: init { require(value.isNotBlank()) { "ID cannot be blank" } }.If launching a coroutine: Always give it an owner scope (never GlobalScope). If the work is tied to a ViewModel, use viewModelScope. If it must outlive a screen (sync, cleanup), use an injected CoroutineScope qualified with @ApplicationScope or delegate to WorkManager.
suspend function or Flow; do not launch inside and return Unit.If a suspend function does blocking IO or CPU work: Switch context at the source, never at the call site. Inject a CoroutineDispatcher (qualified as @IoDispatcher, @DefaultDispatcher, or similar) and wrap the blocking work in withContext(dispatcher).
@IoDispatcher. For heavy computation, use @DefaultDispatcher. Never use the main dispatcher for blocking work.If catching exceptions around suspend code: Always catch and rethrow CancellationException. Narrow the other catch blocks to the exceptions that can actually be handled. If catching Exception or using runCatching, place catch (e: CancellationException) throw e before any broader catch.
runCatching around suspend code without an explicit cancellation check afterward.If a coroutine scope needs to launch multiple independent tasks (e.g., fetching weather and news in parallel): Use supervisorScope so a failure in one task does not cancel the others. Use coroutineScope if all-or-nothing is needed (one failure fails everything).
awaitOrNull() that catches expected exceptions and rethrows cancellation.If managing mutable state in a ViewModel: Use MutableStateFlow, mark it private, expose it as a read-only StateFlow via asStateFlow(), and update it atomically with update {} (never value = to avoid races).
stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) to keep the upstream alive across configuration changes but stop it when the app is in the background.If a composable needs to observe a Flow from the ViewModel: Use collectAsStateWithLifecycle() on Android (Compose 1.6+) to collect only when the screen is started, stopping when paused or destroyed. If not on Android or on older Compose, use repeatOnLifecycle(Lifecycle.State.STARTED) with launchIn(lifecycleScope).
collectAsState(initial = ...) without lifecycle awareness; it keeps the upstream alive in the background.If a composable receives state from above: Make it stateless (no mutable state, no ViewModel reference). Pass only the data it needs and lambda callbacks for events. Apply modifier: Modifier = Modifier as the first optional parameter and apply it only to the root composable. Use rememberSaveable only for user input that must survive rotation and process death.
If a composable parameter is a list or other collection: Use an immutable type (e.g., ImmutableList from kotlinx.collections.immutable) so instances do not change between emissions, and the Compose compiler can skip recomposition of child rows. Build the immutable collection when converting from the data layer: posts.map(::toUi).toImmutableList().
key and contentType parameters to avoid recomposing rows with the same logical identity.If using LaunchedEffect or other effects: Key the effect by the values it reads. If the key changes, the effect restarts. If a long-lived effect needs to call a lambda that changes, use rememberUpdatedState to capture the latest version without restarting. Register listeners or observers in DisposableEffect with an onDispose block to clean them up.
LaunchedEffect(Unit) and read a parameter that changes; this misses updates.If the codebase uses Hilt: Use constructor injection for all classes (ViewModels, repositories, services). Bind interfaces with abstract @Binds methods; use @Provides only for third-party types. Scope the dependency to the appropriate lifetime (ViewModelScoped, Singleton, etc.). Use @ApplicationContext only; never inject Context without it.
If using Room: Make DAOs suspend-based (not blocking) or return Flow for reads. Use @Upsert for inserts/updates. Mark the database with @Database(exportSchema = true) and place the JSON schema in version control. For every version bump, ship a Migration or AutoMigration and a migration test. Never use fallbackToDestructiveMigration() in release builds.
If writing or updating build.gradle.kts: Use Kotlin DSL, alias plugins and dependencies from libs.versions.toml with alias(libs.plugins.xxx) and libs.xxx, never hardcode versions. For annotation processors, use KSP (ksp plugin) instead of kapt. Add Compose compiler plugin with alias(libs.plugins.kotlin.compose), versioned with Kotlin. For release builds, enable R8 and add keep rules for reflection or serialization.
libs.versions.toml first, then reference it in build.gradle.kts with alias(...).If the code targets Android 15 (SDK 35) or higher: Enable edge-to-edge layouts with enableEdgeToEdge() and apply insets to content via Scaffold or manual windowInsetsPadding. Use BackHandler instead of overriding onBackPressed (which is deprecated for SDK 36+). Check Platform changes for foreground service types, large-screen orientation rules, and other SDK-specific requirements.
targetSdk, note the new permission rules (e.g., clipboard access, file access, foreground services) and update the manifest and code accordingly.If writing a test: Use runTest for deterministic virtual time (no real delays). Mock external dependencies with coEvery { ... } returns ... from MockK. For Flow testing, use Turbine's test { ... } block. Assert the final or intermediate state, not timing. Never use Thread.sleep or real Dispatchers in tests.
If the code will ship to production: Report all changes (new dependencies, SDK bumps, permission additions, manifest changes) honestly in the summary. Do not hide breaking changes or permission escalations. If a secret or token appears in the code, escalate immediately to the security skill (do not leave it in).
src/main/kotlin/** follow the 17 Rules. No !! without explanation, no GlobalScope, no mutable public state, no CancellationException swallowed in a catch-all, composables stateless below the screen level, all state immutable and sealed.src/test/kotlin/** using runTest, MockK, and Turbine. All Compose UI with new interactivity has tests in src/androidTest/kotlin/**. Tests run deterministically with virtual time, mock external IO, and assert final state without timing assertions.gradle/libs.versions.toml has all new dependency versions. build.gradle.kts aliases all plugins and dependencies, uses KSP instead of kapt, and enables the Compose compiler plugin. No hardcoded versions in module files. R8 keep rules added if reflection or serialization is used.targetSdk. No secrets in BuildConfig, strings.xml, or source; escalate to mobile-security../gradlew build completes with no errors, no warnings, no test failures../gradlew detekt ktlintCheck report no violations. ktlintFormat