android - 如何为 build.gradle 增值?
问题描述
有没有办法处理共享偏好?或者,
向 build.gradle 添加数据库列值?
buildConfigField "String", "secretKey", "\"here should be the value i want\""
解决方案
在根目录创建一个文件,例如:apikey.properties
SECRET_KEY=YOUR_SECRET_KEY
在你的 build.gradle 中读取 apikey.properties
def apikeyPropertiesFile = rootProject.file("apikey.properties")
def apikeyProperties = new Properties()
apikeyProperties.load(new FileInputStream(apikeyPropertiesFile))
android {
defaultConfig {
buildConfigField("String", "SECRET_KEY", apikeyProperties['SECRET_KEY'])
}
}
您可以使用BuildConfig
对象访问您的字段。
String secretKey = BuildConfig.SECRET_KEY;
推荐阅读
- sql-server - 如何将两个模式导出到 Sql Server 中的同一个文件?
- json - 问:如何指定此模式要满足的条件?
- javascript - React:usePrevious 钩子仅在组件中有 1 个状态时才有效。如果有多个状态,如何使其工作?
- python - 熊猫分组但保留原始索引和重复的聚合值
- c# - ASP NET Core Web API 禁用 TLS 1.0 和 TLS 1.1 支持
- caching - 根据时间戳条件删除 Caffeine 条目
- asp.net-core - 查询在 Azure 上超时,但不是来自同一数据库上的本地主机
- ios - 关闭应用程序时,如何将我的新文本输入到 UITextfield 中进行存储?
- vue.js - Vuex 调度会产生大量开销
- python - discord.ext.commands'没有属性'change_presence' Discord.py