【Compose Multiplatform】Using ROOM Database in CMP - [KSP2] Annotation value is missing in nested annotations

Introduction
Important Notes - Current Compatibility Issues with ROOM in CMP
  • Note 1. Room version 2.7.0-alpha01 and above supports KMM.

  • Note 2. When importing ksp, you might encounter issues with version incompatibility due to different Kotlin versions
    and fail to Build
    In this case, you can check the official GitHub for supported versions: ksp releases

  • Note 3. Using Kotlin with ksp will check for ksp version and Kotlin compatibility
    When using Kotlin 2.0.0, during gradle sync you might see errors about version incompatibility
    such as Cannot change attributes of configuration ':composeApp:debugFrameworkIosX64' after it has been locked for mutation
    or [KSP2] Annotation value is missing in nested annotations

    Initially, I found a solution for the KSP2 issue
    by adding ksp.useKSP2=true in gradle.properties

    However, even after solving this issue
    and passing gradle sync
    you’ll encounter problems when configuring Room with ksp
    For example, after setting up ksp(libs.androidx.room.compiler)
    you’ll consistently get missing dao errors: [ksp] [MissingType]: xxxxx 'data.xxx.xxxDao' references a type that is not present

    After researching this issue
    Some suggest downgrading the Kotlin version to match ksp
    But since the official Wizard for CMP now defaults to Kotlin 2.0.0
    and following the principle of using newer rather than older versions XD
    If you want to successfully set up Room with Kotlin 2.0.0, you’ll need to use a workaround
    You can refer to the methods below until an official solution is available


    I’ll provide methods below
    that you can reference

    Additionally, I’ve seen that other developers have already reported issues to the official team:

Implementation
Import - With Kotlin version 1.9
  • Step 1. Import Room
    • Add the following to your .toml file:

    • Add plugin to build.gradle.kts:

    • Add library to build.gradle.kts:

    • Add the following code to the outer layer of build.gradle.kts:

    • If you’re using Kotlin version greater than 1.9.20, add the following to gradle.properties:

Import - With Kotlin version 2.0.0
  • Step 1. Modify ksp version:

  • Step 2. Adjust build.gradle.kts:
    • Add build/generated/ksp/metadata to the kotlin block
    • Use the add method to import ksp
    • Add tasks.withType to the outer layer
  • Step 3. Implement RoomDatabase using a workaround

    This is the current workaround
    If you’re using Kotlin 2.0.0, you’ll need to do this
    as we need to wait for the official team to resolve compatibility issues
Using Room in Practice
Android Main

Implement AppDataBase builder

Koin:

iOS Main

Implement AppDataBase builder

Koin:

Common Main

Implement AppDataBase

Dao

Entity

References

You might also enjoy