5

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"
8
  • Did you add the annotation processor to your dependencies closure in build.gradle? See developer.android.com/topic/libraries/architecture/… Commented Apr 12, 2018 at 15:51
  • Yes, I did. Just added it to the post Commented Apr 12, 2018 at 15:54
  • The error in your title differs from the error in your question. Which of these are you getting? If you are getting the "usable public constructor" one, that would be tied to your @Entity classes or any POJOs returned by methods on your @Dao class. Commented Apr 12, 2018 at 15:56
  • Thank you for catch! It is not correct title. I fixed it Commented Apr 12, 2018 at 16:05
  • OK. The annotation processor should be generating the code in build/generated/source/apt/debug/com/home/myapplication/films/storage/impl/, inside of your module (e.g., app/). Is anything showing up there? Commented Apr 12, 2018 at 16:15

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. It was a while when I worked with Room, may be I already found the answer. I don't remember. I'll accept your replay when find time to verify it.
yeah just in case someone else could stumble in the same issue, hope it might help then
Four (4) years later and have exact same problem!
0

solution: def room_version = "2.3.0"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.