Home > Mobile >  AppDatabase_Impl does not exist
AppDatabase_Impl does not exist

Time:10-27

I'm new learning Kotlin and I'm developing a Notes app to practise with databases and viewModels. I'm having trouble generating my database and the app crashes when it should generate the database and shows me this error on the logcat: "java.lang.RuntimeException: cannot find implementation for com.example.blocnotas.database.AppDatabase. AppDatabase_Impl does not exist"

This is how I made my Database class:

package com.example.blocnotas.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Notes::class], version = 1, exportSchema = false)
abstract class AppDatabase: RoomDatabase() {

    abstract fun noteDao(): NoteDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_notes_database")
                    .build()
                INSTANCE = instance

                return instance
            }
        }
    }

My Application class:

package com.example.blocnotas

import android.app.Application
import com.example.blocnotas.database.AppDatabase


class NotesApplication : Application() {
    val database: AppDatabase by lazy { AppDatabase.getDatabase(this) }

}

My app gradle:

    plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    //id 'androidx.navigation.safeargs.kotlin'
}

android {
    namespace 'com.example.blocnotas'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.blocnotas"
        minSdk 19
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        vectorDrawables.useSupportLibrary = true
    }

    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'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'

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

// optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"


}

Does anybody know how can i fix it and generate my database table? I leave you the gitHub repository where i'm making it so if it can help get any extra necessary information: https://github.com/R3inbow/BlocNotasApp.git

CodePudding user response:

I finally solved it changing in the gradle the "kapt" for "ksp" this way:

In dependencies:

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

In plugins:

id 'com.google.devtools.ksp' version '1.7.20-1.0.6' 
  • Related