Home » Android » java – Android room persistent: AppDatabase_Impl does not exist

java – Android room persistent: AppDatabase_Impl does not exist

Posted by: admin March 11, 2020 Leave a comment

Questions:

My app database class

@Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract FavoritesDao favoritesDao();

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();

                    //Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

Gradle lib:

 compile "android.arch.persistence.room:runtime:+"   
 annotationProcessor "android.arch.persistence.room:compiler:+"

And when i ask for instance it will give this error, AppDatabase_Impl does not exist
in my application class

public class APp extends Application {

    private boolean appRunning = false;

    @Override
    public void onCreate() {
        super.onCreate();
        AppDatabase.getAppDatabase(this); //--AppDatabase_Impl does not exist

    }   

}
How to&Answers:

For those working with Kotlin, try changing annotationProcessor to kapt in the apps build.gradle

for example:

// Extensions = ViewModel + LiveData
implementation "android.arch.lifecycle:extensions:1.1.0"
kapt "android.arch.lifecycle:compiler:1.1.0"
// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"

also remember to add this plugin

apply plugin: 'kotlin-kapt'

to the top of the app level build.gradle file and do a clean and rebuild (according to https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)

In Android Studio, if you get errors when you paste code or during the build process, select Build >Clean Project. Then select Build > Rebuild Project, and then build again.


UPDATE

If you have migrated to androidx

def room_version = "2.2.3" // check latest version from docs

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

Answer:

Just use

apply plugin: 'kotlin-kapt'

in app build.gradle

And keep both in dependencies

annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"

EDIT

In newer version don’t need to add both dependencies at a time
Just use, hope it will work.

kapt 'android.arch.persistence.room:compiler:1.1.1'

Answer:

I had this error when I missed

