Any official guide for implementing room in multip...
# multiplatform
p
Any official guide for implementing room in multiplatform project? the official documentation is not very clear, they describe some dependencies, but they don't explain how to start using room. For example, they don't explain you if you need to add plugins, they don't explain you what to do with KSP to make room work, which .toml file lines to add, etc... this is the official doc: https://developer.android.com/kotlin/multiplatform/room following it you can't start using room in a KMP project, it lacks steps
a
f
Hi Pablo, > they don't explain you if you need to add plugins Yes, you need to add room gradle plugin and ksp plugin. assuming you have a KMP gradle module called something like
:database
where you'd like to use room, here is what you have to configure in
build.gradle.kts (:database)
Copy code
plugins {
    //..other plugins..
    kotlin("multiplatform")
    id("com.google.devtools.ksp") version "2.0.21-1.0.28" //ksp version
    id("androidx.room") version "2.7.0-alpha11" //room version
    //..other plugins..
}

kotlin {
    androidTarget()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    sourceSets {
        val commonMain by getting {
            dependencies {
                //..other commonMain dependencies..

                // Room dependencies
                val roomVersion = "2.7.0-alpha11"
                implementation("androidx.room:room-runtime:$roomVersion")
                implementation("androidx.sqlite:sqlite-bundled:2.5.0-alpha11")

                //..other commonMain dependencies..
            }
        }
        //.. other sourceSets configuration
    }
}

// Ksp configuration for kmp projects (see also: <https://kotlinlang.org/docs/ksp-multiplatform.html>)
dependencies {
    val roomVersion = "2.7.0-alpha11"
    add("kspAndroid", "androidx.room:room-compiler:$roomVersion")
    add("kspIosSimulatorArm64","androidx.room:room-compiler:$roomVersion")
    add("kspIosX64","androidx.room:room-compiler:$roomVersion")
    add("kspIosArm64", "androidx.room:room-compiler:$roomVersion")
}

// room plugin configuration
room {
    schemaDirectory("$projectDir/schemas") //or any folder you'd like to use
}
Now, this should get you started quickly. You can then improve and clean-up the gradle file by adding toml declaration and use that instead. See the link posted by Alejandro
p
I'm trying with alejandro link, and it gives me this error:
Configuration with name 'kspJvm' not found.
on this line:
Copy code
add("kspJvm", libs.androidx.room.compiler)
also, this block appears entirely in red, like non recognized:
Copy code
room {
    schemaDirectory("$projectDir/schemas")
}
take in mind that this is a compose desktop project, so I think kspJvm is the only necessary ksp
well, changing kspJm to
Copy code
kspDesktop
solved the issues, now tomorrow will continue trying make it working thank you
@Alejandro Rios your kt file for defining the database is very big and complex:
Copy code
@Database(
    entities = [CachedGeneralTestModel::class, CachedTestDateModel::class],
    version = 1,
    exportSchema = true,
)
@TypeConverters(DateConverter::class)
@ConstructedBy(TestDatabaseConstructor::class)
abstract class TestDatabase : RoomDatabase() {
    abstract fun cachedGeneralTestDAO(): CachedGeneralTestDAO
}

@Suppress("NO_ACTUAL_FOR_EXPECT")
internal expect object TestDatabaseConstructor : RoomDatabaseConstructor<TestDatabase>

const val dbFileName = "test.db"

fun <T : RoomDatabase> RoomDatabase.Builder<T>.setDefaults(): RoomDatabase.Builder<T> = this.apply {
    setJournalMode(RoomDatabase.JournalMode.WRITE_AHEAD_LOGGING) //enabling WAL <https://www.sqlite.org/wal.html>
    setDriver(BundledSQLiteDriver())
    addCallback(TestDatabaseCallback())
    addFallbackInDebugOnly()
    setQueryCoroutineContext(Dispatchers.IO)
}

private fun <T : RoomDatabase> RoomDatabase.Builder<T>.addFallbackInDebugOnly(): RoomDatabase.Builder<T> {
    if (BuildKonfig.isDebug) { // we are using BuildKonfig as a demonstration reference
        fallbackToDestructiveMigration(true)
    }

    return this
}
can it be reduced to a simple abstract class and method like in android? for example:
Copy code
@Database(entities = [MyModel::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun myModelDao(): MyModelDao
}
a
The article is not mine, the author is @Hristijan
p
and what do you think? seems to be super complex
cannot be reduced to 4 lines like in android? are all that stuff necessary?
not sure if it's the correct way to do it
a
@Pablo did you try to use your
simple abstract definition
with no issues? if so, then you're good to go. The article might seem complex but is because the setup concept he wants to show, a setup that handles
Write-Ahead logging
and a
DatabaseBuilder
, here's a different approach