Storing data in local database like a boss: Introducing Room in Compose Multiplatform

Binay Shaw
ProAndroidDev
Published in
3 min readMay 8, 2024

--

Photo by Mr Cup / Fabien Barral on Unsplash

Hi everyone,
So recently we achieved Room Database compatibility in Kotlin Multiplatform or Compose Multiplatform and I already had plans to write a CMP project making good use to newly introduced AndroidX libraries for KMP/CMP.
Let’s get started

Twitter Post by John O’Reilly https://x.com/joreilly/status/1785762700586320174

Installation:

Step 1: Add these libraries in Version Catalog first, i.e. libs.version.toml file

Step 2: Add these dependencies/libs in build.gradle.kts which can be found in composeApp/build.gradle.kts

Once done, we can proceed with the rest of the setup, which is similar to Android.

Step 3: Creating Data class. Making it simple.

Step 4: Creating UserDao.

Edit: Notice, all functions are suspend functions and does not return Flow type here. Using Coroutines flow is a good practice but that does not lets you compile and target for all the targets.

It won’t work for any target as it could either be suspend or Deferred/Async return type.
Code example: https://bit.ly/suspend-fun-error-code-eg
Official Doc: https://bit.ly/suspend-dao-error-room-ofc-doc

Error Log:

Error log if the fun type is suspend and return type if deferred/async

Step 5: Creating Database

We are done with the setup, now we just need to pass databaseBuilder as a parameter in App.kt from all the target sources. (i.e Android, iOS and Desktop)

Your common main code should look something like this:

As said, you should pass the userDao from all the target sources, and here we goooooooo.

For Android:

And pass it in MainActivity.kt like this:

For iOS:

And pass it in MainViewController.kt like this:

For Desktop:

And pass it in MainActivity.kt like this:

That’s it! We are done with the Room setup here.

Now, you can interact with your Room database

You can play around with the other possible interactions like creating, inserting, updating and deleting.

Improvements to Consider
1. Follow any architecture like MVI/MVVM
2. Create repositories and viewModels for clean UI and state management
3. Use Koin or other alternatives for Dependency Injection

That’s it, thank you for reading till the end. I hope I was able to contribute value to this community. 😁

Resources to know more:
Room KMP: https://developer.android.com/kotlin/multiplatform/room
KSP with KMP: https://kotlinlang.org/docs/ksp-multiplatform.html

Video tutorials:
Chris Athanas: https://www.youtube.com/watch?v=2E-3FDRPmYI

Blogs:
John O’Reilly: https://johnoreilly.dev/posts/jetpack_room_kmp/

Repositories:
- https://github.com/binayshaw7777/Knowtify
- https://github.com/joreilly/FantasyPremierLeague
- https://github.com/philipplackner/Room-CMP
- https://github.com/Maragues/kmp-persistence-room
- https://github.com/realityexpander/KMP_Room

Need help? https://www.jetbrains.com/kotlin-multiplatform/

Note: This blog was not written using any AI tools 😉

Where to follow?
Let’s catch up on Twitter or LinkedIn to learn more about me 😁 check out portfolio.

--

--