0
votes

I am trying to inject a Room DAO into a repository using Hilt. I am using the following code:

def room_version = "2.2.6"
ext.hilt_version = '2.33-beta'

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-guava:$room_version"
testImplementation "androidx.room:room-testing:$room_version"

implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

DAO

@Dao
interface SearchDAO {

    @Query("SELECT * FROM searches_table WHERE name LIKE '%' || :query || '%'")
    fun readSearches(query : String) : Flow<List<SearchItem>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSearch(search : SearchItem)

    @Delete
    suspend fun deleteSearch(search: SearchItem)
}

Database

@Database(
        entities = [SearchItem::class],
        version = 1
)
abstract class SearchesDatabase : RoomDatabase(){
    abstract fun getSearchesDao() : SearchDAO
}

Providers

@Module
@InstallIn(ActivityComponent::class)
object AppModule{
    @Singleton
    @Provides
    fun provideSearchDatabase(@ApplicationContext context : Context) =
        Room.databaseBuilder(context, SearchesDatabase::class.java, SEARCH_DATABASE_NAME)
            .fallbackToDestructiveMigration()
            .build()

    @Provides
    fun provideSearchDAO(appDatabase: SearchesDatabase): SearchDAO {
        return appDatabase.getSearchesDao()
    }
}

Base Application

@HiltAndroidApp
class BaseApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        //Timber.plant(Timber.DebugTree())
    }
}

Repository injecting

class Repository @Inject constructor(
    val searchesDao : SearchDAO
){
...
}

And the problem is that I receive the following error:

[Dagger/MissingBinding] com.example.leagueapp.database.SearchDAO cannot be provided without an @Provides-annotated method. public abstract static class SingletonC implements BaseApplication_GeneratedInjector

2
try with @InstallIn(ApplicationComponent::class) instead of @InstallIn(ActivityComponent::class) - Kishan Maurya
Thanks but I think is deprecated, I can't find it using the current hilt version - Alex Pop
I figured it out, I had to use SingletonComponent. Thanks @KishanMaurya for the input - Alex Pop

2 Answers

3
votes

Short answer: anything @Singleton must be installed in a SingletonComponent.

Explanation: singleton's are meant to live for the life of the process they are in. Your database is a singleton (as it probably should be) but you are using @InstallIn(ActivityComponent::class) which tells dagger that everything which is provided in this Module should be scoped to the lifecycle of an activity; when the Activity dies the module dies.

1
votes

please write the following code in your App Module class. you just need to write SingletonComponent::class instead of ActivityComponent::class in @installIn() annotation :)

@Module
@InstallIn(SingletonComponent::class)
object AppModule{
    @Singleton
    @Provides
    fun provideSearchDatabase(@ApplicationContext context : Context) =
        Room.databaseBuilder(context, SearchesDatabase::class.java, SEARCH_DATABASE_NAME)
            .fallbackToDestructiveMigration()
            .build()

    @Provides
    fun provideSearchDAO(appDatabase: SearchesDatabase): SearchDAO {
        return appDatabase.getSearchesDao()
    }
}