Dagger2 where inject @Named @Provides in dependent module?
Your Retrofit provider should use @Named
annotation for OkHttpClient, for example:
@Provides
@Singleton
public Retrofit provideRetrofit(@Named("cached") OkHttpClient okHttpClient)
{
return new Retrofit.Builder()
.baseUrl("...")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
You have two methods with same name: provideOkHttpClient()
. Rename one of them, make them distinct.
If you are using kotlin, the correct way to inject named is next:
@field:[Inject Named("api1")]
.
Source: https://medium.com/@WindRider/correct-usage-of-dagger-2-named-annotation-in-kotlin-8ab17ced6928