Background
I am converting my app to MVP architecture and found Dagger 2 to be useful to inject dependencies when needed. My app needs to communicat
You are already halfway through the solution. To complete the solution try to do the following:
@Provides
@Named("myApiRestAdapter")
RestAdapter provideMyRestAdapter(@Named("myApiGson") Gson gson, OkHttpClient okHttpClient) {
return new RestAdapter.Builder()
.setEndpoint(MY_API_URL)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.build();
}
@Provides
@Named("thirdPartyApiRestAdapter")
RestAdapter provideThirdPartyRestAdapter(@Named("thirdPartyApiGson") Gson gson, OkHttpClient okHttpClient) {
return new RestAdapter.Builder()
.setEndpoint(THIRD_PARTY_API_URL)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.build();
}
To make sure that only two instances of your RestAdapters are created during the lifetime of the application, annotate both the methods providing RestAdapter with @Singleton like you have done with your other methods. As for your other question whether Dagger 2 will create new instance of RestAdapter every time it has to inject it, I think it does this exactly, but I'm not sure on this.
Hope this helps!