So I'm trying to use Room on a personnal project. I have implemented my entities, dao and my roomdatabase extended application class :
@Database(
version = 1,
entities = [
UserDBEntity::class
]
)
abstract class MyDatabase: RoomDatabase() {
companion object {
const val DATABASE_NAME = "MyDb"
}
abstract fun getUserDao(): UserDao
}
However, I'm also trying to use Hilt DI with it, so, I created a module like that :
@InstallIn(ApplicationComponent::class)
@Module
object PersistenceModule {
lateinit var database: MyDatabase
@Provides
@Singleton
fun provideDatabase(@ApplicationContext context: Context): MyDatabase{
database = Room.databaseBuilder(
context,
MyDatabase::class.java,
MyDatabase.DATABASE_NAME
)
.fallbackToDestructiveMigration()
.build()
return database
}
@Provides
@Singleton
fun provideUserDao(db: MyDatabase): UserDao {
return db.getUserDao()
}
}
Everything seems good to me. However, when I try to compile, I've got this error message :
class file for androidx.room.RoomDatabase not found
Any idea of what I missed ?
If it can help you, I tried to change my build.gradle from this :
implementation "androidx.room:room-runtime:2.3.0-alpha02"
To this :
api "androidx.room:room-runtime:2.3.0-alpha02"
And with that, it compiles succefully, but I don't think it's a good practice, looking the tutorials I found.
Don't hesitate if you need more details on my code.
Thank you for your answers :)
EDIT :
My build.gradle for Room :
implementation "androidx.room:room-runtime:2.3.0-alpha02"
kapt "androidx.room:room-compiler:2.3.0-alpha02"
implementation "android.arch.persistence.room:runtime:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
With this also :
apply plugin: 'kotlin-kapt'
EDIT 2 :
I think I found what is the cause of the problem. I called my DAO method in a class like that :
@Singleton
class DBManagerImpl @Inject constructor(
private val userDao: UserDao
) : DBManager {
override fun insertUser(userDBEntity: UserDBEntity) {
userDao.insertUser(userDBEntity)
}
}
It seems the problem is in the userDao injection cause if I comment the "private val userDao: UserDao" line, I don't have the error anymore. So I think my problem comes from my way to inject the dao object.