0

I'm new to dagger and I'm studding it with small app for android. I'm trying to use Retrofit for REST request over http and https to two servers (dev and prod). So I have module in debug flavour where :

@dagger.Module(overrides = true, library = true, complete = false)
public class DebugApiModule {
    private static final String SRV = "dev.mysrv.com";
    private static final String HTTP = "http://";
    private static final String HTTPS = "https://";

    public static final String API_URL = HTTP + SRV;
    public static final String API_URL_SECURE = HTTPS + SRV;

    @Provides @Singleton @Api String provideApi() { return API_URL; }
    @Provides @Singleton @ApiSecure String provideApiSecure() { return API_URL_SECURE; }
}

Here I'm using annotation in order to differ two strings but I get error:

Error:(23, 50) error: Duplicate bindings for java.lang.String in override module(s) - cannot override an override:
com.myapp.common.api.DebugApiModule.provideApi()
com.myapp.common.api.DebugApiModule.provideApiSecure()

what's wrong with this code ?

oleg.semen
  • 2,901
  • 2
  • 28
  • 56

1 Answers1

2

Both the @Api and @ApiSecure annotations need to be annotated with javax.inject.Qualifier in order for Dagger to use them as a differentiating factor.

@Qualifier
@Retention(RUNTIME)
public @interface Api {
}
Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
  • Thanks Jake, I missed `@Qualifier` annotation. I'm using your u2020 app as reference and there is `@ClientId` annotation also without `@Qualifier` annotation. So why does it work in your app ? – oleg.semen Oct 17 '14 at 16:29
  • Oh, I'm sorry, it is not your app, it is port of your app by lemon labs, where they added NavigationDrawer, Mortar an Flow. ([github](https://github.com/lemonlabs/u2020-mortar/blob/ab9b1b06b37bd9c67dcdd614548fa1899b62f8f3/app/src/main/java/co/lemonlabs/mortar/example/data/api/ClientId.java)) in your app there is `@Qualifier` ([github](https://github.com/JakeWharton/u2020/blob/d5dc1aff3eb23ab185bb191f609b6f6187dcfaaa/src/main/java/com/jakewharton/u2020/data/api/ClientId.java)) So it's rether question to lemonlabs – oleg.semen Oct 20 '14 at 08:32