android 如何为build.gradle增值?

jecbmhm3  于 2023-01-03  发布在  Android
关注(0)|答案(2)|浏览(165)

有什么办法可以解决共同偏好吗?或者,
添加数据库列值以构建.gradle?

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

1szpjjfi1#

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

nkoocmlb2#

1.在gradle.properties中写入代码

YOUR_ACCESS_PARAMS = "YOUR_VALUE"

1.将代码写入应用程序级别build.gradle

android{
    ...
     buildTypes.each{
        it.buildConfigField 'String', 'ACCESS_PARAMS', YOUR_ACCESS_PARAMS
    }
}

1.生成的BuildConfig.java如下:

public final class BuildConfig {  
    public static final boolean DEBUG = Boolean.parseBoolean("true");   
    public static final String APPLICATION_ID = "xxx.xxxxxxxx.xxx";   
    public static final String BUILD_TYPE = "debug";  
    public static final String FLAVOR = "";  
    public static final int VERSION_CODE = 1;  
    public static final String VERSION_NAME = "1.0.0";  
    // Fields from build type: debug  
    public static final String ACCESS_PARAMS = "YOUR_VALUE";  
}

相关问题