Expo React Native APK安卓真机Mapview启动闪退问题求助
Expo React Native MapView 独立APK启动闪退问题
问题描述
- 基于Expo开发的简易MapView应用,安卓真机上通过Expo GO测试完全正常,但用EAS构建独立APK安装后,启动即闪退(后台仍能看到应用进程)
- 纯HelloWorld测试应用打包后可正常运行,确定问题出在MapView组件上
- 已尝试添加
provider={PROVIDER_GOOGLE}参数(官方文档说明安卓无需此参数),但闪退问题未解决
构建命令
使用以下命令构建APK:
eas build -p android --profile preview
相关代码与配置
App.js
import React from 'react'; import { View, StyleSheet } from 'react-native'; import MapView, { PROVIDER_GOOGLE } from 'react-native-maps'; export default function App() { return ( <View style={styles.container}> <MapView provider={PROVIDER_GOOGLE} style={styles.map} initialRegion={{ latitude: 37.78825, longitude: -122.4324, latitudeDelta: 0.0922, longitudeDelta: 0.0421, }} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, map: { width: '100%', height: '100%', }, });
app.json
{ "expo": { "name": "SimpleMap", "slug": "SimpleMap", "version": "1.0.1", "orientation": "portrait", "icon": "./assets/icon.png", "userInterfaceStyle": "light", "splash": { "image": "./assets/splash.png", "resizeMode": "contain", "backgroundColor": "#ffffff" }, "assetBundlePatterns": [ "**/*" ], "ios": { "supportsTablet": true }, "android": { "adaptiveIcon": { "foregroundImage": "./assets/adaptive-icon.png", "backgroundColor": "#ffffff" }, "package": "com.expo_andrej.SimpleMap" }, "web": { "favicon": "./assets/favicon.png" }, "extra": { "eas": { "projectId": "8360acb8-6326-4690-bb67-18cd0ad6e9dc" } } } }
eas.json
{ "cli": { "version": ">= 7.1.2" }, "build": { "development": { "developmentClient": true, "distribution": "internal" }, "preview": { "distribution": "internal", "android": { "buildType": "apk" } }, "production": {} }, "submit": { "production": {} } }
解决方法
配置Google Maps API密钥
Expo GO内置了测试用API密钥,但独立打包必须使用自己的密钥。在app.json的android字段下添加配置:"android": { // 原有配置... "config": { "googleMaps": { "apiKey": "你的Google Maps API密钥" } } }需先在Google Cloud控制台启用Maps SDK for Android,并生成对应API密钥(建议限制应用包名,提升安全性)。
移除Provider参数
安卓平台默认使用Google Maps,无需手动指定provider={PROVIDER_GOOGLE},删除该参数后重新打包测试,避免潜在冲突。添加必要权限
在app.json的android字段中添加权限配置:"android": { // 原有配置... "permissions": [ "ACCESS_COARSE_LOCATION", "ACCESS_FINE_LOCATION" ] }部分设备可能因权限缺失导致地图组件初始化失败。
查看崩溃日志定位根源
用adb命令抓取崩溃日志,直接查看闪退的具体原因:adb logcat *:E日志会明确显示错误类型,比如API密钥无效、依赖库缺失等。
内容的提问来源于stack exchange,提问作者andrew
相关产品推荐
相关产品推荐

