安卓配置文件
最好的方法是什么以及如何为应用程序设置配置文件?
我希望应用程序能够查看 SD 卡上的文本文件并挑选出它需要的某些信息。
What is the best way and how do I set up a configuration 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的应用程序要向公众发布,并且您的配置中有敏感数据,例如 API 密钥或密码,我建议使用 安全首选项 而不是 SharedPreferences 因为 SharedPreferences 最终以明文形式存储在 XML 中,并且在 root 后的手机上,应用程序可以很容易地访问另一个应用程序的共享首选项。
我建议其他一些方法:
*方法 1:使用一个 .properties 文件,其中包含属性
优点:
缺点:
首先,创建一个配置文件:
res/raw/config.properties
并添加一些值:然后您可以使用如下方式轻松访问这些值:
用法:
当然,这个可以优化为读取一次配置文件并获取所有值。
方法2:使用AndroidManifest.xml 元数据 element:
就我个人而言,我从来没有使用过这种方法,因为它看起来不太灵活。
在您的
AndroidManifest.xml
中,添加如下内容:现在是一个检索值的函数:
用法:
方法 3:在 风味:
我在官方Android文档/培训中没有找到这个,但是这篇博客文章非常有用。
基本上设置一个项目 Flavor(例如
prod
),然后在应用程序的build.gradle
中包含类似以下内容:
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 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:
您可以使用 共享首选项 来实现此目的,
有一个非常详细的指南如何在 Google Android 页面上使用共享首选项
https://developer.android.com/guide/topics/数据/data-storage.html#pref
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
如果你想存储应用程序的首选项,Android 提供了 SharedPreferences为此。
这是官方培训资源的链接。
If you want to store the preferences of your application, Android provides SharedPreferences for this.
Here is the link to official training resource.
我最近遇到了这样的要求,在这里记下我是如何做到的。
要求:
config.txt 文件的内容是,
创建一个实用程序类 Config.java,用于读写文本文件。
创建另一个实用程序类来写入应用程序首次执行的配置文件,
注意:应用程序必须设置SD卡读/写权限。
在您的主要活动中,onCreate()
I met such requirement recently, noting down here, how I did it.
Requirement:
The config.txt file contents are,
Create a utility class Config.java, for reading and writing text file.
Create another utility class to write the configuration file for the first time execution of the application,
Note: SD card read/write permission has to be set for the application.
At your Main Activity, onCreate()