Android Room Persistent: Appdatabase_Impl Does Not Exist

Android room persistent: AppDatabase_Impl does not exist

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.3.0" // check latest version from docs

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

UPDATE 2 (since July 2021)

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

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

Room persistance AppDatabase_Impl does not exist android

When we are working with Room persistence it's good to define proper annotation for each class.

1) Define your Entity class with annotation @Entity like below

@Entity(tableName = "notifications")
public class NotificationDetail {

@PrimaryKey(autoGenerate = true)
private int notification_id;

@ColumnInfo(name = "message")
private String message;

@ColumnInfo(name = "title")
private String title;
}

2) Define your Dao interface with @Dao annotation,like below

@Dao
public interface NotificationDao {

@Query("SELECT * FROM notifications")
List<NotificationDetail> getAll();

@Insert
void insertNotification(NotificationDetail... users);

@Update
void updateNotification(NotificationDetail notificationDetail);

@Delete
void delete(NotificationDetail user);
}

3) Database class that extends RoomDatabase define the @Database annotation with all the Entity class,like below

@Database(entities = {NotificationDetail.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

private static final String DATABASE_NAME = "notification_room_db";

public abstract NotificationDao userDao();


private static AppDatabase INSTANCE;

public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
// allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}

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

I have forgot to add @Database annotation in database class

Android room shows the error AppDatabase_Impl does not exist

I have added @Database(entities = {Class_.class}, version = 1) in AppDatabase.java . The @Database annotation for your database class, and @Entity for your entity. I have given as correct. Then the issue solved.

Room Persistence Library run time exception when calling Rooms inMemoryBuilder method

I changed the 'annotationProcessor' keyword to 'kapt' in my gradle file. Like so:

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


Related Topics



Leave a reply



Submit