No injectable members on android.app.Application. Do you want to add an injectable constructor? required by provideOkHttpClient

StackOverflow https://stackoverflow.com/questions/22942327

  •  29-06-2023
  •  | 
  •  

سؤال

I am trying to merge U2020 and Dagger's example android-activity-graphs in the following repo.

However, I am getting compile time error:

Error:(32, 8) error: No injectable members on android.app.Application. Do you want to add an injectable constructor? required by provideOkHttpClient(android.app.Application) for org.kamol.nefete.ActivityModule
هل كانت مفيدة؟

المحلول

You haven't bound Application, you have only bound Context, but somewhere else in your graph, you have some type that depends on Application. Downstream dependencies which need Application will attempt to implicitly bind it because Dagger cannot know that an object which requires Application can be satisfied by @ForApplication Context. Since Application does not have an @Inject constructor nor @Inject fields, Dagger will fail.

You can fix your example by simply adding

@Provides @Singleton Application provideApplicationContext() {
  return application;
}

in org.kamol.nefete.AndroidModule

As an aside, I recommend against binding Context as it is too abstract a supertype and too easy to confuse with Activity Contexts, even if you are binding with a @Qualifier annotation, such as @ForApplication Context. I would recommend instead binding Application and having things depend on the more concrete type.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top