android-roomkotlin-multiplatformkotlin-symbol-processing

Is Room KMP support available for a project with no shared UI?


I have suspicion that the shared.build.kts of a KMP project with native UIs , has an issue with the ksp block in ios code .

I do not understand KSP accurately but from my initial investigation it does appear to be something that ios target doesn't have support for.

This is my shared.build.kts file

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
    alias(libs.plugins.ksp)
    alias(libs.plugins.kmpNativeCoroutines)
    alias(libs.plugins.room)
}

kotlin {
    task("testClasses")
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true
        }
    }

    sourceSets {
        commonMain.dependencies {
            implementation(libs.room.runtime)
            implementation(libs.sqlite.bundled)
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "com.testing.roomdbkmp"
    compileSdk = 34
    defaultConfig {
        minSdk = 26
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

room{
    schemaDirectory("$projectDir/schemas")
}
dependencies{
    ksp(libs.room.compiler)
}

This is my TOML

[versions]
agp = "8.4.1"
kotlin = "1.9.23"
compose = "1.6.7"
compose-compiler = "1.5.13"
compose-material3 = "1.2.1"
androidx-activityCompose = "1.9.0"
kmpNativeCoroutines = "1.0.0-ALPHA-28"
ksp = "1.9.23-1.0.20"
ktor = "2.3.10"
room = "2.7.0-alpha03"
sqlite = "2.5.0-SNAPSHOT"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-runtime-android = { module = "androidx.room:room-runtime-android", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
sqlite-bundled = { module = "androidx.sqlite:sqlite-bundled", version.ref = "sqlite" }


[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinCocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
room = { id = "androidx.room", version.ref = "room" }
kmpNativeCoroutines = { id = "com.rickclephas.kmp.nativecoroutines", version.ref = "kmpNativeCoroutines" }

This is the compile time exception I get when i build the ios project

error: java.lang.NoClassDefFoundError: javax/lang/model/type/TypeVisitor
    at androidx.room.compiler.codegen.XTypeName.<clinit>(XTypeName.kt:139)
    at androidx.room.solver.types.PrimitiveColumnTypeAdapter$Companion$Primitive.<clinit>(PrimitiveColumnTypeAdapter.kt:51)
    at androidx.room.solver.types.PrimitiveColumnTypeAdapter$Companion.createPrimitiveAdapters(PrimitiveColumnTypeAdapter.kt:70)
    at androidx.room.solver.TypeAdapterStore$Companion.create(TypeAdapterStore.kt:185)
    at androidx.room.processor.Context$typeAdapterStore$2.invoke(Context.kt:59)
    at androidx.room.processor.Context$typeAdapterStore$2.invoke(Context.kt:55)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at androidx.room.processor.Context.getTypeAdapterStore(Context.kt:55)
    at androidx.room.processor.Context.collectLogs(Context.kt:190)
    at androidx.room.DatabaseProcessingStep.process(DatabaseProcessingStep.kt:66)
    at androidx.room.compiler.processing.CommonProcessorDelegate.processRound(XBasicAnnotationProcessor.kt:132)
    at androidx.room.compiler.processing.ksp.KspBasicAnnotationProcessor.process(KspBasicAnnotationProcessor.kt:62)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$8$1.invoke(KotlinSymbolProcessingExtension.kt:310)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension$doAnalysis$8$1.invoke(KotlinSymbolProcessingExtension.kt:308)
    at com.google.devtools.ksp.AbstractKotlinSymbolProcessingExtension.handleException(KotlinSymbolProcessingExtension.kt

Solution

  • Run into the same error. Doc say

    Warning: As of Kotlin 1.9.20, you must add the property kotlin.native.disableCompilerDaemon = true to the gradle.properties configuration file for Room's KSP processor to function properly. For more information, see https://youtrack.jetbrains.com/issue/KT-65761.

    Just have to add kotlin.native.disableCompilerDaemon=true to gradle.properties file

    See https://developer.android.com/kotlin/multiplatform/room#setting-dependencies