2

I am facing some issue using Room DB

java.lang.RuntimeException: cannot find implementation for com.zeliot.roomtuto.db.NoteDB. NoteDB_Impl does not exist

below is my app grale

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.zeliot.roomtuto"
        minSdk 23
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

}

dependencies {

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //Room
    def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"



    // Coroutine Lifecycle Scopes
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"

    //coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
}

@Entity(
    tableName = "note"
)
data class Note(
    @PrimaryKey(autoGenerate = true)
    var id:Int?=null,
    val title :String,
    val desciption:String,
    val datetime:String,
)



@Dao
interface NoteDao {

    @Insert
    fun insert(note: Note)

    @Delete
    fun delete(note: Note)

    @Query("select * from note")
    fun getNotes(): LiveData<List<Note>>

    @Update
    fun update(note: Note)
}


@Database(
    entities = [Note::class],
    version = 1
)
abstract class NoteDB :RoomDatabase() {

    abstract fun getNoteDao():NoteDao

    companion object{
        @Volatile
        private var instance : NoteDB? = null
       
        fun createDB(context: Context) :NoteDB{
             return instance ?:
             Room.databaseBuilder(
                 context.applicationContext,
                 NoteDB::class.java,
                 "Note_db.db"
             )
                 .build()
         }

    }
}

UNABLE TO GET THE ANSWER ,I HAVE TRIED MANY SOLUTIONS BUT DIDN'T WORK. I have tried many solution which are already available in stackoverflow but those solutions didnt work.

John Lobo
  • 14,355
  • 2
  • 10
  • 20

2 Answers2

2

In Apple M1 chip we need to use room version of 2.4.0-alpha04.

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id'kotlin-kapt'
}

    implementation "androidx.room:room-runtime:2.4.0-alpha04"
    implementation "androidx.room:room-ktx:2.4.0-alpha04"
    kapt "androidx.room:room-compiler:2.4.0-alpha04"

issue tracker link https://issuetracker.google.com/issues/174695268?pli=1#comment13

1

Change :-

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

to

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

and then change

annotationProcessor "androidx.room:room-compiler:$room_version"

to

kapt "androidx.room:room-compiler:$room_version"
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • after adding above dependency i got this error -> Execution failed for task ':app:kaptDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message) – Vishwanath Kumbar Sep 24 '21 at 11:17
  • also i have analyze the code and resolved the warnings still not working – Vishwanath Kumbar Sep 24 '21 at 11:29