You need to fix the following issues to solve this error First, check whether your device is properly connected to the computer and has USB debugging is enabled. Second, check the development server's(npm start) dependency graph is successfully started and Done.
Try the following to fix the issue : Ensure the packager server is running. Ensure your device/ emulator is connected to your machine and has USB debugging enabled – run 'adb devices' to see a list of connected devices.
In my case the problem was with Android security policies. According to the documentation:
Starting with Android 9.0 (API level 28), cleartext support is disabled by default.
But the application tries to access Metro Bundler through HTTP, right? And that's why it can't. In order to enable cleartext traffic, open your AndroidManifest.xml
and add android:usesCleartextTraffic="true"
to <application>
node.
For example:
<application
android:name="com.example.app"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
You can find more solutions in this SO: https://stackoverflow.com/a/50834600/1673320. OP's problem is different, but similar.
From the Docs: http://facebook.github.io/react-native/docs/running-on-device.html#method-2-connect-via-wi-fi
Method 2: Connect via Wi-Fi
You can also connect to the development server over Wi-Fi. You'll
first need to install the app on your device using a USB cable, but
once that has been done you can debug wirelessly by following these
instructions. You'll need your development machine's current IP
address before proceeding.Open a terminal and type /sbin/ifconfig to find your machine's IP address.
- Make sure your laptop and your phone are on the same Wi-Fi network.
- Open your React Native app on your device.
- You'll see a red screen with an error. This is OK. The following steps will fix that.
- Open the in-app Developer menu.
- Go to Dev Settings → Debug server host for device.
- Type in your machine's IP address and the port of the local dev server (e.g. 10.0.1.1:8081).
- Go back to the Developer menu and select Reload JS.
Run this and it worked for me
adb reverse tcp:8081 tcp:8081
Starting with Android 9.0 (API level 28), cleartext support is disabled by default. we can use android:usesCleartextTraffic="true" this will work but this is not recommended solution. For Permanent and recommended Solution is below:
Step 1 : create a file in android folder
app/src/debug/res/xml/network_security_config.xml
Step 2 : add this to network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!-- deny cleartext traffic for React Native packager ips in release -->
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">10.0.2.2</domain>
<domain includeSubdomains="true">10.0.3.2</domain>
</domain-config>
</network-security-config>
Step 3 : Apply the config to your AndroidManifest.xml
<application
android:networkSecurityConfig="@xml/network_security_config">
</application>
This is applicable to Android 9.0+ according to the Network Security Configuration.
Well, so after trying all possible solutions I found on the web, I decided to investigate the native Android logcat manually. Even after adding android:usesCleartextTraffic="true"
, I found this in the logcat:
06-25 02:32:34.561 32001 32001 E unknown:ReactNative: Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.29.96 not permitted by network security policy
So, I tried to inspect my react-native app's source. I found that in debug
variant, there is already a network-security-config which is defined by react-native guys, that conflicts with the main
variant.
There's an easy solution to this.
Go to <app-src>/android/app/src/debug/res/xml/react_native_config.xml
Add a new line with your own IP address in the like:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>
***<domain includeSubdomains="false">192.168.29.96</domain>***
</domain-config>
</network-security-config>
As my computer's local IP (check from ifconfig
for linux) is 192.168.29.96, I added the above line in ***
Then, you need to clean and rebuild for Android!
cd <app-src>/android
./gradlew clean
cd <app-src>
react-native run-android
I hope this works for you.
Default metro builder runs on port 8081. But in my PC, this port is already occupied by some other process (McAfee Antivirus) So I changed the default running port of metro builder to port number 8088 using the following command
react-native run-android start --port=8088
This resolved my issue and the app works fine now.
PS: You can check whether a port is occupied or not in windows using "Resource Monitor" app. (Under "Listening Ports" in "Network" tab). Check out this link for detailed explanation: https://www.paddingleft.com/2018/05/03/Find-process-listening-port-on-Windows/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With