Anique Sabir
Anique Sabir

Reputation: 81

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

Upvotes: 7

Views: 3826

Answers (5)

LiuWenbin_NO.
LiuWenbin_NO.

Reputation: 1326

For me, downgrade the plugins version to

    classpath 'com.android.tools.build:gradle:4.0.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"

works, but didn't figure out the cause.

Upvotes: 0

Anique Sabir
Anique Sabir

Reputation: 81

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

Upvotes: 1

rivelbab
rivelbab

Reputation: 141

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.

Upvotes: 7

IR42
IR42

Reputation: 9682

For queries that return multiple values, you can use List or Array. Query

@Query("select * from result")
fun readMovieList(): MutableLiveData<List<Result>>

also, add field target to TypeConverters annonation in entity

@field:TypeConverters(MyTypeConverter::class)

How to apply Room TypeConverter to a single field of an entity?

Upvotes: 0

Atiq
Atiq

Reputation: 14835

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

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

Upvotes: 2

Related Questions