You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 00:07:08