We have an Android app now that are working fine using Retrofit. We are trying to automate the build for testing and production release, to reduce the chance of human error (i.e. dev forget to point the Base URL to production when build for production..etc)
I noticed that the Base URL for Retrofit are defined in the class file. Is there any method that we can use to configure it to be in a properties file or xml so that we can use the Gradle script to pick the right file to be included with the build?
Thank you
You can define a constant in your buildTypes:
buildTypes {
debug {
buildConfigField "String", "API_URL", "\"https://url/pre/\""
}
release {
buildConfigField "String", "API_URL", "\"https://url/pro/\""
}
}
And then use it in the retrofit builder:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.build();
Notice that if you have different modules the BuildConfig is defined in each of them. For this cases I define the buildTypes in the app module and pass the BuildConfig.API_URL as parameter in the call to the module with the retrofit builder.