【问题标题】:I am facing this error A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution我正面临这个错误执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障
【发布时间】:2021-05-17 14:58:59
【问题描述】:

在我的应用程序中,我有一个模型类,它有一些变量,我可以使用改造和房间数据库在这个应用程序中调用和显示这些数据。这意味着这个应用程序首先从服务器收集数据,然后显示在房间数据库中。但是当我在这个模型类中使用列表时,它会显示这个错误。这是我的代码

电影.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>
)

演员.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>
}

电影数据库.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()

}
}

这是我的假 JSON API enter link description here

这是错误 enter image description here 我找不到任何错误,我也在使用分析来获取错误,但它什么也没显示。 我该如何解决这个问题?谢谢。

【问题讨论】:

  • Room 无法插入 List 等自定义类型,您需要为字段 Actor 提供自定义 Serializer 并将其标记为已忽略
  • 我的建议是使用外键为演员创建一个新表
  • 能否举例说明
  • 谢谢@JoãoPauloSena。我使用类型转换器解决了这个错误。

标签: json android-studio retrofit2 android-room


【解决方案1】:

首先你必须从房间使用@TypeConverter,因为房间不能插入自定义类型,如列表、对象或位图。所以首先创建一个名为converter的类,然后使用注解@TypeConverters将该类添加到数据库中。 这是代码

转换器.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 类

电影数据库.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()

    }
}

谢谢。

【讨论】:

  • 我不知道我是怎么错过的,但这是我的问题的确切原因。感谢 user10997009。
【解决方案2】:

就我而言,我只是在 build.gradle(项目)中更新了 kotlin 版本 应该是这样的:

buildscript {
       ext.kotlin_version = "1.4.21"
dependencies { 
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

}

【讨论】:

    【解决方案3】:

    就我而言,jdk 版本存在问题。

    1. 我从 Oracles 网站新下载了 JDK。
    2. 打开 Android Studio
    3. Android Studio > 首选项... > 构建、执行、部署 > 构建工具 > Gradle
    4. 选择 Gradle JDK:1.8
    5. 单击确定并运行您的应用程序。

    在这些步骤之后它对我有用

    【讨论】:

      【解决方案4】:

      只需将以下行添加到“gradle.properties”

      kapt.use.worker.api=false
      kapt.incremental.apt=false
      

      上述解决方案对我有用。

      【讨论】:

        【解决方案5】:

        如果您在新项目中实施 dagger 2 时遇到此错误。

        在项目级 build.gradle 中检查您的 kotlin 版本 如果是 1.6.0,则将其更改为 1.4.31 或 1.4.32

        【讨论】:

          猜你喜欢
          • 2020-09-19
          • 1970-01-01
          • 2020-12-18
          • 2020-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-17
          • 2022-09-20
          相关资源
          最近更新 更多