从这里question ,我知道如何在 Android 应用程序中获取属性。
但是,如果我想编写一个 Android 库,并打算读取一个属性文件(比如 myconfig.properties
,我该怎么做?
鉴于 Android 库(例如它称为“myAndroidLib”)具有 android-support-v4.jar
,并且该库被导入到 Android 应用程序中。还假设 myconfig.properties
存在。
我在网上找到了一段代码,但这只适用于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();
}
最佳答案
I found a piece of code in the Internet, but this works only within Android
该代码将在 Android 库项目中正常运行。您将需要托管应用程序为您提供一个 Context
,您最终可以从中派生出 AssetManager
。
关于android - 使用 Android 库从 Android 应用程序获取属性文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788985/