I want to setup a variable SERVER_URL and it'll be switched between environments production, test, development.
What I want to do:
@Override
protected void onCreate(Bundle bundle) {
R.urls.SERVER_URL; // is it a valid approach using resources?
}
Is there a way to switch environments(dev, prod, test) without change the code?
What's the best approach to implement this behavior?
Is there a way to configure it in the playstore my variable(SERVER_URL) or must I implement only in code?
There are 2 ways you can do it:
1/ By
stringresource like you wantAdd a resource file called
secret_keys.xmlor whatever name to separate it from other resources file. Put your keys, api endpoints here as the normal string resource, remember to addtranslatable="false"if you don't want to mess with localization.Place that file in
app/debug/res/values/. Create a newdebugfolder if it doesn't exist. Do the same forstagingorrelease, Android will automatically use the folder with the same name as the build type.2/ By
propertiesfilesCreate 3 .properties files and put your keys inside:
Bind it to
BuildConfigvariable in your appbuild.gradle, do the same for other build types:In your app just call
BuildConfig.HOSTto get the string you wantUPDATE
Ignore these config files in
.gitignore: