Room 3: Migrating to the New Major Version with wasmJs and Coroutine Support
Room 3 introduces the androidx.room3 package, completely eliminating the dependency on the Android-specific SQLite Driver API. The public API now uses documented alternatives for connection handling.
Example of transitioning from the old style to the new:
// Before
roomDatabase.query("select * from users").use { cursor -> ... }
// After
roomDatabase.useReaderConnection { connection ->
connection.usePrepared("select * from users") { stmt -> ... }
}
Code generation has migrated from KAPT to KSP. All internal DAO logic has been rewritten to use coroutines, simplifying asynchronous work in Kotlin Multiplatform.
The key upgrade is wasmJs support. The KMP version of Room is now compatible with WebAssembly, opening up possibilities for mobile web development.
Deprecated features have been removed, cleaning up the codebase of legacy code.
Steps for Migrating to Room 3
Migration proceeds without radical architectural changes. The main steps are:
- Update the plugin and dependencies in build.gradle.kts:
// root build.gradle.kts
plugins {
id("androidx.room3") version "3.0.0-alpha02" apply false
}
// lib build.gradle.kts
plugins {
id("androidx.room3")
}
kotlin {
sourceSets {
commonMain {
val room_version = "3.0.0-alpha02"
implementation("androidx.room3:room3-runtime:$room_version")
ksp("androidx.room3:room3-compiler:$room_version")
}
}
}
room3 {
schemaDirectory("$projectDir/schemas")
}
- Replace imports: RoomDatabase, @Entity, and other classes are now in the room3 package.
- Build the project and fix any errors. Suspend functions have been added to DAOs, and method naming has changed.
- Check the database schemas in the schemas directory — Room 3 requires them for compilation.
After migration, test on all targets: JVM, Android, iOS, wasmJs.
Advantages for KMP Development
- Coroutines in core: Suspend functions are the default in DAOs, eliminating the need for Flow or LiveData.
- wasmJs: Full WebAssembly support allows running Room in the browser without workarounds.
- KSP instead of KAPT: Faster compilation, fewer artifacts.
Performance comparison of code generation (based on typical KMP projects):
| Metric | Room 2 (KAPT) | Room 3 (KSP) |
|----------------------|---------------|---------------|
| Compilation time | 45 sec | 22 sec |
| APK size | 1.2 MB | 0.9 MB |
| Runs on wasm | No | Yes |
Data is approximate and depends on project size.
Key Points
- New room3 package with a clean API free from Android dependencies.
- Full coroutine and KSP support for KMP.
- wasmJs integration for web applications.
- Migration is straightforward: update dependencies + fix imports.
- Alpha version: wait for the stable release for production use.
Implementation Recommendations
For mid-level/senior developers: start with greenfield KMP projects. For legacy projects, postpone migration until 3.0.0-stable.
Test with real-world scenarios:
- Transactions with suspend functions.
- Schema migrations in wasm.
- Query performance in the browser.
Room 3 strengthens its position as a universal ORM for multiplatform, especially with the growth of the wasm ecosystem.
— Editorial Team
No comments yet.