How to inject application context in a repository with Hilt?

25,737

Just use @ApplicationContext annotation on your context parameter.

By annotating context with @ApplicationContext provided by Hilt, we don't need to create a provider for the application context.

import dagger.hilt.android.qualifiers.ApplicationContext

/* For hilt versions lower than v2.28.2 use ApplicationComponent instead of
SingletonComponent. ApplicationComponent is deprecated and even removed in 
some versions above v2.28.2 so better refactor it to SingletonComponent. */


@Module
@InstallIn(SingletonComponent::class)
class ProductionModule {

    @Singleton
    @Provides
    fun provideAppDatabase(@ApplicationContext appContext: Context): AppDatabase {
        return Room
            .databaseBuilder(appContext, AppDatabase::class.java, AppDatabase.DB_NAME)
            .build()
    }
}

NOTE: In case you are tempted to pass activity context as a dependency, try to use application context or rethink your use-case. Passing activity context may lead to serious implications like memory leaks. Having said that, if you know what you are doing, use @ActivityContext annotation for passing the activity context. A possible use-case might be adapters.

Share:
25,737
Tushar
Author by

Tushar

Work mainly on web related projects. Developed apps with react and golang. Looking for a remote job.

Updated on July 18, 2022

Comments

  • Tushar
    Tushar almost 2 years

    I want to inject an application context into a repository class to create a room database instance inside the repository. I am using Hilt for dependency injection.

    Can we use hilt for passing application context or do I have to manually pass it?

    I am getting the following error:

    [Dagger/MissingBinding] android.content.Context cannot be provided without an
    @Provides-annotated method. public abstract static class ApplicationC 
    implements ExartApplication_GeneratedInjector
    

    Context Error

  • Arul
    Arul almost 4 years
    Is there any way to annotate ActivityContext ?
  • Dimitri Leite
    Dimitri Leite almost 4 years
    Saved me the day!
  • Ashu
    Ashu almost 4 years
    @ArulMani if you need activity context to be injected, maybe you are doing something wrong. ActivityContext is a fragile thing and I don't thing it is meant to be injected carelessly.
  • Rissmon Suresh
    Rissmon Suresh over 3 years
    @Ashu Is there a way to do field injection of context?
  • Ashu
    Ashu over 3 years
    @RissmonSuresh can you explain your use-case? Normally you would prefer to use constructor injection.
  • Rissmon Suresh
    Rissmon Suresh over 3 years
    @Ashu Just curious about the access of context(Any object) through field injection. Earlier I was using Koin for DI and field injection can be implemented like this stackoverflow.com/a/49629378/4247543
  • Ashu
    Ashu over 3 years
    @RissmonSuresh @Inject lateinit var... for field injection. For more info refer to the docs: developer.android.com/training/dependency-injection/…
  • Mohd Faizan
    Mohd Faizan over 3 years
    by default Applicationcomponent modules provide singleton scope as per developer.android.com/training/dependency-injection/… Application ApplicationComponent @Singleton
  • Ashu
    Ashu over 3 years
    @MohdFaizan I didn't exactly get what your point is. Can you clarify what it is that you are talking about?
  • Tanjim ahmed
    Tanjim ahmed almost 3 years
    savior of the day