@Database(entity="{<model.class>})

Ensure that the entity model specified in the annotation above refers to the particular model class and also ensure that the necessary annotation:

@Entity(tableName = "<table_name>" ...)

is properly defined and you’d be good

Answer:

if you are using kotlin classes to implement database then
use

apply plugin: 'kotlin-kapt'

and

kapt "android.arch.persistence.room:compiler:1.1.1"

in your gradle file, it will work.

Answer:

It is not just about updating your dependencies. Make sure all your Room dependencies have the same version.

implementation 'android.arch.persistence.room:rxjava2:1.1.0-alpha2'
implementation 'android.arch.persistence.room:runtime:1.1.0-alpha2'
annotationProcessor "android.arch.persistence.room:compiler:1.1.0-alpha2"

In the sample snippet above, all my Room dependencies have the same version 1.1.0-alpha2

Answer:

For Kotlin Developers

Use this:

implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"

And add apply plugin: 'kotlin-kapt' to the top of the app level build.gradle.

For Java Developers

implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

Answer:

I met this problem because I have forgotten the apt dependences

implementation "android.arch.lifecycle:extensions:$archLifecycleVersion"
implementation "android.arch.persistence.room:runtime:$archRoomVersion"
annotationProcessor "android.arch.lifecycle:compiler:$archLifecycleVersion"
annotationProcessor "android.arch.persistence.room:compiler:$archRoomVersion"

after added the annotationProcessor, and rebuild it, the problem solved.

Answer:

I meet with the problem, because I forget @Dao annotation

@Dao
public interface SearchHistoryDao {
    @Query("SELECT * FROM search_history")
    List<SearchHistory> getAll();

    @Insert
    void insertAll(SearchHistory... histories);

    @Delete()
    void delete(SearchHistory history);
}

Room Official tutorial

Answer:

make sure to add correct dependency for room in build.gradle

ext {
   roomVersion = '2.1.0-alpha06'
}

// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

And below line at the top-

apply plugin: 'kotlin-kapt'

Answer:

The question is pretty old, but I’ve stumbled on this today and none of the provided answers helped me. Finally I managed to resolve it by noticing that google documentation actually is still adopted to Java and not Kotlin by default, actually they have added a comment which I’ve ignored

For Kotlin use kapt instead of annotationProcessor

So, instead of

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

If you are developing with Kotlin, you should use:

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

Answer:

Had the same problem. Implemented the few classes and interface as officially told in a new example project created by Android Studio:
https://developer.android.com/training/data-storage/room/

All mentioned solutions above did not help, the necessary _Impl files according to my database class were not generated by Room. Finally executing gradle clean build in terminal gave me the hint that lead to the solution:

“warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false.”

I added the parameter exportSchema = false in the database class

@Database(entities = arrayOf(User::class), version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}

And then it worked, found these two generated files in the app module under generatedJava:

  • AppDatabase_Impl
  • UserDao_Impl

I don’t understand this behaviour as the parameter is said to be optional, see
https://stackoverflow.com/a/44645943/3258117

Answer:

Use the following gradle link:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'

You need to create diffretn singloton class and get the Appdatabase from ther like this:

RoomDB.java

public class RoomDB {

private static RoomDB INSTANCE;

public static AppDatabase getInstance(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();

                //Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
    }
    return INSTANCE;
}

public static void destroyInstance() {
    INSTANCE = null;
}

App.java

public class App extends Application {

private boolean appRunning = false;

@Override
public void onCreate() {
    super.onCreate();
    RoomDB.getInstance(this); //This will provide AppDatabase Instance
}

Answer:

In my case, I was testing the connectivity for room database and I have put the testing class inside the directory which I have created inside the AndroidTest folder. I have moved it out of the custom directory, then it worked pretty well.

Answer:

The same phenomenon occurred to me.

following

implementation "android.arch.persistence.room:runtime:1.1.1"

Adding causes another build error but tracks the cause from the log.

In my case, there was an error in the SQL implementation.
After fixing, the build was successful.

So you may want to check the implementation of the entire room library instead of looking at the crashed locals.

Answer:

The issue is more around the correct library that is not included in the gradle build. I had a similar issue and added the missing

testImplementation “android.arch.persistence.room:testing:$room_version

Answer:

Changing the dependencies in my gradle file did’nt help me in fixing the error.I had missed this Database annotation in class where Room database was initialized which was causing this issue.

@Database(entities = [UserModel::class], version = 1)

Ensure that the entity model specified in the annotation above refers to the particular model class

Answer:

For me, the Android Studio automatically updated dependencies as soon as you include any of the Room database related imports. But as per https://developer.android.com/jetpack/androidx/releases/room#declaring_dependencies you need to update few. Here is how my code-base looks like:

AppDatabase.kt

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

@Database(entities = arrayOf(MyEntity::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun myDAO(): MyDAO

    companion object {
        @Volatile private var instance: AppDatabase? = null
        private val LOCK = Any()

        operator fun invoke(context: Context)= instance ?: synchronized(LOCK){
            instance ?: buildDatabase(context).also { instance = it}
        }

        private fun buildDatabase(context: Context) = Room.databaseBuilder(context,
            AppDatabase::class.java, "db-name.db")
            .build()
    }
}

Update the build.gradle as specified in one of the answers:

apply plugin: 'kotlin-kapt' // this goes with other declared plugin at top
dependencies { // add/update the following in dependencies section
    implementation 'androidx.room:room-runtime:2.2.3'
//    annotationProcessor 'androidx.room:room-compiler:2.2.3' // remove this and use the following
    kapt "androidx.room:room-compiler:2.2.3"

}

Sync the gradle and you should be good to go.

Answer:

Reading the example here:
Room Example

I fixed this error just using the correct (I guess it is) annotationProcessorFile, as follows:

annotationProcessor "android.arch.persistence.room:compiler:<latest_version>"

Also, I upgraded to 2.2.0 either in Room Version as in Lifecycle version.

Once synchronized the graddle, I could start working with Room.

So, Good luck! And let the code be with you!