Lesson 05 · Android with Kotlin & Compose
Android: Room, DataStore & Permissions
State that must outlive the app needs real storage: this lesson climbs the persistence ladder from in-memory through Preferences DataStore for small key-value settings to Room over SQLite - entities, DAOs returning Flow, KSP in Gradle, migrations - then adds runtime permissions requested the platform way, rationale before the second ask.
Prerequisites: Android: State, Lifecycle & ViewModels
Learning objectives
By the end of this lesson, you will be able to:
- Choose the right rung on the persistence ladder - in-memory state for right now, DataStore for small key-value settings, Room for structured records you need to query
- Read and write Preferences DataStore the idiomatic way - one instance per file via the delegate, typed keys, suspend edits, and reads as Flows collected in Compose
- Define a Room database as @Entity, @Dao and @Database - suspend mutations, @Query methods returning Flow<List<T>>, compile-time-checked SQL, and KSP wired into Gradle
- Keep database work off the main thread with suspend DAO calls from viewModelScope, collect Room Flows in Compose, and handle schema changes with migrations - treating fallbackToDestructiveMigration as a dev-only crutch
- Request runtime permissions the platform way - manifest declaration, rememberLauncherForActivityResult, and a rationale shown when shouldShowRequestPermissionRationale says the user has denied once