执行org.jetbrains.kotlin.gradle.internal.KaptExecution Room数据库时发生故障

时间:2020-07-31 16:24:12

标签: kotlin kapt

我已经为这个错误苦苦挣扎了好几个小时,并且能够追溯到第一次发生该错误时。将@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!!
        }
    }
}

Game.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"

}

2 个答案:

答案 0 :(得分:0)

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

您的数据库类似乎还不错...将Room与Kotlin协程一起使用,对吗?所有@Query批注都在编译时进行了验证,因此可能是个问题。我建议在您的Dao文件中的suspend中添加fun getAll()关键字,以便Room可以保证它将在后台线程。

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

答案 1 :(得分:0)

对我也不起作用,我发现如果你命名你的 typeconverter 类

@TypeConverters(TypeConverters :: class)

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

@TypeConverters(Converters :: class)