Is there a way to set environment variables in an Android project? For example, in a situation where you want to use your app with different webservers in development and production, where could you place the different URLs for this?
Asked
Active
Viewed 2,544 times
1 Answers
14
For example, in a situation where you want to use your app with different webservers in development and production, where could you place the different URLs for this?
If you are using Android Studio, this is handled by configuring your build types.
In your android
closure, you can have a buildTypes
closure where you define constants to be added to the BuildConfig
class:
buildTypes {
debug {
buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"'
}
release {
buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
}
}
Now, in my Java code, I can refer to BuildConfig.SERVER_URL
. A debug
build will give me the debug URL; a release
build will give me the release URL. You can create your own custom build types for more complex scenarios as well.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
Thanks a lot for this! I realized after reading your answer I was just Googling the wrong thing – Jamaal Feb 28 '15 at 23:51
-
https://stackoverflow.com/questions/51499455/promotion-of-app-from-beta-to-production-on-google-play-working-with-build-varia @CommonnsWare can you please tell us how can i achieve this. 1 apk move from beta to prod but pointing to diff URL. – Mahesh Sep 28 '19 at 18:18
-
@Mahesh: I personally do not use Play Store's beta system, and so I do not have any direct experience with it. – CommonsWare Sep 28 '19 at 18:36