i try to room integrate kmp project(android, ios, ...
# multiplatform
m
i try to room integrate kmp project(android, ios, desktop) desktop version not work main.kt
Copy code
fun main() = application {
    Window(
        onCloseRequest = ::exitApplication,
        title = "News app",
    ) {
        val roomDatabase = createRoomDatabase()

        Text("Hello world")
    }
}

fun createRoomDatabase(): NewsDatabase {
    val dbFile = File(System.getProperty("java.io.tmpdir"), "my_room.db")
    return Room.databaseBuilder<NewsDatabase>(name = dbFile.absolutePath,)
        .setDriver(BundledSQLiteDriver())
        .build()
}
NewsDatabase.kt
Copy code
package data.local

import androidx.room.Database
import androidx.room.RoomDatabase
import data.model.Article

@Database(entities = [Article::class],version = 1, exportSchema = false)
abstract class NewsDatabase : RoomDatabase() {
    abstract fun newsDao(): NewsDao
}
c
have you configured KSP to run against all targets?
That error is actually very good, I do have to say however the docs for Room KMP do completely miss setting up gradle correctly: https://github.com/android/kotlin-multiplatform-samples/blob/main/Fruitties/shared/build.gradle.kts#L88
m
thanks i solve this erro
Copy code
kotlin {

    sourceSets.commonMain {
        kotlin.srcDir("build/generated/ksp/metadata")
    }
}
Copy code
dependencies {
// ios laptop use
//    ksp(libs.androidx.room.compiler)

// ios laptop use
//    add("kspAndroid", libs.androidx.room.compiler)
//    add("kspIosSimulatorArm64", libs.androidx.room.compiler)
//    add("kspIosX64", libs.androidx.room.compiler)
//    add("kspIosArm64", libs.androidx.room.compiler)
//

// this is for windows laptop
    // Room
    add("kspCommonMainMetadata", libs.androidx.room.compiler)
}
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().configureEach {
    if (name != "kspCommonMainKotlinMetadata" ) {
        dependsOn("kspCommonMainKotlinMetadata")
    }
}
c
Not sure why you have all that metadata stuff, I just use
add("kspDesktop", libs.androidx.room.compiler)
My Desktop is called desktop so make sure your names match
m
ok i will try it
not work
image.png
c
AS output is generally useless, always look at the top level tree to get the full compile log
Also if you added that kspDesktop, you can probably remove:
Copy code
kotlin {

    sourceSets.commonMain {
        kotlin.srcDir("build/generated/ksp/metadata")
    }
}
but I'm guessing based on snippets you've sent
m
ok i will check after tell you
thanks for time
well. thanks for help.
c
That work? Also if you provide the actual compile error that would help
For reference this is my config:
Copy code
//...
kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies {
                api(libs.kotlinx.datetime)
                api(libs.kotlinx.serialization.json)

                api(libs.androidx.room.runtime)
                implementation(libs.kotlinx.serialization.json.okio)
                implementation(libs.androidx.sqlite.bundled)

                implementation(projects.coreTypes)
                implementation(projects.coreUtil)
            }
        }
    }
}

dependencies {
    add("kspAndroid", libs.androidx.room.compiler)
    add("kspDesktop", libs.androidx.room.compiler)
}

room {
    schemaDirectory("$projectDir/schemas")
}
(just the room stuff)
Obviouly no iOS target
m
ok i am dm this file code