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

Viewed 3971

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()
}
5 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

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

change ApplicationComponent::class to SingletonComponent::class and also you can find more hilt generated components by referring this Hilt Generated Components

@Module
@InstallIn(SingletonComponent::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()
}

It looks like you add @InstallIn annotation to not related class in your project.

In my case, import was incorrect

from:

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

to:

import dagger.Module
import dagger.Provides
Related