I understood so. If you need to pass parameters to the method that will be injected, then you need to make another method that will provide this parameter to Dagger .

That's what there is

 @Module class ModelModule { @Provides @Singleton ApiInterface provideApiInterface() { return ApiModule.getApiInterface(); } } 

A method that works great. Now, if we need to add a parameter to this method, then we immediately need to create a method that will provide this parameter

like this

 @Module class ModelModule { @Provides @Singleton int provideInt() { return 1; } @Provides @Singleton ApiInterface provideApiInterface(int i) { return ApiModule.getApiInterface(i); } } 

But the thing is that I already have another module in which the method with the return type int initialized

 @Module class AnotherModule { @Provides Integer getInt(){ return 3; } } 

And it turns out that when I start, I get this error

Error: (11, 10) error: java.lang.Integer is bound multiple times: @Provides @ Singleton int com.krokosha.aleksey.daggertwo.ModelModule.provideInt () @Provides Integer com.krokosha.aleksey.daggertwo.AnotherModule. getInt ()

It turns out I can not use 2 methods that return the same type ... But how then to be?

I need him both there and there ...

  • If you need exactly integer, then wrap it in a wrapper as XyzSettings, because just the int in the container should not roll. If you have something else, then start with the @Singleton documentation, because this annotation clearly indicates the presence of only one instance in the container. - etki
  • It is possible through Qualifier ... - Yura Ivanov
  • @Etki, in this case, the container in which the int should not roll? As for sington, I tried to delete these annotations, but still the same error ... Can you give an example of how this code should look like? - Aleksey Timoshchenko
  • @YuraIvanov maybe I don’t understand something, maybe the library is not thought out (although I’m more inclined to the first)). This is a very standard situation. If a method accepts a parameter, how can I pass a parameter there? In general, of course, the idea of ​​passing parameters is confusing. To pass a parameter, you need to create a method that will provide the desired value, and the return value should not be repeated in any of the methods of any module ... Although there can be as many modules as possible ... Strange . And plus to everything, if the transmitted parameter is not static but depends on something ... - Aleksey Timoshchenko
  • Tell us what you want to do? Why parameters ApiInterface? - katso

1 answer 1

In the end, I used the Qualifier and it turned out like this

 @Module class ModelModule { @Provides @Named("FirstInt") int provideInt() { return 1; } } @Module class AnotherModule { @Provides @Named("SecondInt") int provideInt() { return 1; } } @Inject protected ApiInterface apiInterface; @Inject @Named("FirstInt") //or whatever you need protected Integer valueInt;