Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I try to implement a Room Database with Androidx, I am getting error

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)

I have add every thing in my gradle file. My gradle file is shown below

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"

}

Invoking room

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)
like image 619
Sabinmon ks Avatar asked Aug 14 '18 06:08

Sabinmon ks


1 Answers

When using annotation processing in a Kotlin project (which Room does), you should be using kapt, the Kotlin annotation processor plugin.

First, you should add this to the top of your build.gradle file to the rest of the plugins, to enable kapt:

apply plugin: 'kotlin-kapt'

And then you should use kapt instead of annotationProcessor for your dependencies, as the comment you have on the line says:

kapt "androidx.room:room-compiler:$room_version" // use kapt for Kotlin

Note that kapt will also process Java files, so you don't need to add everything twice, it's enough to have any annotation processors you have added with kapt.

like image 167
zsmb13 Avatar answered Nov 20 '22 18:11

zsmb13