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

React Native运行Android项目报错:Activity Class不存在问题求助

Fixing "Activity class does not exist" Error in React Native run-android

Hey there, let's work through this frustrating issue you're dealing with. It's weird that your APK builds and installs fine but react-native run-android throws that missing Activity error—let's break down the fixes step by step.

1. Find Your Actual Entry Activity First

The root problem here is that react-native run-android looks for MainActivity by default, but your project uses a different entry Activity (or it's been renamed/moved). Here's how to locate it:

  • Open android/app/src/main/AndroidManifest.xml
  • Look for the <activity> tag that includes these two intent filters:
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    
  • The android:name attribute here is your real entry Activity (e.g., .SplashActivity, .CustomMainActivity).

Once you have that, run the command with the --main-activity flag pointing to this Activity:

npx react-native run-android --main-activity .SplashActivity

(Replace .SplashActivity with your actual Activity name—keep the dot if it's in the same package.)

2. Verify Your applicationId Matches

Even if you specify --appId, if it doesn't match what's in your build config, the command will still fail:

  • Open android/app/build.gradle
  • Check the defaultConfig block for applicationId—it should look like this:
    defaultConfig {
        applicationId "com.ooo.ooo" // This must match the --appId you're using
        // ... other configs
    }
    

Make sure the --appId value you pass exactly matches this applicationId.

3. Clear Caches to Fix Stale Configs

Sometimes cached build artifacts can cause mismatched references. Run these commands to clean everything up:

# Clean Android build cache
cd android && ./gradlew clean
cd ..

# Reset React Native packager cache
npx react-native start --reset-cache

After this, try running react-native run-android again (with the correct --main-activity and --appId if needed).

4. Recreate MainActivity If It's Actually Missing

If your project truly has no entry Activity file (unlikely since the APK builds), you can create a default one:

  • For Kotlin projects, create android/app/src/main/java/com/xxx/xxx/MainActivity.kt with this code:
    package com.xxx.xxx
    
    import com.facebook.react.ReactActivity
    import com.facebook.react.ReactActivityDelegate
    import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
    import com.facebook.react.defaults.DefaultReactActivityDelegate
    
    class MainActivity : ReactActivity() {
      override fun getMainComponentName(): String {
        return "YourAppName" // Replace with your app's registered component name from index.js
      }
    
      override fun createReactActivityDelegate(): ReactActivityDelegate {
        return DefaultReactActivityDelegate(
          this,
          mainComponentName,
          fabricEnabled
        )
      }
    }
    
  • Update your AndroidManifest.xml to use this MainActivity as the entry, then rebuild.

内容的提问来源于stack exchange,提问作者Joe199382

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 23:07:31