using Room with kotlin, get exception when build t

2019-08-06 03:28发布

问题:

UPDATE: tried Mike's suggestion, still get error:

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"

tried using the kapt, it will give compile error:

Error:Annotation processor '__gen.AnnotationProcessorWrapper_robotiumDebug_android_arch_persistence_room_RoomProcessor' not found

the code snippet is below:

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

}

the table clas:

@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

……
}

the Dao interface defination:

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


}

the exception:

   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)

tried someone suggested and does not work:

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

回答1:

This is my workable build.gradle file:

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

Take into account:

  • apply plugin: 'kapt' is removed.
  • kapt { generateStubs = true }
  • compile instead on implementation.

I used Kotlin 1.1.50.