What is the best way and how to set up a config file for a application?
I want the application to be able to look into a text file on the sd card and pick out certain information that it requires ?
What is the best way and how to set up a config file for a application?
I want the application to be able to look into a text file on the sd card and pick out certain information that it requires ?
If you want to store the preferences of your application, Android provides SharedPreferences for this.
Here is the link to official training resource.
If your application is going to be released to the public and if you have sensitive data in your config such as API keys or passwords I would suggest to use secure-preferences instead of SharedPreferences since ultimately SharedPreferences are stored in an XML in clear text and on a rooted phone it is very easy for an application to access another's shared preferences.
I would suggest a few of other methods:
Method 1: Use a *.properties file with Properties
Pros:
Cons:
First, create a config file:
res/raw/config.properties
and add some values:You can then easily access the values with something like this:
Usage:
Of course this could be optimized to read the config file once and get all values.
Method 2: Use AndroidManifest.xml meta-data element:
Personally I've never used this method because it doesn't seem very flexible.
In your
AndroidManifest.xml
add something like:Now a function to retrieve the values:
Usage:
Method 3: Use
buildConfigField
in your Flavor:I didn't find this in the official Android documentation/training but this blog article is very useful.
Basically setting up a project Flavor (for example
prod
) and then in your app'sbuild.gradle
have something like:Usage:
You can achieve this using shared preferences
There is a very detailed guide on how to use Shared Preferences on the Google Android page https://developer.android.com/guide/topics/data/data-storage.html#pref