As per documentation for Hilt, I completed all the steps
Add Class Path to Project Module
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
Add Plugins
plugins {
id "kotlin-kapt"
id("dagger.hilt.android.plugin")
}
Add Dependencies
dependencies {
implementation("com.google.dagger:hilt-android:$hilt_version")
kapt("com.google.dagger:hilt-android-compiler:$hilt_version")
}
Annotated Application Class, in my case AppClass, with @HiltAndroidApp
. Annotated Activity and regarding fragment with @AndroidEntryPoint
.
Created Module as
@InstallIn(AppClass::class)
@Module
object DatabaseModule {
@Provides
@Singleton
fun provideAppDB(application: Application): AppDB {
return AppDB.getDatabase(application)
}
@Provides
@Singleton
fun provideMediaDao(appDB: AppDB): MediaDao {
return appDB.downloadMediaDao()
}
}
Injected it in ViewModel class as
@HiltViewModel
class DownloadViewModel @Inject constructor(
private val mediaDao: MediaDao
): ViewModel() {
...
}
But After following all the steps I got the error saying "@InstallIn, can only be used with @DefineComponent-annotated classes, but found: [com.abc.xyz.AppClass]"