Expo应用打包APK后启动页闪退,但Expo Go开发模式运行正常
Expo应用打包Android预览版后启动页闪退问题排查方案
开发的Expo应用在Expo Go中运行完全正常,但执行
eas build -p android --profile preview打包生成APK安装到手机后,仅显示启动页就自动闪退,开发模式无任何报错。以下是项目的package.json配置:{ "name": "kotinski-remake", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "kotinski-remake", "version": "1.0.0", "dependencies": { "@expo/vector-icons": "^13.0.0", "@react-native-async-storage/async-storage": "1.18.2", "expo": "^49.0.3", "expo-barcode-scanner": "~12.5.3", "expo-checkbox": "^2.5.0", "expo-constants": "~14.4.2", "expo-linear-gradient": "~12.3.0", "expo-linking": "~5.0.2", "expo-location": "~16.1.0", "expo-router": "2.0.0", "expo-splash-screen": "~0.20.5", "expo-status-bar": "~1.6.0", "firebase": "^10.6.0", "moment": "^2.29.4", "nativewind": "^2.0.11", "react": "18.2.0", "react-dom": "18.2.0", "react-native": "0.72.6", "react-native-gesture-handler": "~2.12.0", "react-native-radio-buttons": "^1.0.0", "react-native-reanimated": "~3.3.0", "react-native-safe-area-context": "4.6.3", "react-native-screens": "~3.22.0", "react-native-web": "~0.19.6" }, "devDependencies": { "@babel/core": "^7.19.3", "@babel/plugin-proposal-export-namespace-from": "^7.18.9", "tailwindcss": "3.3.2" } }, }
排查与解决步骤
1. 先抓崩溃日志找根源
- 把手机连电脑,执行
adb logcat *:E命令,过滤出错误日志,看应用崩溃时的具体报错(比如未处理的异常、权限缺失、依赖加载失败等)。 - 也可以在手机「设置-开发者选项-错误报告」里生成应用崩溃报告,查看详细堆栈信息。
2. 检查权限配置是否齐全
项目用到了expo-barcode-scanner和expo-location,必须确保app.json/app.config.js里声明了对应权限:
{ "expo": { "android": { "permissions": [ "CAMERA", "ACCESS_FINE_LOCATION", "ACCESS_COARSE_LOCATION" ] } } }
生产打包时,缺权限很可能导致启动时因权限请求失败崩溃。
3. 验证依赖兼容性
- Expo 49对应React Native 0.72.x,你的依赖整体匹配,但
firebase@10.6.0可能和Expo 49存在兼容问题,试试降级到firebase@9.x版本(Expo官方推荐的稳定版本)测试。 nativewind@2.0.11要确保Tailwind CSS配置正确,且Babel插件设置无误。检查babel.config.js是否包含:
module.exports = function(api) { api.cache(true); return { presets: ['babel-preset-expo'], plugins: [ 'nativewind/babel', 'react-native-reanimated/plugin' ] }; };
注意:react-native-reanimated的插件必须放在Babel配置的最后一项。
4. 检查Expo Router的生产适配
- Expo Router 2.0.0在生产打包时,要确保
_layout.js/_layout.tsx里正确处理启动逻辑,比如是否在useEffect中正确隐藏启动屏:
import { useEffect } from 'react'; import { SplashScreen } from 'expo-splash-screen'; export default function Layout() { useEffect(() => { async function prepare() { try { // 等待所有资源加载完成(比如初始化Firebase、读取本地数据等) } catch (e) { console.warn(e); } finally { await SplashScreen.hideAsync(); } } prepare(); }, []); // ...其他布局代码 }
如果启动屏没正确隐藏,或者初始化逻辑抛出未捕获异常,都会导致闪退。
5. 核对Firebase初始化逻辑
生产环境的Firebase配置可能和开发环境不同,要确保:
- 已在
app.json中配置了Firebase的Android参数(googleServicesFile),或者代码里正确引入了生产环境的配置文件。 - 初始化代码要加错误捕获,避免崩溃扩散:
import { initializeApp } from 'firebase/app'; try { const firebaseConfig = { // 你的生产环境配置 }; initializeApp(firebaseConfig); } catch (error) { console.error('Firebase初始化失败:', error); }
6. 清理缓存重新构建
- 执行
npx expo start --clear清理Expo缓存。 - 删除
node_modules和package-lock.json,重新npm install。 - 用
eas build -p android --profile preview --clear强制清理构建缓存后重新打包。
7. 测试最小化版本
建一个只保留核心依赖(expo、react-native、expo-router)的测试项目,打包看是否正常。如果正常,再逐步添加原项目的依赖和代码,定位到具体崩溃模块。
内容的提问来源于stack exchange,提问作者Ana Apresentação
相关产品推荐
相关产品推荐

