React Native中yarn android报Activity不存在,gradlew运行正常问题
React Native run-android 启动失败问题排查
问题背景
我有一个React Native应用,通过yarn运行两个脚本:
- 脚本
"and": "cd android && ./gradlew installDevDebug"可成功安装应用,但需手动从模拟器打开 - 脚本
"android": "react-native run-android --mode=DevDebug"本该安装并启动带Metro构建的应用,却出现报错
build.gradle 配置的 productFlavors
flavorDimensions ("environment") productFlavors { dev { dimension "environment" applicationIdSuffix ".dev" } sandbox { dimension "environment" applicationIdSuffix ".sandbox" } staging { dimension "environment" applicationIdSuffix ".staging" } prod prod { dimension "environment" } }
报错信息
info Connecting to the development server... info Starting the app on "emulator-5554"... Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.app/.MainActivity } Error type 3 Error: Activity class {com.example.app/com.example.app.MainActivity} does not exist.
问题原因
使用dev flavor时,它的applicationIdSuffix为.dev,实际安装的应用包名是com.example.app.dev,但react-native run-android --mode=DevDebug命令默认尝试启动原始包名com.example.app下的MainActivity,两者不匹配导致找不到对应Activity。
解决方案
方案1:指定正确的应用包名
在命令中添加--appId参数指定实际包名:
react-native run-android --mode=DevDebug --appId com.example.app.dev
方案2:直接指定flavor和variant
通过--flavor和--variant参数明确指定构建变体:
react-native run-android --flavor dev --variant DevDebug
方案3:修改package.json脚本
将package.json中的android脚本修改为:
"android": "react-native run-android --flavor dev --variant DevDebug"
之后运行yarn android即可正常启动应用。
内容的提问来源于stack exchange,提问作者Eco
相关产品推荐
相关产品推荐

