How to read hostname from meta-data when providing Retrofit in DI Modules?
up vote
0
down vote
favorite
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
android retrofit dagger
add a comment |
up vote
0
down vote
favorite
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
android retrofit dagger
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
android retrofit dagger
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
android retrofit dagger
android retrofit dagger
asked Nov 8 at 10:35
holy934
328
328
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable
productFlavors {
dev {
buildConfigField "String", "SERVER_URL", ""your dev url""
}
stage {
buildConfigField "String", "SERVER_URL", ""your stage url""
}
prod {
buildConfigField "String", "SERVER_URL", ""your prod url""
}
}
And after that use it
private static String BASE_URL = BuildConfig.SERVER_URL;
If you like to provide it dynamically using dagger, you can do it in that way
@Module
public class AppModule {
@Named("server_url")
@Provides
String provideServerUrl() {
return "https://api.github.com/";
}
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
Another way of dynamically providing server url using dagger - using builder. For example,
@Component(AppModule.class)
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder serverUrl(@Named("server_url") String url);
AppComponent build();
}
}
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
DaggerAppComponent.Builder()
.appModule(new AppModule())
.serverUrl("https://api.github.com/")
.build();
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
add a comment |
up vote
0
down vote
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
productFlavors {
production {
buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
}
staging {
buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
}
}
Then here is my codes in my AppModule.java
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://" + BuildConfig.SERVER_HOST)
.build();
return retrofit;
}
}
By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable
productFlavors {
dev {
buildConfigField "String", "SERVER_URL", ""your dev url""
}
stage {
buildConfigField "String", "SERVER_URL", ""your stage url""
}
prod {
buildConfigField "String", "SERVER_URL", ""your prod url""
}
}
And after that use it
private static String BASE_URL = BuildConfig.SERVER_URL;
If you like to provide it dynamically using dagger, you can do it in that way
@Module
public class AppModule {
@Named("server_url")
@Provides
String provideServerUrl() {
return "https://api.github.com/";
}
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
Another way of dynamically providing server url using dagger - using builder. For example,
@Component(AppModule.class)
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder serverUrl(@Named("server_url") String url);
AppComponent build();
}
}
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
DaggerAppComponent.Builder()
.appModule(new AppModule())
.serverUrl("https://api.github.com/")
.build();
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
add a comment |
up vote
2
down vote
accepted
You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable
productFlavors {
dev {
buildConfigField "String", "SERVER_URL", ""your dev url""
}
stage {
buildConfigField "String", "SERVER_URL", ""your stage url""
}
prod {
buildConfigField "String", "SERVER_URL", ""your prod url""
}
}
And after that use it
private static String BASE_URL = BuildConfig.SERVER_URL;
If you like to provide it dynamically using dagger, you can do it in that way
@Module
public class AppModule {
@Named("server_url")
@Provides
String provideServerUrl() {
return "https://api.github.com/";
}
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
Another way of dynamically providing server url using dagger - using builder. For example,
@Component(AppModule.class)
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder serverUrl(@Named("server_url") String url);
AppComponent build();
}
}
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
DaggerAppComponent.Builder()
.appModule(new AppModule())
.serverUrl("https://api.github.com/")
.build();
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable
productFlavors {
dev {
buildConfigField "String", "SERVER_URL", ""your dev url""
}
stage {
buildConfigField "String", "SERVER_URL", ""your stage url""
}
prod {
buildConfigField "String", "SERVER_URL", ""your prod url""
}
}
And after that use it
private static String BASE_URL = BuildConfig.SERVER_URL;
If you like to provide it dynamically using dagger, you can do it in that way
@Module
public class AppModule {
@Named("server_url")
@Provides
String provideServerUrl() {
return "https://api.github.com/";
}
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
Another way of dynamically providing server url using dagger - using builder. For example,
@Component(AppModule.class)
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder serverUrl(@Named("server_url") String url);
AppComponent build();
}
}
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
DaggerAppComponent.Builder()
.appModule(new AppModule())
.serverUrl("https://api.github.com/")
.build();
You can define in build.gradle several flavor types like dev, prod and stage and for each flavor define build config variable
productFlavors {
dev {
buildConfigField "String", "SERVER_URL", ""your dev url""
}
stage {
buildConfigField "String", "SERVER_URL", ""your stage url""
}
prod {
buildConfigField "String", "SERVER_URL", ""your prod url""
}
}
And after that use it
private static String BASE_URL = BuildConfig.SERVER_URL;
If you like to provide it dynamically using dagger, you can do it in that way
@Module
public class AppModule {
@Named("server_url")
@Provides
String provideServerUrl() {
return "https://api.github.com/";
}
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
Another way of dynamically providing server url using dagger - using builder. For example,
@Component(AppModule.class)
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder serverUrl(@Named("server_url") String url);
AppComponent build();
}
}
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson, @Named("server_url") String url) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(url)
.build();
return retrofit;
}
}
DaggerAppComponent.Builder()
.appModule(new AppModule())
.serverUrl("https://api.github.com/")
.build();
edited Nov 8 at 18:07
answered Nov 8 at 13:13
ConstOrVar
91446
91446
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
add a comment |
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
Really appreciate @ConstOrVar suggestion so that I probably know how to correct my codes as my own answer
– holy934
Nov 9 at 2:36
add a comment |
up vote
0
down vote
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
productFlavors {
production {
buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
}
staging {
buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
}
}
Then here is my codes in my AppModule.java
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://" + BuildConfig.SERVER_HOST)
.build();
return retrofit;
}
}
By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically
add a comment |
up vote
0
down vote
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
productFlavors {
production {
buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
}
staging {
buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
}
}
Then here is my codes in my AppModule.java
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://" + BuildConfig.SERVER_HOST)
.build();
return retrofit;
}
}
By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically
add a comment |
up vote
0
down vote
up vote
0
down vote
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
productFlavors {
production {
buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
}
staging {
buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
}
}
Then here is my codes in my AppModule.java
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://" + BuildConfig.SERVER_HOST)
.build();
return retrofit;
}
}
By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically
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
productFlavors {
production {
buildConfigField("String", "SERVER_HOST", ""myproductionost.com"")
}
staging {
buildConfigField("String", "SERVER_HOST", ""mystaginghost.com"")
}
}
Then here is my codes in my AppModule.java
@Module
public class AppModule {
@Provides
Retrofit provideRetrofit(Gson gson) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://" + BuildConfig.SERVER_HOST)
.build();
return retrofit;
}
}
By doing so, Retrofit will tells which host of webservice it shall talk with based on BuildVariant automatically
answered Nov 9 at 3:13
holy934
328
328
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205937%2fhow-to-read-hostname-from-meta-data-when-providing-retrofit-in-di-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown