Expo CLI环境下react-native-maps生产构建崩溃问题求助
解决方案
针对开发环境正常、生产环境崩溃的问题,从API配置、代码、构建流程三个维度排查:
1. 补全Google Cloud API密钥的SHA-1指纹
开发环境使用的是本地调试密钥的SHA-1,但Google Play发布应用时,若启用了Google Play应用签名,API密钥的限制列表必须添加Play控制台中的应用签名证书SHA-1,而非仅上传密钥的SHA-1:
- 进入Google Play控制台 → 你的应用 → 应用签名 → 复制SHA-1指纹
- 打开Google Cloud控制台 → 对应API密钥 → 安卓应用限制 → 添加该SHA-1与包名的组合
2. 验证包名一致性
确保app.json中android.package的值,与Google Cloud API密钥限制的包名、Play控制台的应用ID完全一致,包名大小写敏感,不能有拼写错误。
3. 修复MapView代码中的潜在问题
(1)移除Marker坐标中的冗余属性
Marker的coordinate仅需要latitude和longitude,latitudeDelta、longitudeDelta是MapView区域的属性,无需传递给Marker,会导致无效参数:
<Marker coordinate={{ latitude: currentLocation.location.lat, longitude: currentLocation.location.lng, // 移除以下两行冗余属性 // latitudeDelta: 0.005, // longitudeDelta: 0.005, }} title="current" description={currentLocation.description} identifier="current" > <UserLocationSvg /> </Marker>
(2)添加空值安全判断
生产环境中currentLocation可能因定位权限或网络问题为空,直接访问currentLocation.location会抛出空指针异常,添加可选链判断:
{currentLocation?.location && ( <Marker coordinate={{ latitude: currentLocation.location.lat, longitude: currentLocation.location.lng, }} title="current" description={currentLocation.description} identifier="current" > <UserLocationSvg /> </Marker> )}
4. 清理缓存并重新构建
依赖缓存或构建缓存可能导致配置未正确生效,执行以下命令清理后重新打包:
# 清理Expo缓存 expo r -c # 删除node_modules并重新安装依赖 rm -rf node_modules && npm install # 清理Android构建缓存并重新构建 expo run:android --clear
5. 捕获崩溃日志定位根源
利用已集成的Sentry或Google Play控制台获取生产环境崩溃栈:
- Sentry:查看项目的生产环境事件,定位具体崩溃的代码行与错误类型(如API密钥无效的
SecurityException) - Google Play控制台:进入Android Vitals → 崩溃与ANR,查看详细错误信息
6. 确认Google Maps服务启用状态
再次检查Google Cloud控制台中,是否启用了以下服务:
- Android版Maps SDK
- 地理编码API(若使用了地址解析功能)
- 方向API(若使用了react-native-maps-directions)
内容的提问来源于stack exchange,提问作者karim.belhadjali
相关产品推荐
相关产品推荐

