React Native安卓模拟器启动应用报错原因咨询
Hey there! I’ve tackled countless react-native run-android errors in Windows CMD over the years, so let’s walk through the most common culprits and how to fix them:
This is by far the most frequent source of headaches. If CMD can’t locate your Android tools, the command will fail before it even gets started.
Missing or misconfigured
ANDROID_HOMEvariable
The system needs to know where your Android SDK lives. Here’s how to fix it:- Open System Properties → Advanced → Environment Variables
- Create a new system variable named
ANDROID_HOME, set its value to your SDK path (usuallyC:\Users\[YourUsername]\AppData\Local\Android\Sdk) - Add
%ANDROID_HOME%\platform-toolsand%ANDROID_HOME%\toolsto your systemPathvariable - Restart CMD and run
adb versionto confirm it works
Missing Android SDK components
React Native relies on specific SDK packages. Fire up Android Studio’s SDK Manager and make sure you have these installed:- SDK Platform matching the version specified in your project’s
android/app/build.gradle(e.g., API 33) - Android SDK Build-Tools (stick to the latest stable version, like 33.0.2)
- Android SDK Command-line Tools (always grab the latest update)
- SDK Platform matching the version specified in your project’s
Sometimes the project’s cached files or dependencies get messed up, breaking the build process.
Corrupted node modules or React Native cache
Wipe the old stuff and start fresh with these commands (adjusted for CMD syntax):# Delete node modules and reinstall rd /s /q node_modules && npm install # Reset React Native cache npx react-native start --reset-cache # Clean Gradle cache cd android gradlew.bat clean cd ..Mismatched Gradle versions
Check two key files to ensure compatibility:android/build.gradle: The Gradle plugin version (e.g.,com.android.tools.build:gradle:7.4.2)android/gradle/wrapper/gradle-wrapper.properties: The Gradle distribution version (e.g.,gradle-7.5-all.zip)
These versions need to pair correctly—look up the official compatibility table if you’re unsure.
If there’s no device to deploy to, the command can’t complete successfully.
No connected device or running emulator
Runadb devicesin CMD to see if any devices show up. If using an emulator, launch it via Android Studio’s AVD Manager first. For a physical device, enable USB debugging and install your device’s USB drivers on your PC.Emulator hardware acceleration disabled
Slow or unresponsive emulators often stem from missing hardware acceleration. Edit your emulator in AVD Manager, set the "Graphics" option to "Hardware - GLES 2.0", and make sure virtualization (VT-x/AMD-V) is enabled in your computer’s BIOS.
Gradle needs to fetch a lot of dependencies, and network problems can halt this process.
- If you’re in a region with restricted access, configure a domestic Gradle mirror by adding these lines to your project’s
android/build.gradle:repositories { maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/jcenter' } maven { url 'https://maven.aliyun.com/nexus/content/groups/public' } } - Alternatively, switch to a stable network or manually download missing dependencies and place them in Gradle’s cache directory.
内容的提问来源于stack exchange,提问作者m.f

