95

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

Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
Tushar
  • 1,076
  • 1
  • 9
  • 11
  • Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. If you post images of code or error messages, make sure you also copy and paste or type the actual code/message into the post directly. Please see [Why may I not upload images of code on SO when asking a question?](//meta.stackoverflow.com/a/285557/208273)—the same reasoning applies to error messages as well. Posts in which required text content is only present in images are likely to be closed as lacking enough details. – Ryan M Jul 28 '20 at 00:02
  • Hey... Can you accept my answer if it helped..?? I would greatly help :) – Ashu Aug 20 '20 at 19:46

1 Answers1

208

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.

Ashu
  • 2,970
  • 1
  • 19
  • 31
  • 1
    Is there any way to annotate ActivityContext ? – Arul Aug 11 '20 at 16:05
  • 2
    @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. – Ashu Aug 19 '20 at 14:51
  • @Ashu Is there a way to do field injection of context? – Rissmon Suresh Nov 20 '20 at 13:13
  • @RissmonSuresh can you explain your use-case? Normally you would prefer to use constructor injection. – Ashu Nov 23 '20 at 08:12
  • @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 https://stackoverflow.com/a/49629378/4247543 – Rissmon Suresh Nov 23 '20 at 08:53
  • @RissmonSuresh `@Inject lateinit var...` for field injection. For more info refer to the docs: https://developer.android.com/training/dependency-injection/hilt-android – Ashu Nov 26 '20 at 10:14
  • by default Applicationcomponent modules provide singleton scope as per https://developer.android.com/training/dependency-injection/hilt-android Application ApplicationComponent @Singleton – Faizzy Jan 20 '21 at 18:13
  • @MohdFaizan I didn't exactly get what your point is. Can you clarify what it is that you are talking about? – Ashu Jan 22 '21 at 13:29