首页 > 解决方案 > 如何为 build.gradle 增值?

问题描述

有没有办法处理共享偏好?或者,

向 build.gradle 添加数据库列值?

buildConfigField "String", "secretKey", "\"here should be the value i want\""

标签: androidandroid-studiobuild.gradlesharedpreferences

解决方案


在根目录创建一个文件,例如: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;

推荐阅读