I am new at the dependency injection on Android. I am using Dagger-Hilt and in AppModule class that I generated for the DB providers I got an error and the project doesn't compile.
The error is @InstallIn can only be used on @Module or @EntryPoint classes
This is my AppModule object. Where do I make mistake?
@Module
@InstallIn(ApplicationComponent::class)
object AppModule {
@Singleton
@Provides
fun provideAppDatabase(
@ApplicationContext app: Context
) = Room.databaseBuilder(
app,
AppDatabase::class.java,
"gelirkenal"
).build()
@Singleton
@Provides
fun provideItemDao(db: AppDatabase) = db.itemDao()
}
A module is installed in a Hilt Component by annotating the module with the @InstallIn annotation. These annotations are required on all Dagger modules when using Hilt, but this check may be optionally disabled.
Hilt provides a standard way to incorporate Dagger dependency injection into an Android application. The goals of Hilt are: To simplify Dagger-related infrastructure for Android apps. To create a standard set of components and scopes to ease setup, readability/understanding, and code sharing between apps.
Hilt is a dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project. Doing manual dependency injection requires you to construct every class and its dependencies by hand, and to use containers to reuse and manage dependencies.
I set a import of Module as following:
import com.google.android.datatransport.runtime.dagger.Module
But the below is correct:
import dagger.Module
Change the following imports:
import com.google.android.datatransport.runtime.dagger.Module
import com.google.android.datatransport.runtime.dagger.Binds
To =>
import dagger.Module
import dagger.Binds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With