Mobile Dev

Fix React Native App Closing When Opening DevTools in Android Emulator

Troubleshoot React Native Android app crashes when accessing DevTools. Solutions for Metro connectivity, debug mode, and emulator configuration issues.

1 answer 1 view

How to fix React Native app closing when trying to open DevTools in Android Emulator?

I have a simple React Native project where everything runs fine, but attempting to open DevTools causes the app to close in the Android Emulator.

Issue Details:

  • Pressing Ctrl + M (or Cmd + M on macOS) in the emulator triggers the app to close.
  • Running the command adb shell input keyevent 82 in the terminal also closes the app.
  • No errors appear in the logs when running npx react-native log-android. The log shows:
    info Starting logkitty
    [11:51:46] I | ReactNativeJS ▶︎ Running "ReactNativeProject" with {"rootTag":1,"initialProps":{},"fabric":true}
    

Project Setup (package.json):

json
{
  "name": "ReactNativeProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "^2.2.0",
    "@react-native/new-app-screen": "0.82.1",
    "@react-navigation/native": "^7.1.25",
    "@react-navigation/native-stack": "^7.8.6",
    "@react-navigation/stack": "^7.6.12",
    "react": "^19.2.3",
    "react-native": "^0.83.1",
    "react-native-gesture-handler": "^2.29.1",
    "react-native-safe-area-context": "^5.5.2",
    "react-native-screens": "^4.18.0"
  },
  "devDependencies": {
    "@babel/core": "^7.25.2",
    "@babel/preset-env": "^7.25.3",
    "@babel/runtime": "^7.25.0",
    "@react-native-community/cli": "20.0.0",
    "@react-native-community/cli-platform-android": "20.0.0",
    "@react-native-community/cli-platform-ios": "20.0.0",
    "@react-native/babel-preset": "0.82.1",
    "@react-native/eslint-config": "0.82.1",
    "@react-native/metro-config": "0.82.1",
    "@react-native/typescript-config": "0.82.1",
    "@types/jest": "^29.5.13",
    "@types/react": "^19.1.1",
    "@types/react-test-renderer": "^19.1.0",
    "eslint": "^8.19.0",
    "jest": "^29.6.3",
    "prettier": "2.8.8",
    "react-test-renderer": "19.1.1",
    "typescript": "^5.8.3"
  },
  "engines": {
    "node": ">=20"
  }
}

What could be causing this issue, and how can I resolve it to open DevTools without the app closing?

When your React Native app closes instead of opening DevTools in the Android Emulator, it’s typically caused by connectivity issues between the Metro bundler and the emulator, or by incorrect debug configurations. This common React Native Android development issue can be resolved by checking your Metro server connection, verifying your emulator settings, and ensuring proper debugging configurations.

Contents

Understanding the Problem

The issue where your React Native app closes when attempting to open DevTools in the Android Emulator is a frustrating but common problem for React Native Android developers. Based on your logs showing “info Starting logkitty” and “Running ‘ReactNativeProject’ with fabric: true,” your app appears to be running in production mode rather than debug mode, which restricts access to developer tools.

This React Native Android development challenge occurs because the DevTools interface requires a debug connection between your development machine and the emulator. When this connection fails or is blocked, the React Native application terminates instead of launching the developer menu. Understanding the root causes is essential for effective React Native debugging.

Common Causes

Several factors can cause your React Native app to close when opening DevTools in the Android Emulator:

1. Metro Bundler Connectivity Issues

The Metro bundler serves your JavaScript code to the emulator. If the Metro server isn’t running or the emulator can’t connect to it, attempting to open DevTools will fail. This is especially common when using the Android Emulator with different network configurations.

2. Production Mode Configuration

If your React Native application is running in production mode, developer tools are disabled by default. The lack of proper React Native DevTools configuration would cause the app to close when attempting to access debugging features.

3. Device Emulator Network Restrictions

The Android Emulator sometimes has network connectivity issues that prevent it from communicating with your local development machine. These Android Emulator networking problems can block the Metro bundler connection needed for React Native debugging.

4. Debug Support Library Issues

Missing or incorrectly configured debug support libraries can cause the React Native application to crash when developer tools are accessed. This is particularly relevant in newer React Native versions where the debug support system has evolved.

Step-by-Step Solutions

Follow these solutions to fix your React Native app closing when opening DevTools:

Solution 1: Restart the Metro Server

  1. Stop any running Metro server instances with Ctrl+C in your terminal
  2. Navigate to your project root directory
  3. Start the Metro server with:
    npx react-native start
    
  4. In a separate terminal, run your app with:
    npx react-native run-android
    
  5. Once the app is running, try opening DevTools again with Ctrl+M or Cmd+M

