android app/business-logic config

2019-06-04 03:26发布

问题:

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.

回答1:

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