android - 在Dao中使用@Query时,房间[exception:KaptExecution]

标签 android kotlin android-room

由于我未知的原因,将@Query添加到我的房间后,我的应用将无法构建。
我通过逐步执行所有操作来弄清楚它是@Query,最终在我添加@Query函数时出现了错误。
我得到的错误是:

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
这是我的代码:
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 =>每当我取消注释任何@Query函数时,我的应用程序都无法构建
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
}

最佳答案

找到了问题。
在我的应用程序build.gradle中,我具有以下内容:

configurations.all() {
        resolutionStrategy.force "org.antlr:antlr4-runtime:4.5.3"
        resolutionStrategy.force "org.antlr:antlr4-tool:4.5.3"
    }
过去,我将其用于DataBinding和Room之间的兼容性错误,并且不再需要它。

关于android - 在Dao中使用@Query时,房间[exception:KaptExecution],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64160945/

相关文章:

android - 如何更改android `temp`的 `sdk`目录路径?

kotlin - 协程子作业取消

android - 在第二个列表中找到丢失的对象然后使用 Kotlin 添加它的更好方法

android - 房间 - 使用@get :Query instead @Query

android - 房间数据库多对多关系从几个表中获取一个响应

android - '无法访问主线程上的数据库,因为它可能会长时间锁定 UI' 另外,我使用协程访问了房间

android - 运行时异常: using Android Design library

android - 在方向更改时旋转 ImageButton

android - 在 XML 中设置工具栏 Logo

syntax - Kotlin:访问 when 语句的参数