使用Kotlin的房间,建立数据库时得到异常

更新:尝试迈克的建议,仍然得到错误:

ext { kotlin_version = '1.1.50' arch_lib_version = '1.0.0-alpha9' gradle_version = '2.3.3' } kapt { generateStubs = true } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile "android.arch.lifecycle:runtime:$arch_lib_version" compile "android.arch.lifecycle:extensions:$arch_lib_version" compile "android.arch.persistence.room:runtime:$arch_lib_version" //annotationProcessor "android.arch.persistence.room:compiler:$arch_lib_version" kapt "android.arch.persistence.room:compiler:$arch_lib_version" annotationProcessor "android.arch.lifecycle:compiler:$arch_lib_version" compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" Error:Annotation processor '__gen.AnnotationProcessorWrapper_robotiumDebug_android_arch_persistence_room_RoomProcessor' not found 

============

在gradle中:

 ext { arch_lib_version = '1.0.0-alpha9' } compile "android.arch.persistence.room:runtime:$arch_lib_version" annotationProcessor "android.arch.persistence.room:compiler:$arch_lib_version" //kapt "android.arch.persistence.room:compiler:$arch_lib_version" 

尝试使用kapt,它会给编译错误:

 Error:Annotation processor '__gen.AnnotationProcessorWrapper_robotiumDebug_android_arch_persistence_room_RoomProcessor' not found 

代码片段如下:

  @Database(entities = arrayOf(CModel::class), version = 1) abstract class CDatabase : RoomDatabase() { abstract fun cModel(): DbCDao companion object { fun getDatabase(context: Context): CDatabase { return Room.databaseBuilder(context.applicationContext, CDatabase::class.java, “database”) .build() //<=== throw here saying CDatabase_Impl does not exist } } 

}

表扣:

 @Entity(tableName = “table”) class CModel() : ICDelegate { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = “index") private var mIndex: Long = -1 @ColumnInfo(name = "parentIndex") private var mParentIndex: Long = -1 …… } 

道界面定义:

 @Dao interface DbCDao { @Query("select * from table") fun loadAllAccountss(): List<CModel> @Query("select * from table where index = :index") fun loadCsByIndex(index: Int): CModel @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertC(ac: CModel) : Long @Update(onConflict = OnConflictStrategy.REPLACE) fun updateAC(ac: CModel) : Int @Delete fun deleteAc(ac: CModel) : Int @Query("DELETE FROM table”) fun deleteAllCs() } 

例外:

  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.RuntimeException: cannot find implementation for com.myproject.data. CDatabase. CDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:90) at 

android.arch.persistence.room.RoomDatabase $ Builder.build(RoomDatabase.java:440)

试过有人建议,不工作:

 javaCompileOptions { annotationProcessorOptions { arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] } } 

这是我可行的build.gradle文件:

 apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' ... kapt { generateStubs = true } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'commons-io:commons-io:2.5' compile "android.arch.persistence.room:runtime:1.0.0-alpha9-1" kapt "android.arch.persistence.room:compiler:1.0.0-alpha9-1" } 

考虑到:

  • 应用插件:'kapt'被删除。
  • kapt {generateStubs = true}
  • 编译而不是实现。

我用Kotlin 1.1.50。