Android Room error: Dao class must be annotated with @Dao Android Room error: Dao class must be annotated with @Dao database database

Android Room error: Dao class must be annotated with @Dao


Check your database class. When you define DAO, you must have use wrong type(Device instead of DeviceDAO).

Incorrect

public abstract Device deviceDao();

Correct

public abstract DeviceDAO deviceDao();

Hope this will work. Thanks


Error Message:Dao class must be annotated with @Dao

To solve error please read it properly.

If this error messages shows on Model class then you need to modify your AppDatabase class. I am giving you the code what gives error then error corrected code.

Error Code:

MyImage.java

@Entitypublic class MyImage {    @PrimaryKey(autoGenerate = true)    private int uid;    @ColumnInfo(name = "title")    private String title;    @ColumnInfo(name = "photo")    private String photo;    public MyImage(String title, String photo) {        this.title = title;        this.photo = photo;    }    public int getUid() {        return uid;    }    public void setUid(int uid) {        this.uid = uid;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getPhoto() {        return photo;    }    public void setPhoto(String photo) {        this.photo = photo;    }}

MyImageDao.java

@Daopublic interface MyImageDao {    @Query("SELECT * FROM myimage")    List<MyImage> getAll();    @Insert    void insertAll(MyImage... myImages);    @Delete    void delete(MyImage myImage);}

AppDatabase.java

@Database(entities = {MyImage.class}, version = 1)public abstract class AppDatabase extends RoomDatabase {    public abstract MyImage myImageDao();}

Here has error on only AppDatabase.java file, you can see myImageDao has return type MyImage, that means it assumed that MyImage is a Dao class but MyImage is model class and MyImageDao is Dao class.So it need to modify AppDatabase.java class and MyImage to MyImageDao.

The corrected code is-

AppDatabase.java

@Database(entities = {MyImage.class}, version = 1)public abstract class AppDatabase extends RoomDatabase {    public abstract MyImageDao myImageDao();}


For Kotlin users :

Check if you've added following line in your Database file.

abstract val myDatabaseDao:MyDatabaseDao