1

From this question, I know how to obtain Properties within an Android Application.

However, if I want to write an Android Library, and intend to read a properties file (say myconfig.properties, how can I do so?

Given that the Android Library (e.g. it's called "myAndroidLib" ) has android-support-v4.jar , and this library is imported to an Android Application. Also given that myconfig.properties exists.

I found a piece of code in the Internet, but this works only within Android:

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();
}
Community
  • 1
  • 1
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • Since the page that you linked to does not have the word "properties" in it, are you sure that you linked to the correct page? Also, what makes you think that doing this from a library is any different than doing it in an application? – CommonsWare May 28 '13 at 09:53
  • My library acts as 3rd party module that deploys to other applications. Main reason for this is to protect my source code and logic while I provide my logics to other applications. – Raptor May 28 '13 at 09:54

1 Answers1

2

I found a piece of code in the Internet, but this works only within Android

That code will work fine within an Android library project. You will need the hosting application to give you a Context from which you can eventually derive the AssetManager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491