Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution with ROOM and @Database annotation

I am using Room for offline storage. My model contain List which Room does not support and i write typeconverters but now i am getting this error. when i remove @Database annotation then error goes but with @Database annotation it is displaying the error. here is my all relevant classes.

here is my Dao

package com.example.mvvm.room

import androidx.lifecycle.MutableLiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.example.mvvm.models.Result

@Dao
interface MovieDao {
    @Query("Select * from Result")
    fun readMovieList():MutableLiveData<ArrayList<Result>>

    @Insert
    fun insertData(result: Result)

    @Delete
    fun deleteAll()
}

here is my Database class

package com.example.mvvm.room
import android.content.Context
import androidx.room.*
import com.example.mvvm.MyTypeConverter
import com.example.mvvm.models.Result

@Database(entities = arrayOf(Result::class),version = 1)
@TypeConverters(MyTypeConverter::class)
abstract class MovieDatabase:RoomDatabase(){
   companion object{

      private var INSTANCE:MovieDatabase?=null
      fun getInstance(context: Context):MovieDatabase? {
         if (INSTANCE == null) {
            synchronized(MovieDatabase::class) {
               INSTANCE = Room.databaseBuilder(context, MovieDatabase::class.java, "movie.db").
               fallbackToDestructiveMigration()
                  .allowMainThreadQueries()
                  .build()
            }
         }
         return INSTANCE
      }
   }
   abstract fun movieDao():MovieDao
}

here is my model

package com.example.mvvm.models

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.TypeConverters
import com.example.mvvm.MyTypeConverter

@Entity(tableName = "result")
data class Result(
    @ColumnInfo val adult: Boolean,
    @ColumnInfo val backdrop_path: String,
    @TypeConverters(MyTypeConverter::class)
    @ColumnInfo val genre_ids: List<Int>,
    @ColumnInfo val id: Int,
    @ColumnInfo val original_language: String,
    @ColumnInfo val original_title: String,
    @ColumnInfo val overview: String,
    @ColumnInfo val popularity: Double,
    @ColumnInfo val poster_path: String,
    @ColumnInfo val release_date: String,
    @ColumnInfo val title: String,
    @ColumnInfo val video: Boolean,
    @ColumnInfo val vote_average: Double,
    @ColumnInfo val vote_count: Int
)

type converter class

package com.example.mvvm

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MyTypeConverter {

    companion object{
        private val gson = Gson()
        @JvmStatic
        @TypeConverter
        fun toJson(mInt : List<Int>):String{
            return gson.toJson(mInt)
        }

        @JvmStatic
        @TypeConverter
        fun fromJson(string : String):String{
            val type = object : TypeToken<List<Int>>(){}.type
            return gson.fromJson(string, type)
        }
    }
}

here is the error

A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
like image 568
Anique Sabir Avatar asked Jul 02 '20 07:07

Anique Sabir


3 Answers

You can compile in debug mode to get more detail about your error and find where is the problem.

Go to Gradle menu / Other / compileDebugKotlin (right sidebar on android studio) you should have more detail about the error and then you can post it here to have more help.

like image 114
rivelbab Avatar answered Nov 10 '22 19:11

rivelbab


I think the issue is in your MovieDao at @Query("Select * from Result")

It should be @Query("Select * from result")

like image 2
Max Avatar answered Nov 10 '22 17:11

Max


I solved the issue by changing MutableLiveData to LiveData in MovieDao readMovieList and changing delete annotaion to Query(Delete from result)

like image 1
Anique Sabir Avatar answered Nov 10 '22 19:11

Anique Sabir