Today I have investigated the possibilities to read properties files on
Android. In
microlog4android there is no configuration possibilities via file as it is today. The microlog4android development team feels that we need to re-write the configuration from scratch. There are many reasons for this decision, for example there is the Properties class available on Android while in Java ME we use
microproperties. These are my findings about properties files on Android.
My first thought was to use the
SharedPreferences class. My brain picked up this idea while using the SharedPreferences in another project. The SharedPreferences is used to read/write preferences for an Android application. The preferences are stored in an xml file that is put in the private directory structure of an application. You could edit the file by pulling it from the device or emulator using
adb pull
. After editing it you push it back using the
adb push
command. If you are using the Android Eclipse plugin you could use the file browser to do the same trick. But this is cumbersome to pull/edit/push the file and I do not really like to use XML for storing properties.
But how do we do it in microlog? The properties are stored in a properties file that is put in the JAR or by setting properties in the JAD file. Both are easily edited in Eclipse without no need to pull/push the file. The same goes for NetBeans. The properties file is bundled with the jar. Why not do the same on Android? Time to Google again! I found out that there was primarily two ways of doing this:
- Using the AssetManager
- Reading a raw resource
To test this I created a simple Android project in Eclipse and copied two microlog properties files. One was put in the
/assets
directory while the other was put in the
/res/raw
directory. The first thing that happened was that Eclipse complained about the naming of
micrologV2.properties
that was put in the
/res/raw
directory. It was kind enough to inform me that only [0..9][a..z] was allowed. Changing the capital V to a lower case v was simple. Now time to do some coding. The code for reading using the AssetManager looked like this:
Resources resources = this.getResources();
AssetManager assetManager = resources.getAssets();
// Read from the /assets directory
try {
InputStream inputStream = assetManager.open("microlog.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println("The properties are now loaded");
System.out.println("properties: " + properties);
} catch (IOException e) {
System.err.println("Failed to open microlog property file");
e.printStackTrace();
}
The code is dead simple and thank God for the Properties class, although microproperties would do the trick. The second way to do it is as simple as the first approach. The code looks like this:
// Read from the /res/raw directory
try {
InputStream rawResource = resources.openRawResource(R.raw.micrologv2);
Properties properties = new Properties();
properties.load(rawResource);
System.out.println("The properties are now loaded");
System.out.println("properties: " + properties);
} catch (NotFoundException e) {
System.err.println("Did not find raw resource: "+e);
} catch (IOException e) {
System.err.println("Failed to open microlog property file");
}
Notice that I omitted the code for getting the resources, since this was part of the first example. Both ways seems to be good. But what to choose? Using approach 1) has the following advantages from a microlog4android perspective:
- You are not limited to name your file with lower case letters only.
- It is possible to use a default file name if the user does not specify one.
But I probably will implement both solutions, since this gives the user a freedom to choose where to put the file. What do you think? Is there any other solutions that I have missed out?