java.lang.RuntimeException:无法找到实现 com.qbitstream.salesmanagementsystem.data.AppDatabase。 AppDatabase_Impl不存在 在androidx.room.Room.getGeneratedImplementation(Room.java:94)
我已经在gradle文件中添加了所有内容。我的gradle文件如下所示
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.qbitstream.salesmanagementsystem"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def nav_version = "1.0.0-alpha04"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$nav_version" // use -ktx for Kotlin
//room
def room_version = "2.0.0-rc01"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // use kapt for Kotlin
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
// retrofit
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
// rxandroid
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
}
调用房间
ioThread {
AppDatabase.getInstance(activity!!).customerDao().insertAll(it.customer.data)
}
AppDatabase.kt
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.qbitstream.salesmanagementsystem.model.Customer
@Database(entities = [(Customer::class)], version = 1)
abstract class AppDatabase: RoomDatabase() {
abstract fun customerDao(): CustomerDao
companion object {
@Volatile private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
AppDatabase::class.java, "sms.db")
.build()
}
}
CustomerDao.kt
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.qbitstream.salesmanagementsystem.model.Customer
@Dao
interface CustomerDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertAll(customer:List<Customer>)
@Query("SELECT * FROM customer where lower(customer_name) like ':name%'")
fun customers(name:String):List<Customer>
}
DataClasses.kt
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName
data class Customers(val customer: Data,val response_code:Int,val status:String)
data class Data(val data:ArrayList<Customer>)
@Entity(tableName = "customer")
data class Customer(
@PrimaryKey
@ColumnInfo(name = "id")
@SerializedName("id")
val id:String,
@ColumnInfo(name = "customer_name")
@SerializedName("customer_name")
val name:String)
data class Manufacturer(val id:Int,val name:String)
data class Product(val id:Int,val manufacturerId: Int,val name:String)
答案 0 :(得分:7)
在Kotlin项目中使用注释处理(Room可以做到),您应该使用kapt,即Kotlin注释处理器插件。
首先,您应该将此文件添加到build.gradle
文件顶部的其余插件中,以启用kapt:
apply plugin: 'kotlin-kapt'
然后您应该使用kapt
而不是annotationProcessor
进行依赖,因为在线上的注释显示:
kapt "androidx.room:room-compiler:$room_version" // use kapt for Kotlin
请注意,kapt还将处理Java文件,因此您无需将所有内容都添加两次,足够使用kapt
添加的任何注释处理器就足够了。