Android Studio中的构建配置出错

问题描述 投票:5回答:1

我在Android Studio中运行应用程序。以下是代码:

strings.xml中

<resources>
  <string name="google_nearby_api_key">XXXXXXXXXXXXXXXXXXXXXXXX</string>
</resources>

清单文件

<application
    <meta-data
        android:name="com.google.android.nearby.messages.API_KEY"
        android:value="@string/google_nearby_api_key"/>
</application>

的build.gradle

def filesAuthorityValue = applicationId + ".files"
manifestPlaceholders = [filesAuthority: filesAuthorityValue]
buildConfigField "String", "FILES_AUTORITY", "\"${filesAuthorityValue}\""
resValue "string", "google_nearby_api_key", getLocalProperties().getProperty("google_nearby_api_key")

我收到错误:

Error:(35, 0) Build Config field cannot have a null parameter

有人可以帮忙吗?

android android-studio gradle build.gradle
1个回答
2
投票

我检查了我的机器,这对我有用

local.properties

GOOGLE_NEARBY_API_KEY="XXXXXXXXXX"

的build.gradle

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
resValue "string", "google_nearby_api_key", properties.getProperty('GOOGLE_NEARBY_API_KEY')

您将在以下位置找到包含字符串的生成文件:

app/build/generated/res/resValues/{build-type}/values/generated.xml

现在,您可以将应用中的字符串用作@string/google_nearby_api_key

此外,当我从local.properties删除密钥时,我收到同样的错误:

Error:(17, 1) A problem occurred evaluating project ':app'.
> Build Config field cannot have a null parameter

所以,你可能没有local.properties中的密钥。

而且您似乎也在strings.xml资源文件中手动添加了字符串,如果要隐藏API密钥,则不应该这样做。通过你的gradle构建注入它(上面的方法)。

© www.soinside.com 2019 - 2024. All rights reserved.