@InstallIn can only be used on @Module or @EntryPoint classes
Asked Answered
L

5

10

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()
}
Lawyer answered 11/12, 2020 at 22:17 Comment(2)
did you find the solution?Berton
Still not found a solution? My problem: @ InstallIn, can only be used with @ DefineComponent - annotated classesHealey
H
25

I set a import of Module as following:

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

But the below is correct:

import dagger.Module

Hestia answered 7/2, 2021 at 11:20 Comment(0)
F
8

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
Filamentary answered 31/8, 2021 at 19:30 Comment(0)
D
6

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()
}
Dorsoventral answered 15/5, 2021 at 16:38 Comment(0)
B
0

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

Bordelon answered 12/12, 2020 at 9:28 Comment(1)
I just added the @InstallIn annotation into AppModule. Also added @HiltAndroidApp to Application class, @AndroidEntryPoint to Activities and Fragments, @ViewModelInject to ViewModel constructor. Where is the mistake?Lawyer
G
0

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
Gayla answered 29/12, 2021 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.