Skip to content
Advertisement

Room database not created

I’m trying to develop an app with Room. The project is in java. This is my first time using Room.

I’ve followed the documentation and created class with @Database, @Dao & @Entity. However when I run the app, it throws the following exception.

Can someone please help me figure out what am I doing wrong here? I’ve spent a day on it but still couldn’t figure out.

2021-02-07 20:29:16.151 13959-13959/com.hello.testapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hello.testapp, PID: 13959
    java.lang.RuntimeException: cannot find implementation for com.hello.testapp.persistance.MyDatabase. MyDatabase_Impl does not exist   <----- ERROR
        at androidx.room.Room.getGeneratedImplementation(Room.java:94)
        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:952)
        at com.hello.testapp.persistance.MyDatabase.getInstance(MyDatabase.java:33)
        at com.hello.testapp.repositories.MyClientRepo.<init>(MyClientRepo.java:30)
        at com.hello.testapp.SplashActivity$1.run(SplashActivity.java:45)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Following are the classes:

Category.java:

@Entity(tableName = "category")
public class Category {

    @PrimaryKey
    @SerializedName("category_name")
    @Expose
    private String categoryName;
    @SerializedName("type")
    @Expose
    private int type;

    public Category() {}

    public Category(String categoryName, int type) {
        this.categoryName = categoryName;
        this.type = type;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}

MyDao.java:

@Dao
public interface MyDao {

    @Query("SELECT * FROM category LIMIT (:pageNum * 10)")
    LiveData<List<Category>> getCategories(int pageNum);
}

MyDatabase.java:

@Database(entities = {Category.class}, version = 1)
@TypeConverters({ListConverter.class})
public abstract class MyDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "my_db";
    private static MyDatabase db;

    public static MyDatabase getInstance(Context context) {
        if(db == null) {
            System.out.println("creating instance");
            db = Room.databaseBuilder(
                    context.getApplicationContext(),
                    MyDatabase.class,
                    DATABASE_NAME
            ).build();
        }

        return db;
    }
    public abstract MyDao getDao();
}

ListConverter.java:

public class ListConverter {

    @TypeConverter
    public static List<String> fromString(String value) {
        Type listType = new TypeToken<List<String>>(){}.getType();
        return new Gson().fromJson(value, listType);
    }

    @TypeConverter
    public static String fromList(List<String> list) {
        return new Gson().toJson(list);
    }
}

Build.gradle

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

dependencies {
    ..other dependencies

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

    def hilt_version = '2.31.2-alpha'
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
}

Please help me understand where am I making a mistake.

Advertisement

Answer

The problem you’re facing is because of the java annotations eg. in this case @Database. Kotlin doesn’t play well with the java annotations, that’s one of the reasons I know which caused such problem (Database_Impl.java) not found.

In order to fix it in your java project, you need to update the dependencies like the following & change the highlighted part from annotationProcessor to implementation. I still need to understand why it works when changing to implementation so if someone can comment below, that’ll be really helpful.

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

//add the following & instead of annotationProcessor use implementation
implementation "android.arch.persistence.room:compiler:$room_version" 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement