Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@InstallIn can only be used on @Module or @EntryPoint classes

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()
}
like image 220
enesigneci Avatar asked Dec 11 '20 22:12

enesigneci


People also ask

What is hilt InstallIn?

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.

What is Dagger Hilt in Android?

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.

Why Hilt Android?

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.


2 Answers

I set a import of Module as following:

import com.google.android.datatransport.runtime.dagger.Module

But the below is correct:

import dagger.Module

like image 105
user3813078 Avatar answered Sep 28 '22 06:09

user3813078


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
like image 45
charlie.7 Avatar answered Sep 28 '22 06:09

charlie.7