gpt4 book ai didi

android - 我尝试使用 Androidx 实现房间数据库,但出现错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:50 25 4
gpt4 key购买 nike

java.lang.RuntimeException: cannot find implementation for com.qbitstream.salesmanagementsystem.data.AppDatabase. AppDatabase_Impl does not exist at 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)

最佳答案

在 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 添加的任何注释处理器就足够了。

关于android - 我尝试使用 Androidx 实现房间数据库,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51835236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com