You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android项目中Gradle提示googleMapsApiKey不存在的问题

解决Android项目中从local.properties读取密钥到AndroidManifest时的'googleMapsApiKey不存在'警告

核心问题分析

你的代码仅在local.properties中存在MAPS_API_KEY时才会向manifestPlaceholders添加googleMapsApiKey键,若密钥缺失或加载失败,AndroidManifest.xml中的${googleMapsApiKey}占位符就会因找不到对应属性触发警告。以下是修复步骤:


1. 修改App级build.gradle(Groovy)代码

确保manifestPlaceholders始终包含googleMapsApiKey键,即使密钥为空:

defaultConfig {
    applicationId "com.testapp.myfirstapp"
    minSdk 23
    targetSdk 34
    versionCode 8
    versionName "1.0.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    Properties properties = new Properties()
    def localPropertiesFile = project.rootProject.file('local.properties')
    // 先检查文件是否存在,避免加载报错
    if (localPropertiesFile.exists()) {
        properties.load(localPropertiesFile.newDataInputStream())
    }

    // 读取密钥,不存在则设为空字符串
    def mapsApiKey = properties.getProperty('MAPS_API_KEY', '')
    // 确保占位符始终存在
    manifestPlaceholders = [googleMapsApiKey: mapsApiKey]

    // 仍保留缺失提示
    if (mapsApiKey.isEmpty()) {
        println "Warning: MAPS_API_KEY not found in local.properties"
    }
}

2. 验证local.properties的正确性

  • 确认文件位于项目根目录(而非app模块目录)
  • 确保内容无多余空格、引号,格式如下:
    MAPS_API_KEY=APIkeysssd1201232ApiTestKeyStack
    

3. 彻底清除缓存并重新同步

  • 点击Android Studio顶部菜单栏 File -> Invalidate Caches...,选择Invalidate and Restart
  • 或手动删除项目根目录下的.gradle文件夹、app模块下的.gradle和build文件夹,再重新同步项目

4. 确认AndroidManifest.xml引用无误

检查meta-data标签的value属性拼写正确:

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="${googleMapsApiKey}" />

内容的提问来源于stack exchange,提问作者user13926345

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:40:02