title: Set dynamic base url using Retrofit 2.0 and Dagger 2
body: <p>I'm trying to perform a login action using Retrofit 2.0 using Dagger 2</p>
  
  <p>Here's how I set up Retrofit dependency</p>
  
  <pre><code>@Provides
  @Singleton
  Retrofit provideRetrofit(Gson gson, OkHttpClient client) {
      Retrofit retrofit = new Retrofit.Builder()
                              .addConverterFactory(GsonConverterFactory.create(gson)
                              .client(client)
                              .baseUrl(application.getUrl())
                              .build();
      return retrofit;     
  }
  </code></pre>
  
  <p>Here's the API interface.</p>
  
  <pre><code>interface LoginAPI {
     @GET(relative_path)
     Call&lt;Boolean&gt; logMe();
  }
  </code></pre>
  
  <p>I have three different base urls users can log into. So I can't set a static url while setting up Retrofit dependency. I created a setUrl() and getUrl() methods on Application class. Upon user login, I set the url onto Application before invoking the API call. </p>
  
  <p>I use lazy injection for retrofit like this</p>
  
  <pre><code>Lazy&lt;Retrofit&gt; retrofit
  </code></pre>
  
  <p>That way, Dagger injects dependency only when I can call</p>
  
  <pre><code>retrofit.get()
  </code></pre>
  
  <p>This part works well. I got the url set to retrofit dependency. However, the problem arises when the user types in a wrong base url (say, mywifi.domain.com), understands it's the wrong one and changes it(say to mydata.domain.com). Since Dagger already created the dependency for retrofit, it won't do again.
  So I have to reopen the app and type in the correct url.</p>
  
  <p>I read different posts for setting up dynamic urls on Retrofit using Dagger. Nothing really worked out well in my case. Do I miss anything? </p>