【Compose Multiplatform】Using ROOM Database in CMP - [KSP2] Annotation value is missing in nested annotations
目錄
- 【Compose Multiplatform】專案轉移探討與開發指南
- 【Compose Multiplatform】手機使用依賴注入Koin
- 【Compose Multiplatform 】跨平台App但Android需要context作法並搭配Koin
- 【Compose Multiplatform】手機本地持久化儲存DataStore實作
- 【Compose Multiplatform】手機資料庫SqlDelight實作
- 【Compose Multiplatform】CMP中使用ROOM開發資料庫 - [KSP2] Annotation value is missing in nested annotations
- 【Compose Multiplatform】CMP專案中導入CocoaPods及無CocoaPods情況下使用IOS Swift/Obj-C
-
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
andfail 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, duringgradle sync
you might see errors about version incompatibility
such asCannot 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 theKSP2
issue
by addingksp.useKSP2=true
in gradle.properties
However, even after solving this issue
and passinggradle sync
you’ll encounter problems when configuring Room with ksp
For example, after setting upksp(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:
- 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:
-
-
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
- Add
- 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
Implement AppDataBase builder
Koin:
Implement AppDataBase builder
Koin:
Implement AppDataBase
Dao
Entity