Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot find implementation for database Room

I am writing my first application with Room. I discovered it has issues when it uses kotlin, even some sample doesn't work on my machine, so I rollback to plain Java.

FilmsDatabaseJ db = Room.databaseBuilder(getApplicationContext(), FilmsDatabase.class, "DATABASE_NAME").build();

This call fails with exception:

java.lang.RuntimeException: cannot find implementation for com.home.myapplication.films.storage.FilmsDatabase. FilmsDatabase_Impl does not exist

I explored source code, Room expects to have such class already (does generated somewhere?), but by some reason such class doesn't exist for my case which is not far from documentation. Could you please share your ideas what might went wrong here?

@TypeConverters({Converters.class})
@Database(entities = {Film.class, User.class, UserFilms.class}, version = 1)
public abstract class FilmsDatabaseJ extends RoomDatabase {

    private static final String DATABASE_NAME = "DATABASE_NAME";

    private static FilmsDatabaseJ instance;

    public abstract FilmsDaoJ getFilmsDao();


    @NotNull
    public static FilmsDatabaseJ getInstance(Context context) {
        if (instance == null) {
            synchronized (FilmsDatabaseJ.class) {
                if (instance != null) return
                instance = Room.databaseBuilder(context, FilmsDatabaseJ.class, DATABASE_NAME).build();
            }
        }
        return instance;
    }
} 



   annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

    // Room (use 1.1.0-beta2 for latest beta)
    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
like image 891
Gleichmut Avatar asked Jan 02 '23 13:01

Gleichmut


1 Answers

This is because the kotlin compiler doesn't work well with @Database java annotation (which generates the FilmsDatabase_Impl for you).

So to fix this:

  • First you need to get the kapt plugin to parse annotation in kotlin file, add this to your app level gradle:

    apply plugin: 'kotlin-kapt'

  • Then replace annotationProcessor with kapt for the Room's compiler

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

  • Compiling the project again now you should see those boiler plate codes get generated and resolve the above error.

like image 82
Trung Le Avatar answered Jan 05 '23 15:01

Trung Le