使用带kotlin的Room,在构建数据库时遇到异常

时间:2017-09-25 22:21:11

标签: android kotlin android-room

更新:尝试过Mike的建议,仍然会收到错误:

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

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

in 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
    }    
}

}

表ps:

@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界面定义:

@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()]
        }
    }

1 个答案:

答案 0 :(得分:2)

这是我可行的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。