我正在开发一个 Kotlin 多平台项目,并尝试集成一个 Room 数据库。我正在使用 KSP 并且在编译过程中遇到以下错误:
[ksp] [MissingType]:元素“ProductEntity”引用不存在的类型
我不知道这意味着哪种类型
这是我的代码的相关部分:
数据库
import androidx.room.BuiltInTypeConverters
import androidx.room.ConstructedBy
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.RoomDatabaseConstructor
import androidx.room.TypeConverters
import com.example.shopapp.converter.CategoryObjectConverter
import com.example.shopapp.converter.ListConverter
import com.example.shopapp.data.AppDatabase.Companion.VERSION
import com.example.shopapp.data.local.dao.CartDao
import com.example.shopapp.data.local.dao.ProductsDao
import com.example.shopapp.data.local.entity.CartProductEntity
import com.example.shopapp.data.local.entity.ProductEntity
import com.example.shopapp.getAppDatabase
@TypeConverters(
CategoryObjectConverter::class ,
ListConverter::class ,
builtInTypeConverters = BuiltInTypeConverters()
)
@ConstructedBy(AppDatabaseProvider::class)
@Database(
version = VERSION,
exportSchema = false,
entities = [
ProductEntity::class,
CartProductEntity::class,
],
)
abstract class AppDatabase : RoomDatabase() {
abstract fun getCartDao(): CartDao
abstract fun getProductsDao(): ProductsDao
companion object {
const val VERSION = 1
const val NAME = "my_room"
}
}
class AppDatabaseProvider (): RoomDatabaseConstructor<AppDatabase>{
override fun initialize(): AppDatabase = getAppDatabase()
}
实体
package com.example.shopapp.data.local.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.example.shopapp.converter.CategoryObjectConverter
import com.example.shopapp.converter.ListConverter
import kotlinx.serialization.Serializable
@TypeConverters(CategoryObjectConverter::class , ListConverter::class)
@Entity(tableName = "productEntity" )
@Serializable
data class ProductEntity(
@PrimaryKey
val id: Long,
val title: String,
val price: Double,
val description: String,
val category: Category,
val images: List<String>,
)
@Entity(tableName = "cart" )
@TypeConverters(CategoryObjectConverter::class , ListConverter::class)
@Serializable
data class CartProductEntity(
@PrimaryKey
val id: Long,
val title: String,
val price: Double,
val description: String,
val category: Category,
val images: List<String>,
val quantity: Int
)
data class CartProduct(
val id: Long,
val title: String,
val price: Double,
val description: String,
val category: Category,
val images: List<String> ,
//val rating: Rating ,
val quantity: Int
)
/*data class RatingEntity(
val rate: Double,
val count: Int
)*/
@Serializable
data class Category(
val id: Int,
val name: String,
val image: String )
@Serializable
data class Product(
val id: Long,
val title: String,
val price: Double,
val description: String,
val category: Category,
val images: List<String>
)
版本:
room = "2.7.0-alpha06"
room_compiler = "2.7.0-alpha06"
sqlite = "2.5.0-SNAPSHOT"
ksp = "2.0.0-1.0.21"
梯度:
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.androidApplication)
alias(libs.plugins.buildConfig)
alias(libs.plugins.kotlinx.serialization)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.compose)
alias(libs.plugins.room)
alias(libs.plugins.ksp)
id("com.google.osdetector") version "1.7.3"
}
kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "shared"
isStatic = true
// Required when using NativeSQLiteDriver
linkerOpts.add("-lsqlite3")
}
}
/* linuxX64() {
binaries {
executable()
}
}*/
sourceSets {
androidMain.dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.activity.compose)
implementation(libs.compose.ui.tooling)
implementation(libs.ktor.client.okhttp)
implementation(libs.core)
implementation(libs.core)
implementation(libs.custom.ui)
implementation(libs.koin.android)
implementation(libs.koin.androidx.compose)
implementation(libs.androidx.glance.app.widget)
implementation(libs.androidx.glance.material3)
implementation(libs.androidx.glance.app.widget)
implementation(libs.androidx.glance.material3)
}
commonMain.dependencies {
// Compose
implementation(compose.runtime)
implementation(compose.material3)
implementation(libs.screen.size)
implementation(compose.material)
implementation(compose.animation)
//Networking
implementation(libs.ktor.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.kotlinx.serialization.json)
implementation(libs.kotlinx.datetime)
implementation(libs.ktor.client.logging)
implementation(libs.io.ktor.ktor.client.serialization)
api(libs.koin.core)
implementation(libs.koin.compose.viewmodel)
implementation(libs.room.runtime)
implementation(libs.sqlite.bundled)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.gson)
implementation(libs.kamel.image)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
}
iosMain.dependencies {
implementation(libs.ktor.client.darwin)
}
commonTest.dependencies {
implementation(libs.kotlin.test)
}
}
}
android {
namespace = "com.example.shopapp"
compileSdk = 34
defaultConfig {
minSdk = 24
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}
}
task("testClasses") {}
room {
schemaDirectory("$projectDir/schemas")
}
dependencies {
/* add("kspCommonMainMetadata", project(":test-processor"))
add("kspJvm", project(":test-processor"))
add("kspJvmTest", project(":test-processor")) // Not doing anything because there's no test source set for JVM
// There is no processing for the Linux x64 main source set, because kspLinuxX64 isn't specified
add("kspLinuxX64Test", project(":test-processor"))*/
ksp(libs.room.compiler)
}
ksp beta 版本目前不支持转换器,因此请尝试删除
@TypeConverters(CategoryObjectConverter::class , ListConverter::class)
如果您仍然遇到相同的错误并且您在多平台上使用空间,那么
kotlin = "2.0.20-RC"
room = "2.7.0-alpha01"
ksp = "2.0.20-RC-1.0.24"
并将
ksp.useKSP2=true
添加到您的 gradle.properties
(如果您正在使用 kotlin 2.x.x
)
这将解决您的问题,如果您仍然遇到相同的错误,请尝试删除 @TypeConverters(<converter class name>::class)