React Native Pitfalls
Troubleshooting methods for script loading failures, app connection errors, and slow Gradle downloads in a React Native Android environment.
Updated
Installing React Native
Whenever something goes wrong, first run cd android && ./gradlew clean to clear the cache, then rerun dev; this solves 90% of problems.
Unable to load script. Make sure you are either running Metro (run 'react-native start') or that your bundle 'index.js' is packaged correctly for release.
Possible cause:
Some dependency information could not be read, such as JDK, Android SDK, Android Studio, and so on.
Or some dependency versions are too high; for example, the JDK needs to be around version 20. You can check with npx react-native doctor, and for missing dependencies you can press f to attempt automatic repair. If it is a JDK version issue, reinstall it and update the environment variables.
If it still reports errors after the fix, restart your computer first; some changes may only take effect after a restart, and the terminal may not have picked up the relevant information.
If the problem persists, you can try running the following command to build the assets yourself. If it reports an error, the directory may lack permissions (this generally does not happen on Windows; it is common on Linux and Mac).
npx react-native bundle --platform android --dev true --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res --verbose
No apps connected. Sending "reload" to all React Native apps failed. Make sure your app is running in the simulator or on a phone connected via USB.
This may be a gradle cache issue; you only need to clean it with cd android && ./gradlew clean.
If you first installed Android Studio and created a virtual device, and then npx react-native doctor's automatic repair installed another Android Studio, you need to delete the original virtual device, open the newly installed lower-version Android Studio, and create a new virtual device.
Slow gradle downloads
You can modify android/gradle.properties and add proxy settings to speed up downloads.
# Set up a proxy; change the IP and port to those of your local proxy
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=7890
systemProp.https.proxyHost=127.0.0.1
systemProp.https.proxyPort=7890
If you don't have a usable acceleration proxy, you can switch the gradle repositories to domestic mirrors by modifying android/build.gradle.
buildscript {
repositories {
// Remove Google's repository
// google()
// Add Aliyun mirrors
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
// Keep this as is
mavenCentral()
}
}