vt-dev0
vt-dev0

Reputation: 773

Room [exception:KaptExecution] when using @Query in Dao

For unknown to me reasons, my app is not able to build once I add @Query to my Room.

I figured out that it was @Query by doing everything step-by-step and eventually got to a point when I added @Query function which resulted in error.

The error I get is:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

Here is my code:

MainDatabase.kt

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import my.test.movieexpert.localdatasource.dao.PopularMovieDao
import my.test.movieexpert.profilescreen.model.PopularMovie

@Database(entities = [PopularMovie::class], version = 1, exportSchema = false)
abstract class MainDatabase : RoomDatabase() {

    abstract fun popularMovieDao(): PopularMovieDao

    companion object {
        @Volatile
        private var Instance: MainDatabase? = null

        fun getInstance(context: Context): MainDatabase {
            val tempInstance = Instance
            if (tempInstance != null) {
                return tempInstance
            }

            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    MainDatabase::class.java,
                    "main_database"
                ).build()
                Instance = instance
                return instance
            }
        }
    }
}

PopularMovieDao.kt => Whenever I uncomment any of @Query functions, my app is not building

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface PopularMovieDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun addMovies(listOfMovies: List<PopularMovie>)

//    @Query("DELETE FROM PopularMovie")
//    suspend fun clearTable()

//    @Query("SELECT * FROM PopularMovie")
//    suspend fun getMovies(): List<PopularMovie>
}

PopularMovie.kt

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.squareup.moshi.Json

@Entity
data class PopularMovie(
    @field:Json(name = "id") val id: Int,
    @field:Json(name = "title") val title: String,
    @field:Json(name = "overview") val overview: String,
    @field:Json(name = "release_date") val release_date: String,
    @field:Json(name = "poster_path") val poster_path: String,
    @field:Json(name = "vote_average") val vote_average: String
) {
    @PrimaryKey(autoGenerate = true)
    var rowId: Int = 0
}

Upvotes: 1

Views: 317

Answers (1)

vt-dev0
vt-dev0

Reputation: 773

Found the problem.

In my app build.gradle I had the following:

configurations.all() {
        resolutionStrategy.force "org.antlr:antlr4-runtime:4.5.3"
        resolutionStrategy.force "org.antlr:antlr4-tool:4.5.3"
    }

I used this for compatibility errors between DataBinding and Room in the past and it was no longer needed.

Upvotes: 1

Related Questions