This approach ensures a fresh Metro bundler connection between your development machine and the Android Emulator.

Solution 2: Use ADB to Open Dev Menu

If keyboard shortcuts don’t work, use the ADB command specifically mentioned in the React Native debugging guide:

adb shell input keyevent 82

This command directly sends the menu key event to the emulator. If this still causes the app to close, proceed to the next solution.

Solution 3: Verify Debug Mode Configuration

Ensure your React Native application is running in debug mode by checking:

  1. Verify you’re not building a release version:
    npx react-native run-android --variant=debug
    
  2. Check your AndroidManifest.xml for:
    xml
    <application
      android:usesCleartextTraffic="true"
      ...>
    
  3. Make sure your MainActivity.java or MainActivity.kt has debugging enabled:
    java
    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
    

Solution 4: Fix Metro Server Connection

If the Android Emulator can’t connect to the Metro server:

  1. Get your development machine’s IP address:
    ipconfig getifaddr en0  # For macOS
    ip addr show            # For Linux
    
  2. Configure Metro to use this IP:
    npx react-native start --host <your-ip-address>
    
  3. In your Android emulator, open the browser and navigate to <your-ip-address>:8081 to verify connectivity

Solution 5: Clear Metro Cache

Sometimes Metro’s cache causes connection issues:

  1. Stop the Metro server
  2. Delete the metro cache:
    rm -rf node_modules/.cache/metro*
    
  3. Also clear the Android emulator cache:
    adb shell pm clear com.your.package.name
    
  4. Restart the Metro server and run your app again

Advanced Troubleshooting

If the basic solutions don’t work, try these advanced troubleshooting steps:

1. Check ProGuard Rules

For React Native applications using ProGuard (common in release builds), add these rules to your android/app/proguard-rules.pro file:

-keep class com.facebook.react.devsupport.** { *; }
-keep class com.facebook.react.** { *; }
-keep class javax.** { *; }
-keep class * extends javax.inject.Inject { *; }

This prevents ProGuard from obfuscating the debug support classes needed for React Native DevTools, as mentioned in the official React Native GitHub issue.

2. Verify React Native Version Compatibility

Ensure your React Native version (0.83.1) is compatible with all your dependencies. Check for known issues with your specific version by reviewing the React Native release notes and GitHub issues.

3. Test on a Physical Device

If possible, test your app on a physical Android device to determine if the issue is specific to the Android Emulator. Connect a device via USB and run:

adb devices
npx react-native run-android

4. Check for Conflicting Extensions

Some Android Studio extensions or other development tools can interfere with React Native debugging. Try running Android Studio in safe mode or temporarily disabling extensions.

5. Reinstall Metro Dependencies

Sometimes corrupted metro dependencies can cause issues:

rm -rf node_modules
npm install

Then restart your development environment and try again.

Preventive Measures

To avoid future issues with React Native DevTools in the Android Emulator:

1. Regular Metro Server Restarts

Make it a habit to restart the Metro server after making significant changes to your project configuration. This prevents connection issues from accumulating over time.

2. Network Configuration Best Practices

When working with the Android Emulator, ensure stable network connectivity:

  • Use a reliable Wi-Fi connection or Ethernet
  • Configure your firewall to allow connections on port 8081
  • Consider using a static IP address for your development machine

3. Keep Dependencies Updated

Regularly update your React Native and related dependencies to benefit from bug fixes and improvements:

npm update react-native
npm update @react-native-community/cli

4. Monitor Log Output

Always monitor the React Native log output while developing:

npx react-native log-android

This helps catch issues early before they escalate to app crashes.

Additional Resources

For further troubleshooting and React Native Android development guidance:

  1. React Native Debugging Guide - Comprehensive debugging techniques and best practices
  2. Metro Bundler Connectivity Issues - Specific solutions for Metro bundler connection problems
  3. React Native DevTools Setup - Detailed configuration for developer tools
  4. Running React Native on Android - Setup and troubleshooting guide for Android development
  5. Reddit React Native Community - Community solutions and discussions

Sources

Conclusion

The React Native app closing when opening DevTools in the Android Emulator is typically caused by connectivity issues, incorrect debug configurations, or production mode settings. By following the solutions outlined in this guide—restarting the Metro server, using ADB commands, verifying debug mode, fixing network connections, and clearing cache—you can resolve this React Native Android development issue.

Remember that maintaining stable Metro bundler connections and keeping your React Native environment properly configured is essential for smooth development. The React Native DevTools are powerful debugging assets, and understanding how to troubleshoot access issues will significantly improve your development workflow.

Authors
Verified by moderation
Moderation
Fix React Native App Closing When Opening DevTools in Android Emulator