buildConfigField不恢复从gradle.properties我的API密钥

问题描述 投票:0回答:2

我想是因为我想上传一个公共仓库,以确保我的API密钥,我该试着在我的gradle产出和增加的钥匙gradle.properties文件

的build.gradle

 buildTypes {

        debug {
            buildConfigField 'String', "FIRST_APIKEY", "\"FIRST_APIKEY\""
            buildConfigField 'String', "SECOND_APIKEY", "\"SECOND_APIKEY\""
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

grad了.properties

FIRST_APIKEY = "apikey"
SECOND_APIKEY = "apikey"

它说:apikey是目前的字母数字apikey

当我落实到一类这个来获取apikey它不返回apikey

例如,下面我试着从该apikey分析数据,但我没有得到什么,因为它不能达到

service.getData(BuildConfig.FIRST_APIKEY)....

BuildConfig.FIRST_APIKEY与调试的结果如下

结果= FIRST_APIKEY

而不是FIRST_APIKEY它应该实现我apikey,我以前在gradle.properties文件可配置

感谢您的任何提示

java android gradle android-gradle
2个回答
1
投票

首先,加载性能Properties对象的gradle中的配置阶段:

apply plugin: 'com.android.application'

Properties localProperties = new Properties()
localProperties.load(project.rootProject.file('local.properties').newDataInputStream())

然后你可以找到你的buildConfigField属性:

buildConfigField 'String', "FIRST_APIKEY", localProperties.getProperty('FIRST_APIKEY')
buildConfigField 'String', "SECOND_APIKEY", localProperties.getProperty('SECOND_APIKEY')

如果你的属性看起来像这样:

FIRST_APIKEY=apikey
SECOND_APIKEY=apikey

然后,你需要更换localProperties.getProperty('FIRST_APIKEY')"\"${props.getProperty('FIRST_APIKEY')}\""


1
投票

你不应该分配一个静态的字符串,而是一个变量:

buildConfigField("String", "FIRST_APIKEY", "\"${firstApiKey}\"")
resValue("string", "first_api_key", "${firstApiKey}")

它可能不会在乎任何.properties,除非加载它们;例如:

def apikeys = new Properties()
apikeys.load(new FileInputStream(rootProject.file('apikeys.properties')))
project.ext.set('firstApiKey', apikeys['firstApiKey'])
© www.soinside.com 2019 - 2024. All rights reserved.