I am using Dagger 2 + Retrofit to implement my interfaces which sends/receives data to/from my web service
I am referring Philippe BOISNEY's AppModule.java as below
private static String BASE_URL = "https://api.github.com/";
@Provides
Gson provideGson() { return new GsonBuilder().create(); }
@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(BASE_URL)
.build();
return retrofit;
}
However I have a question that what if I have multiple hosts of my web services, such as Production, Staging and Development?
I already setup those different hosts connected to Build Config in my AndroidManifest.xml, but I don't have an idea how to read meta-data in AppModule, in order to replace BASE_URL with corresponding build config
Please kindly advice me, thank you
You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable
And after that use it
If you like to provide it dynamically using dagger, you can do it in that way
Another way of dynamically providing server url using dagger - using builder. For example,
I give up reading corresponding host from meta-data in AndroidManefist, instead, I switch to another approach by referring @ConstOrVar's suggestion
Here is my app/build.gradle, I have 2 flavors which describes my staging and production host respectively
Then here is my codes in my AppModule.java
By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically