1

how does an app developer store app-specific (ie. my server addresses, path endpoints, etc.) global and build variant specific config values in an android app? further, is it possible to have overrides? ie. i want to set a default value in my global.config, but collisions in staging-build-variant.config and prod-build-variant.config should override this, while dev-build-variant.config would simply use the default value.

i've read about SharedPreferences but this seems for storing user input at runtime for later runs, and i've read about people using a class to hold constants, but that doesn't quite fit either as i don't get any benefit of a specific configuration values overriding common ones. there is also a lot of seemingly outdated articles out there which i'm not sure are accurate anymore.

i asked this as a comment in this question which seems to be close to what i'm looking for, but thought i'd ask as a question for more exposure.

EDIT: maybe i explained this poorly - to clarify, this comment.

Community
  • 1
  • 1
Justin
  • 2,692
  • 2
  • 18
  • 24

1 Answers1

0

Justin, it seems that what you whant is a library project. You will have to create a library project with one default configuration. Let's say that project is called Core and then it has the following strings.xml:

<string name="server_address">htt://path.to.server</string>

This way you can create a module (called app1) for the project that uses the 'Core' library project. Then you will end with two strings.xml file. So the strings.xml file from your app module will override the strings.xml from the library project. Then you can have:

<string name="server_address">htt://app1.path.to.server</string>

Resources: Create a library Project

[UPDATED]

Instead of use a library you can try a grade variable. See this question: Gradle Variables And this link about build variants: Build variants

Community
  • 1
  • 1
ikkarion
  • 178
  • 4
  • 17
  • this seems unnecessarily complicated. to clarify, an example of a build variant specific config value: api host address. in the production build variant (that i distribute to users, perhaps in something called `production_config.yaml`) i'd want to use `api_host: https://api.mysite.com`. in a dev build variant (that i develop on and build locally, perhaps in `dev_config.yaml`) i might want to use `api_host: http://localhost:8080`. example of a global config value (perhaps in `config.yaml`): the path to a user resource (`/users`). – Justin Jun 14 '16 at 00:21
  • so in code, doing `Config.get('api_host') + Config.get('users_path')` would return `https://api.mysite.com/users` in a production build and `http://localhost:8080/users` on a dev build. – Justin Jun 14 '16 at 00:24
  • Justin, I found a better way by using Gradle Variables. Check the updated answer. – ikkarion Jun 14 '16 at 01:14