Add Room and then Room KMP to Android App - Part 2

Add Room and then Room KMP to Android App - Part 2

·

2 min read

In Part 1 of this post, we walked through adding Room to your Android app. Now it is time to make this a Kotlin Multiplatform app with shared business logic. Let’s get started.

There are 2 primary resources for Room KMP:

Room for Kotlin Multiplatform: developer.android.com/kotlin/multiplatform/..

Migrating to Room Multiplatform developer.android.com/training/data-storage..

Start by adding a Kotlin Multiplatform Shared Module to your project. Then you need to add a reference from the app’s build.gradle.kts file.

implementation(project(":shared"))

If you want to jump to this part in the coding journey, you can use this commit: https://github.com/dyor/Room/tree/c197ad9deb46017ffa33600f7203d1768a71b3f0

Add plugins to your shared module’s build file (plugin section):

alias(libs.plugins.ksp)
alias(libs.plugins.room)
//backed in Version Catalog: room = { id = "androidx.room", version.ref = "androidx-room" }

…and include these as top-level dependences:

dependencies {
    add("kspAndroid", libs.androidx.room.compiler)
    add("kspIosSimulatorArm64", libs.androidx.room.compiler)
    add("kspIosX64", libs.androidx.room.compiler)
    add("kspIosArm64", libs.androidx.room.compiler)
}

…and include this as a standalone code block at the bottom of your shared module’s build file….

room {
    schemaDirectory("$projectDir/schemas")
}

Finally move your Room files from the app / main directory into the shared / commonMain directory:

You will end up with something like this: https://github.com/dyor/Room/tree/082e4ed9ad6c60f2b3c86efc5cc878820a178f12

Next up, either in a future post or an addition to this post, we will:

  • Consume this shared business logic module from iOS;

  • Implement CRUD on Android and iOS (e.g., creating, reading, updating, deleting Golfers and Holes - both business logic and supporting native UI).

On to the next:).