To dynamically change the baseUrl to Retrofit, you need to pass into it not a hard-coded url, but the implementation of the BaseUrl interface BaseUrl
public class UrlProvider implements BaseUrl { private HttpUrl httpUrl; public UrlProvider(String url) { //передаем стандартный урл httpUrl = HttpUrl.parse(url); } // вызываем когда необходимо изменить урл public void changeUrl(@NonNull String url) { httpUrl = HttpUrl.parse(url); } @Override public HttpUrl url() { return httpUrl; } }
Well, in the module for Daggera do something like this
@Provides @Singleton UrlProvider urlProvider(PrefWrapper prefWrapper){ return new UrlProvider(prefWrapper.getPref().url()); } @Provides @Singleton Retrofit provideRetrofit(UrlProvider urlProvider){ return new Retrofit.Builder() //прочие настройки .baseUrl(urlProvider) .build(); }
If you need to change the url, simply request the UrlProvider dependency from UrlProvider , and set its new URL
PrefWrapper in this case is a wrapper around the SharedPreference which stores the current URL. You can use something of your own.