Android Studio中的构建配置出错

use*_*245 6 android gradle android-studio build.gradle

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

strings.xml中

<resources>
  <string name="google_nearby_api_key">XXXXXXXXXXXXXXXXXXXXXXXX</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

清单文件

<application
    <meta-data
        android:name="com.google.android.nearby.messages.API_KEY"
        android:value="@string/google_nearby_api_key"/>
</application>
Run Code Online (Sandbox Code Playgroud)

的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")
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Error:(35, 0) Build Config field cannot have a null parameter
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

jay*_*i93 4

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

本地属性

GOOGLE_NEARBY_API_KEY="XXXXXXXXXX"
Run Code Online (Sandbox Code Playgroud)

构建.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')
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

因此,您的local.properties.

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