将 Room 的@Database添加到数据库抽象类后,任务':app:kaptGenerateStubsDebugKotlin'失败



我正在尝试将Room实现到用Kotlin编写的Android应用程序中。在构建失败了这么多次之后,当我将@Database添加到我的数据库类中时,我指出了它失败的问题。

package sample.service.local
import android.arch.persistence.room.Database
import android.arch.persistence.room.RoomDatabase
import sample.service.model.Announcement
@Database(entities = [Announcement::class], version = 1)
abstract class AnnounceDatabase: RoomDatabase() {
    abstract fun announceDAO(): AnnounceDAO
}

如果我用@Database注释掉了这条线,它就成功构建了。它的 DAO 和实体文件应该不是问题,因为我尝试在没有@Database的情况下使用它们构建并且成功了。我也没有将它们添加到任何其他类中;我刚刚创建了 3 个新文件,它们是这个数据库、它的 dao 和它的实体。

这是build.gradle(Module:app(

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "sample"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        dataBinding.enabled true
    }
}
project.ext {
    supportLibraryVersion = "26.1.0"
    daggerVersion = "2.13"
    butterKnifeVersion = "8.8.1"
    rxJavaVersion = "2.1.0"
    rxAndroidVersion = "2.0.1"
    lifecycleVersion = "1.0.0"
    roomVersion = "1.0.0"
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.google.android.gms:play-services-maps:11.8.0'
    implementation 'com.android.support:support-v4:27.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// Android Support Library
implementation 'com.android.support:design:27.1.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:cardview-v7:27.1.0'
implementation 'com.android.support:recyclerview-v7:27.1.0'
implementation 'com.android.support:support-annotations:27.1.0'
implementation 'com.android.support:support-compat:27.1.0'
implementation 'com.android.support:support-core-ui:27.1.0'
// Easy Permission
implementation 'pub.devrel:easypermissions:1.2.0'
// Lifecycle
implementation "android.arch.lifecycle:extensions:1.1.0"
annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
// Kotlin binding
//    kapt 'com.android.databinding:compiler:3.1.0'
// Dagger core
kapt "com.google.dagger:dagger-compiler:$project.daggerVersion"
implementation "com.google.dagger:dagger:$project.daggerVersion"
// Dagger Android
kapt "com.google.dagger:dagger-android-processor:$project.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$project.daggerVersion"
// Timber
implementation 'com.jakewharton.timber:timber:4.6.0'
// Simple Item Decoration
implementation 'com.bignerdranch.android:simple-item-decoration:1.0.0'
// Retrofit
implementation "com.squareup.retrofit2:retrofit:2.1.0"
implementation "com.squareup.retrofit2:converter-gson:2.1.0"
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
//    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
// RxJava & RxAndroid
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
// PageIndicatorView
implementation 'com.romandanylyk:pageindicatorview:1.0.0@aar'
// Room
implementation "android.arch.persistence.room:rxjava2:$project.roomVersion"
implementation "android.arch.persistence.room:runtime:$project.roomVersion"
kapt "android.arch.persistence.room:compiler:$project.roomVersion"
testImplementation "android.arch.persistence.room:testing:$project.roomVersion"
}

这是项目的成绩

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.31'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}
allprojects {
repositories {
    google()
    jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

我跑了gradlew clean build --stacktrace --debug,这是错误消息

任何帮助将不胜感激。

问题出在我的实体类中,我使用 val 而不是 var 作为参数,所以我只是将它们全部更改为 var 问题就解决了。

编辑:我的DAO类也有一个问题,不能使用ArrayList而不是List

最新更新