【问题标题】:A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution Room Database执行 org.jetbrains.kotlin.gradle.internal.KaptExecution Room 数据库时发生故障
【发布时间】:2020-11-21 12:34:20
【问题描述】:

我已经为这个错误苦苦挣扎了好几个小时,并且能够追溯到它第一次出现的时间。一旦我将@Database 注释添加到我的数据库类中,我就会收到一条错误消息,提示“执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障”有人可以就此提出建议吗?我不得不回滚几个小时的工作才能达到这一点,这让我的大脑崩溃了。

GameDatabase.kt

package com.arcsoft.psncollection.data

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

@Database(entities = [Game::class], version = 1, exportSchema = true)
abstract class GameDatabase: RoomDatabase() {
    abstract fun monsterDao(): GameDao

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

        fun getDatabase(context: Context): GameDatabase {
            if (INSTANCE == null) {
                synchronized(this) {
                    INSTANCE = Room.databaseBuilder(
                        context.applicationContext,
                        GameDatabase::class.java,
                        "games.db"
                    ).build()
                }
            }
            return INSTANCE!!
        }
    }
}

游戏.kt

package com.arcsoft.psncollection.data

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.arcsoft.psncollection.IMAGE_BASE_URL

@Entity(tableName = "games")
data class Game (
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val cover: Cover,
    val name: String,
    val popularity: Double,
    val imageFile: String,
    var summary: String,
    var time_to_beat: String,
    var aggregated_rating: Double
)
{
    val imageUrl
        get() = "$IMAGE_BASE_URL/$imageFile.webp"
    val thumbnailUrl
        get() = "$IMAGE_BASE_URL/${imageFile}_tn.webp"
}

GameDao.kt

package com.arcsoft.psncollection.data

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

@Dao
interface GameDao {
    @Query("SELECT * from games")
    fun getAll(): List<Game>

    @Insert
    suspend fun insertMonster(monster: Game)

    @Insert
    suspend fun insertMonsters(monsters: List<Game>)

    @Query("DELETE from games")
    suspend fun deleteAll()

}

build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"
    defaultConfig {
        applicationId "com.arcsoft.psncollection"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding{
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.3.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
    implementation 'com.github.husnjak:IGDB-API-JVM:0.7'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation"com.squareup.moshi:moshi-kotlin:1.8.0"

    def retrofit2_version = "2.6.0"
    implementation "com.squareup.retrofit2:retrofit:$retrofit2_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit2_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit2_version"

    def coroutines_version = "1.2.1"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

    def glide_version = "4.9.0"
    implementation "com.github.bumptech.glide:glide:$glide_version"
    annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"

    def room_version = "2.1.0"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

}

【问题讨论】:

  • 好问题。我不知道答案,但是如果您可以制作一个更小的项目来展示您所指出的行为,那么有人可能会更快地找到答案。您甚至可能自己偶然发现答案。所以我建议开始一个新的空项目并慢慢添加东西,直到你遇到同样的失败。

标签: kotlin kapt


【解决方案1】:

您是否尝试过在调试模式下编译以获取更多信息?您可以通过点击 Android Studio 右侧栏中的 Gradle,然后点击 Tasks / other / compileDebugKotlin 来完成。清理和重建项目大部分时间都对我有帮助。

您的数据库类似乎没问题...将 Room 与 Kotlin 协程一起使用,对吗?所有@Query 注解都在编译时验证,所以这可能是问题所在,我建议在你的Dao 文件中的fun getAll() 中添加suspend 关键字,这样Room 可以保证它会在后台线程上运行。。

另外,我最近使用了带有协程的 Room,如果您想查看我的代码(在 Github 上),这里是 link,这不是最好的示例,但它可以以某种方式帮助您。

【讨论】:

    【解决方案2】:

    对我也不起作用我发现如果你命名你的类型转换器类

    @TypeConverters(TypeConverters :: class)
    

    它实际上可能与 androidx.room 中的 TypeConverters 接口发生冲突。 我重构并将我的 interfece 重命名为 Converters 并成功编译

    @TypeConverters(Converters :: class)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2020-12-18
      • 2022-09-20
      • 2021-05-17
      相关资源
      最近更新 更多