Expo托管工作流构建中Google Maps API密钥丢失问题求助
Expo托管工作流中Android生产构建忽略Google Maps API密钥的问题及解决
我正在开发跨iOS和Android的Expo托管工作流应用,不管是本地构建还是通过EAS服务器执行生产构建,app.config.ts中Android配置里的Google Maps API密钥都会被忽略,最终导致应用运行时抛出API密钥未找到的错误。
配置与构建情况
app.config.ts中的Android配置
android: { adaptiveIcon: { foregroundImage: "./assets/icons/adaptive-icon.png", monochromeImage: "./assets/icons/adaptive-icon.png", backgroundImage: "./assets/icons/adaptive-icon-bg.png", }, config: { googleMaps: { apiKey: "MY_API_KEY", }, }, package: "mypackage", googleServicesFile: "./certificates/production/google-services.json", permissions: ["ACCESS_FINE_LOCATION", "com.android.vending.BILLING"], newArchEnabled: false, softwareKeyboardLayoutMode: "pan", },
本地构建命令
eas build --platform android --local --build-logger-level=trace
问题现象
从构建日志的[READ_APP_CONFIG]片段可以看到,config.googleMaps.apiKey已从配置中消失:
"android": { "adaptiveIcon": { "foregroundImage": "./assets/icons/adaptive-icon.png", "monochromeImage": "./assets/icons/adaptive-icon.png", "backgroundImage": "./assets/icons/adaptive-icon-bg.png" }, "package": "mypackage", "googleServicesFile": "./certificates/production/google-services.json", "permissions": [ "ACCESS_FINE_LOCATION", "com.android.vending.BILLING", "android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION" ], "newArchEnabled": false, "softwareKeyboardLayoutMode": "pan" }
应用运行时抛出错误:
API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
解决步骤
1. 检查依赖与SDK版本
- 确认已安装
expo-google-maps依赖:npx expo install expo-google-maps - 验证当前Expo SDK版本是否支持
android.config.googleMaps.apiKey配置,部分旧版本可能需要调整配置方式。
2. 排查google-services.json的影响
如果配置了googleServicesFile,检查该文件中是否包含Google Maps API密钥的相关配置。Expo会优先读取google-services.json中的密钥,可能覆盖app.config.ts的设置。可以临时移除googleServicesFile配置,重新构建验证是否能读取到app.config.ts中的密钥。
3. 用环境变量注入密钥
避免硬编码密钥,改用环境变量注入:
- 在项目根目录创建
.env文件:EXPO_PUBLIC_GOOGLE_MAPS_API_KEY=你的实际API密钥 - 修改
app.config.ts读取环境变量:import { ConfigContext } from '@expo/config'; export default ({ config }: ConfigContext) => ({ ...config, android: { ...config.android, config: { googleMaps: { apiKey: process.env.EXPO_PUBLIC_GOOGLE_MAPS_API_KEY, }, }, // 其他Android配置项 }, }); - EAS构建需在控制台对应项目中配置相同环境变量。
4. 通过自定义插件手动配置AndroidManifest.xml
如果上述方法无效,可编写Expo插件手动添加meta-data标签:
- 创建
withGoogleMapsApiKey.ts插件文件:import { AndroidConfig, ConfigPlugin, withAndroidManifest } from '@expo/config-plugins'; const withGoogleMapsApiKey: ConfigPlugin<string> = (config, apiKey) => { return withAndroidManifest(config, async (config) => { const mainApplication = AndroidConfig.Manifest.getMainApplicationOrThrow(config.modResults); // 查找并替换或添加meta-data const existingMetaIndex = mainApplication.metaData?.findIndex( (item) => item.$['android:name'] === 'com.google.android.geo.API_KEY' ); if (existingMetaIndex !== undefined && existingMetaIndex >= 0) { mainApplication.metaData![existingMetaIndex] = { $: { 'android:name': 'com.google.android.geo.API_KEY', 'android:value': apiKey, }, }; } else { mainApplication.metaData = [ ...(mainApplication.metaData || []), { $: { 'android:name': 'com.google.android.geo.API_KEY', 'android:value': apiKey, }, }, ]; } return config; }); }; export default withGoogleMapsApiKey; - 在
app.config.ts中引入插件:import withGoogleMapsApiKey from './withGoogleMapsApiKey'; export default { // 其他全局配置 plugins: [ [withGoogleMapsApiKey, process.env.EXPO_PUBLIC_GOOGLE_MAPS_API_KEY], // 其他已配置插件 ], };
内容的提问来源于stack exchange,提问作者Stefano Ortisi
相关产品推荐
相关产品推荐

