(Android-Noob)
I can’t find a best approach for encapsulating configuration resources.
Basically I’d like to keep configuration values like these:
<string name="environment">dev</string>
<integer name="lookup_timeout">750</integer>
<boolean name="use_cache">true</boolean>
- all in one place,
- in a single xml-file
- accessible from Java through some kind of unified method (
R.config.environment)
Anything I’m missing?
Update: What are configuration resources?
A set of values that typically is environment specific and will most probably change / being fine-tuned by the developer during application development. It is certainly not user-modifiable, and though has nothing to do with user-preferences.
Typical examples are:
- the general log level
- the place where logs are written to
- the urls of servers (development / integration / production)
C’mon, you must know what I mean 😉
Your XML is almost verbatim what you can put in a values resource file. In your project’s
res/values/directory, create aconfiguration_resource_that_is_environment_specific.xmlfile (or whatever you wish to call it — the name does not matter). In there, you can have string, integer, and boolean resources. You would retrieve those at runtime via aResourcesobject, which you can get via a call togetResources()from anyContext, such as yourActivity.You can also then override those resource values via other resource sets (e.g.,
res/values-v11/for versions of those values that should be different for API Level 11+).That being said, I think most people just store this stuff in static data members in Java for simplicity.
Not only is “configuration resource” unclear, but so is “environment specific”, since we do not know what you consider your “environment” to be.