Search code examples
jsonandroid-studioretrofit2android-room

I am facing this error A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution


In my app I have a model class, it has some variables, I can call and show this data in this app using retrofit and room DB. which means this app first collects data from the server then it shows in room DB. But when I am using the list in this model class it shows this error. Here is my code

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()

}
}

here is my fake JSON API enter link description here

here is the error enter image description here I can't find any error, I am also using analyze for get the error but it's show nothing. How can I solve this? Thank you.


Solution

  • at first you have to use @TypeConverter from room because room can not insert the custom types like List or object or bitmap. So firstly make a class named converter then add this class in database using annotation @TypeConverters. here is the code

    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)
        }
    }
    

    and finally add this converter.kt class in your database

    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()
    
        }
    }
    

    Thank you.