Android Studio 构建失败 "Execution failed for task ':app:kaptDebugKotlin'."

Android Studio Build Fails "Execution failed for task ':app:kaptDebugKotlin'."

我遇到了这个错误,我想我知道它是从哪里来的,但我不明白这个问题。我收到的错误的详细摘要。

> Task :app:kaptDebugKotlin
C:\Users\matan\AndroidStudioProjects\RoomTut\app\build\tmp\kapt3\stubs\debug\com\example\roomtut\data\MarkerDao.java:13: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    kotlin.coroutines.Continuation<? super kotlin.Unit> p1);
                                                        ^C:\Users\matan\AndroidStudioProjects\RoomTut\app\build\tmp\kapt3\stubs\debug\com\example\roomtut\data\MarkerDao.java:11: error: Not sure how to handle insert method's return type.
    public abstract java.lang.Object addMarker(@org.jetbrains.annotations.NotNull()
                                     ^[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

> Task :app:kaptDebugKotlin FAILED

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.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > 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 32s
29 actionable tasks: 9 executed, 20 up-to-date

我知道我的标记 dao 中有问题,但对我来说,注释看起来不错,您可以在下面看到,还有 link 到我的整个项目的回购 - https://github.com/M-J-Y-21/room-test

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

@Dao
interface MarkerDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun addMarker(marker: Marker)

    @Query("SELECT * FROM marker_table ORDER BY id ASC")
    fun readAllData(): LiveData<List<Marker>>
}

您可以在下面看到我如何注释我的标记 class

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "marker_table") data class Marker(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val title: String,
    val location: String,
    val colour: String
)

下面是我的数据库class

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = arrayOf(Marker::class), version = 1, exportSchema = false)
abstract class MarkerDatabase: RoomDatabase() {

    abstract fun markerDao(): MarkerDao

    companion object {
        @Volatile
        private var INSTANCE: MarkerDatabase? = null

        fun getDatabase(context: Context): MarkerDatabase{
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    MarkerDatabase::class.java,
                    "marker_database"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}

据我所知,您的项目使用了不同版本的 androidx.room 依赖项,例如:

"androidx.room:room-runtime:2.3.0"
"androidx.room:room-compiler:2.2.5"

您应该对 androidx.room 的所有工件使用相似的版本。 Please look at sample with more right approach:

dependencies {
...
    def room_version = "2.3.0"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    kapt "androidx.room:room-compiler:$room_version" 

...
}

当我将 androidx.room 的所有版本更改为 2.3.0 时,一切都开始正常工作。并且不要忘记更改 return 类型 addMarker:

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker) // OK

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker): Long // OK

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker): Int // ERROR

我是怎么找问题的:

  1. @Insert 方法上检查 return 类型(见上文)。
  2. 检查参数的类型和插入方法的注释(@Entity)。
  3. 检查导入:IDE可以自动添加导入class或具有相似名称但来自其他包的注释。
  4. Check setup of database.
  5. Check project dependencies(or other doc's sample).

P.S。 MarkerDatabase.Companion.getDatabase() 对单例的实现是错误的:( 请使用双重检查锁定算法,或者使所有方法同步,或者尝试根据您的情况调整 kotlin 的 lazy