Feb 21, 2010

Reading Properties Files on Android (Investigation for microlog4android)

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:
  1. Using the AssetManager
  2. 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:
  1. You are not limited to name your file with lower case letters only.
  2. 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?

10 comments:

Anonymous said...

I found this while Googling for java Properties and Android. Very useful, thank you.

My Open Source Software Development Blog said...

Glad that you liked it and found it useful.

Anonymous said...

Helped me a lot, thanks. But is there a possibility to change values in the property file. I can not get an output stream ...

My Open Source Software Development Blog said...

I have not tested to write to a property file, but I guess that it is not possible to do. If you need to write data to a file, you could store a file in the application private directory. Or you could use the preference classes to store preferences.

Kiran said...

I am still not sure where to drop the properties file. Tried keeping it under /src and /assets. But looks like neither of the files are picked up.(to test, i configured FileAppender but found the following in logCat


11-11 20:46:32.804: WARN/Microlog.Logger(269): Warning! No appender is set, using LogCatAppender with PatternFormatter

john said...

Hi, many thanks for this, simple, effective and very useful. Saved me a lot of time.

My Open Source Software Development Blog said...

Thanks! Glad that you liked my article.

My Open Source Software Development Blog said...

Kiran; have you tried the /raw directory? Have you checked that the properties file is really included in the build? What is the name of the file?

Roger Kind Kristiansen said...

Useful post. Thanks!

My Open Source Software Development Blog said...

Thanks! Nice that you appreciate the article.