我正面临这个错误执行时发生故障 org.jetbrains.kotlin.gradle.internal.KaptExecution
I am facing this error A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
在我的应用程序中,我有一个模型 class,它有一些变量,我可以使用改造和房间数据库在这个应用程序中调用和显示这些数据。这意味着此应用程序首先从服务器收集数据,然后显示在房间数据库中。但是当我在这个模型 class 中使用列表时,它显示了这个错误。这是我的代码
Movie.kt
@Entity(tableName = "movie_table")
data class Movie(
@SerializedName("Id")
@PrimaryKey(autoGenerate = true)
val id: Int,
@SerializedName("Title")
@Expose
val title: String,
@SerializedName("Year")
@Expose
val Year: Int,
@SerializedName("Actors")
@Expose
val actorDetails: List<Actor>
)
Actor.kt
data class Actor(
@SerializedName("ActorName")
@Expose
val actorName: String,
@SerializedName("ActorPoster")
@Expose
val actorImage: String
)
MovieDao.kt
@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMovie(movie: Movie)
@Query("SELECT * FROM movie_table")
suspend fun getAllMovieDB(): List<Movie>
}
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
这是我的假货JSONAPI
enter link description here
这里是错误
enter image description here
我找不到任何错误,我也在使用分析来获取错误,但它什么也没显示。
我该如何解决这个问题?谢谢。
首先你必须在 room 中使用@TypeConverter,因为 room 不能插入自定义类型,如列表或对象或位图。因此,首先制作一个 class 命名转换器,然后使用注释 @TypeConverters 在数据库中添加此 class。
这是代码
Converter.kt
class Converter {
@TypeConverter
fun fromActor(actor: List<Actor>):String{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().toJson(actor,type)
}
@TypeConverter
fun toActor(actorString: String): List<Actor>{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().fromJson<List<Actor>>(actorString,type)
}
}
最后将此 converter.kt class 添加到您的数据库中
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
@TypeConverters(Converter::class)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
谢谢。
就我而言,我只是在 build.gradle(项目)中更新了 kotlin 版本
应该是这样的:
buildscript {
ext.kotlin_version = "1.4.21"
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
就我而言,jdk 版本存在问题。
- 我刚从 Oracles 网站下载 JDK。
- 打开Android工作室
- Android Studio > 首选项... > 构建、执行、部署 > 构建工具 > Gradle
- Select Gradle JDK : 1.8
- 点击确定,运行 您的应用。
经过这些步骤后,它对我有用
只需将以下行添加到“gradle.properties”
kapt.use.worker.api=false
kapt.incremental.apt=false
以上解决方案对我有用。
如果您在新项目中实现 Dagger 2 时遇到此错误。
在项目级别检查您的 kotlin 版本 build.gradle
如果是 1.6.0 将其更改为 1.4.31 或 1.4.32
在我的应用程序中,我有一个模型 class,它有一些变量,我可以使用改造和房间数据库在这个应用程序中调用和显示这些数据。这意味着此应用程序首先从服务器收集数据,然后显示在房间数据库中。但是当我在这个模型 class 中使用列表时,它显示了这个错误。这是我的代码
Movie.kt
@Entity(tableName = "movie_table")
data class Movie(
@SerializedName("Id")
@PrimaryKey(autoGenerate = true)
val id: Int,
@SerializedName("Title")
@Expose
val title: String,
@SerializedName("Year")
@Expose
val Year: Int,
@SerializedName("Actors")
@Expose
val actorDetails: List<Actor>
)
Actor.kt
data class Actor(
@SerializedName("ActorName")
@Expose
val actorName: String,
@SerializedName("ActorPoster")
@Expose
val actorImage: String
)
MovieDao.kt
@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMovie(movie: Movie)
@Query("SELECT * FROM movie_table")
suspend fun getAllMovieDB(): List<Movie>
}
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
这是我的假货JSONAPI enter link description here
这里是错误 enter image description here 我找不到任何错误,我也在使用分析来获取错误,但它什么也没显示。 我该如何解决这个问题?谢谢。
首先你必须在 room 中使用@TypeConverter,因为 room 不能插入自定义类型,如列表或对象或位图。因此,首先制作一个 class 命名转换器,然后使用注释 @TypeConverters 在数据库中添加此 class。 这是代码
Converter.kt
class Converter {
@TypeConverter
fun fromActor(actor: List<Actor>):String{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().toJson(actor,type)
}
@TypeConverter
fun toActor(actorString: String): List<Actor>{
val type = object : TypeToken<List<Actor>>() {}.type
return Gson().fromJson<List<Actor>>(actorString,type)
}
}
最后将此 converter.kt class 添加到您的数据库中
MovieDatabase.kt
@Database(
entities = [Movie::class],
version = 1
)
@TypeConverters(Converter::class)
abstract class MovieDatabase : RoomDatabase() {
abstract fun getMovieDao(): MovieDao
companion object {
@Volatile
private var instance: MovieDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance
?: synchronized(LOCK) {
instance
?: buildDatabase(context).also {
instance = it
}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(
context.applicationContext,
MovieDatabase::class.java,
"movieDatabase"
).build()
}
}
谢谢。
就我而言,我只是在 build.gradle(项目)中更新了 kotlin 版本 应该是这样的:
buildscript {
ext.kotlin_version = "1.4.21"
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
就我而言,jdk 版本存在问题。
- 我刚从 Oracles 网站下载 JDK。
- 打开Android工作室
- Android Studio > 首选项... > 构建、执行、部署 > 构建工具 > Gradle
- Select Gradle JDK : 1.8
- 点击确定,运行 您的应用。
经过这些步骤后,它对我有用
只需将以下行添加到“gradle.properties”
kapt.use.worker.api=false
kapt.incremental.apt=false
以上解决方案对我有用。
如果您在新项目中实现 Dagger 2 时遇到此错误。
在项目级别检查您的 kotlin 版本 build.gradle 如果是 1.6.0 将其更改为 1.4.31 或 1.4.32