156

When I run react-native run-android, it gives me the following error:

Could not connect to development server

Error screen

  • Package server is running and I can access it directly from browser on my mobile device.
  • My Android device is connected to computer and has debugging enabled (I checked using adb devices command).
  • My Android version is 4.4.4 so I cannot use adb reverse command.
  • I have set the ID address and port in Dev setting.
  • USB debug is on.
  • I use Windows 7.

How to fix this error?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
cityvoice
  • 2,409
  • 4
  • 14
  • 25
  • did u look at http://stackoverflow.com/questions/38835931/react-native-adb-reverse-enoent? he run `adb reverse` with android 4.4.2 – Shining Love Star Feb 06 '17 at 09:46
  • 2
    but you might have to go with http://facebook.github.io/react-native/docs/running-on-device.html#method-2-connect-via-wi-fi – Shining Love Star Feb 06 '17 at 09:48
  • Run npm start on local machine to start the development server. I have faced same problem and It worked for me like magic – ashraful May 02 '17 at 11:57
  • Since react native changes so often, would be beneficial to include the react native version as well. Seems like the issue happens on less than 0.59 – Divyanshu Maithani Jul 08 '19 at 07:20
  • http://localhost:19001/debugger-ui is working. wasted lot of time being back of 8081 -- looks like this is updated in later versions. React Native Debugger also keeps looking for 8081, but when you click for a new tab with ctrl-t / cmd-t it suggest 19001 by default. and instead of using chrome developer tools, it seems as a better option – Vikas Putcha Jan 07 '21 at 18:13
  • I was also facing the same issue, for me, I just uninstalled the app from the emulator(Pixel4) & re-run the app again which worked!! Thanks – Wasit Shafi Mar 19 '22 at 20:50

39 Answers39

160

Run this and it worked for me

adb reverse tcp:8081 tcp:8081
Yoosaf Abdulla
  • 3,722
  • 4
  • 31
  • 34
148

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.

itachi
  • 3,389
  • 2
  • 24
  • 29
  • 9
    It's better to apply it only for debug. Reference: https://stackoverflow.com/a/55147267/1489446 – Erick M. Sprengel Mar 26 '19 at 21:37
  • If this doesn't work out, please have a look at https://stackoverflow.com/a/56744301/6740333 as new react native versions have a configuration pre-defined – Neil Agarwal Jun 24 '19 at 21:48
  • @ErickM.Sprengel you mentioned this should be applied only for debug. Can there be any issues if this is applied to the main manifest only for `localhost`? – Divyanshu Maithani Jul 08 '19 at 07:08
  • It'll allow http requests in plain text that are unsafe, if you are aware about it, I think that there is no problem. I'm not sure if some sdks check the NetworkSecurityPolicy.isCleartextTrafficPermitted(), but I think it's not a problem. – Erick M. Sprengel Jul 08 '19 at 23:35
  • This worked for me on Xiaomi redmi note 6 pro latest android update to the date. – Rinor Dreshaj Jul 11 '19 at 13:48
  • I was stumped for a few hours with my new Pixel 4a. I just kept getting the red sceen of death. None of the other solutions worked for me, I added usesCleartextTraffic and it worked. – ooolala Nov 05 '20 at 04:42
103

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.

  1. Make sure your laptop and your phone are on the same Wi-Fi network.
  2. Open your React Native app on your device.
  3. You'll see a red screen with an error. This is OK. The following steps will fix that.
  4. Open the in-app Developer menu.
  5. Go to Dev Settings → Debug server host for device.
  6. Type in your machine's IP address and the port of the local dev server (e.g. 10.0.1.1:8081).
  7. Go back to the Developer menu and select Reload JS.
Shining Love Star
  • 5,734
  • 5
  • 39
  • 49
  • Yes,I use Wi-Fi and the mobile device and computer are on the same WIFI network. – cityvoice Feb 07 '17 at 01:25
  • 1
    did you follow these commands? any chance you can make a quick screencap and upload as a gif file? – Shining Love Star Feb 07 '17 at 06:36
  • ok man I am only trying to help ... can you try starting a fresh project with `react-native init myProject` and see if you can connect? so we can eliminate the possibility of the problem being in your code. – Shining Love Star Feb 07 '17 at 13:07
  • In the future, whenever you are dealing with a new framework / tool, always check fresh installation if you encounter a problem. Will help you find the problem faster AND will save others' people time. good luck mate – Shining Love Star Feb 09 '17 at 07:56
  • Hey, I can't open the developer menu on my physical device. No amount of shaking on my Galaxy S6 seems to be helping. – xerotolerant Jul 28 '17 at 05:58
  • 19
    @xerotolerant -- You can hit `adb shell input keyevent 82` on your command-prompt to open the developer-menu. – Saumik Bhattacharya Sep 07 '17 at 18:42
  • Do you know How can I connect my virtual device and my computer same wifi ? I couldn't select wifi network on virtual device. – Alperen ARICI Jan 22 '20 at 19:23
  • @xerotolerant I know it's old, but for people who have the same problem today, you juste have to shake your phone, and the menu should appear. – Nitneq Feb 06 '20 at 11:10
  • For those who don't know, on Windows you can type `ipconfig` on CMD to find your IPv4 address. That's what you need, plus the port (most likely `:8081`). – romin21 May 09 '20 at 11:57
57

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>
Tarun konda
  • 1,740
  • 1
  • 11
  • 19
  • 1
    This helped me. Many thanks. I only added 'android:networkSecurityConfig' to /src/debug/AndroidManifest.xml. – Hossein Abedi Mar 08 '22 at 18:05
  • A more general solution is to add all "local" networks: `localhost 127.0.0.0 10.0.0.0 172.16.0.0 192.168.0.0` – MikeL Dec 26 '22 at 04:57
22

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.

Neil Agarwal
  • 877
  • 1
  • 9
  • 10
  • 1
    I spent 3 hours today trying to figure out why my iOS handset worked via USB and Android didn't, even though the Android browser could pull the bundle using the IP:port address. This was the solution. Huge help! – Artif3x Jun 22 '20 at 17:24
15

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/

Krishna Sailesh
  • 165
  • 2
  • 8
8

None of the above solutions worked for me.

I just opened Dev menu by clicking Ctrl+M and then clicked on change bundle location and added my machine IP followed by port.

Julia Williams
  • 103
  • 1
  • 5
  • Thank you so much, after going through every possible solution this has done the trick, seems like physical devices want to connect to localhost...downsides of not using emulator I guess. – drUniversalis Dec 05 '20 at 22:17
  • This did the trick for me, if anyone else is stuck! – James Dunay Jun 10 '21 at 17:02
  • Didn't work for me. This is how I previously resolved said issue, but right now it's not sufficient :( (Android 10, RN 63.3 ) – Hatchmaster J Oct 03 '21 at 12:58
6

My solution is modify or make new AndroidManifest.xml in android/app/src/debug:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />
</manifest>

I am using React Native version: 0.61.5

Daniel Le
  • 171
  • 1
  • 4
  • 8
  • I checked Logcat in Android Studio after running on my device, and saw `java.net.UnknownServiceException: CLEARTEXT communication to localhost not permitted by network security policy`. After adding `android:usesCleartextTraffic="true"` and rebuilding the app, it works. – user5305519 Jul 25 '22 at 08:04
5

Solution for React-native >V0.60

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 ipconfig getifaddr en0 For MAC

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.

  1. Open the in-app Developer menu. shake your phone or press CMD/ctrl + M
  2. Click on Settings
  3. click on Debug server host & port for device
  4. On popup Type your machine's IP address and the port of the local dev server (e.g. 10.0.1.1:8081).
  5. Go back to the Developer menu and select Reload.

DONE

Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
4

** Ubuntu Try running these commands both in your root folder only:

  1. sudo npm start --- this will pause automatically after saying -- Loading Graphs, done (something like this) ; when this is done -open another terminal

  2. react-native run-android


If still the issue prevails, delete your build folder in android / ios execute this command and then execute the above mentioned commands in the same order

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && react-native run-android**
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
Shubham Kakkar
  • 581
  • 6
  • 4
4

I've added <uses-permission android:name="android.permission.INTERNET" /> to debug/AndroidManifest.xml and it started working! I've removed it from main/AndroidManifest.xml and forgot to add in debug.

ReactNative version is 0.61.2

Denys Klymenko
  • 394
  • 5
  • 18
4

What solved the problem for me was to open up android studio, go into AVD manager, actions > Stop and then Cold Boot Now.

Once the device booted up, run react-native start and react-native run-android as usual.

enter image description here

enter image description here

jperl
  • 4,662
  • 2
  • 16
  • 29
3

To add to goldylucks's answer, you can use IP 192.168.0.10 if that is the IP of your PC running the React Native packager.

You may need to open your firewall as well.

In Ubuntu 18.04, you can get your IP by:

ip -c addr show

You will see your IP in there, something like: inet 192.168.0.10/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s25 (notice the 192.168.0.10 in my sample).

Then, you could open port 8081 in your firewall, something like:

sudo ufw allow 8081

It will be using TCP, so make sure TCP is open on port 8081.

Then you can follow goldylucks's answer and goto Dev settings and enter 192.168.0.10:8081 or whatever IP you need.

You can test it from the browser on your device. While the packager is running from your PC, navigate to http://192.168.0.10:8081 from your mobile device and make sure it works. It will show something like "Packager running". It will be clearly working or failing.

Goldylock's answer excites me because it solves the issue very nicely if you are having problems with that error messages. It sucks to have to rebuild the app all the time or worse yet, delete the APK off your device and then build again.

As such, you should probably be using wifi as part of your dev strategy.

agm1984
  • 15,500
  • 6
  • 89
  • 113
3

I met this question too in native and react-native pro, my solution was:

  1. open npm server with npm start
  2. run native project with Android Studio
  3. transmission network request with adb reverse tcp:8081 tcp:8081
  4. when you need to reload the page, just input: adb shell input keyevent 82

just keep it in order.

P.S. if you ran a fail project, just run adb kill-server

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
3

Easiest for MAC. Get ip address of your wifi network via ipconfig getifaddr en0. Then write that ip to your DEV settings -> Debug server host & port for device adding :8081 example. 192.21.22.143:8081. Then reload. That's it

Nagibaba
  • 4,218
  • 1
  • 35
  • 43
3

Found the issue with Android X device.

For Android API 28 and above

In network_security_config.xml with cleartextTrafficPermitted="true"

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    ....
  </domain-config>
</network-security-config>

Then, reload JS

Amit
  • 2,389
  • 22
  • 29
  • I'm currently on React Native 0.63 and ran into this issue after adding a custom network_security_config.xml to intercept some traffic with a proxy. With no custom config it seems to work fine on emulators >API 28, but if you add a custom config you will also need to allow cleartext on localhost. – ZardozSpeaks Dec 07 '20 at 20:19
2

if adb reverse tcp:8081 tcp:8081 this dosent work , You can restart you computer it'll work , because your serve is still running that's wrok form me

Rajan rek
  • 322
  • 2
  • 8
2

This error occurs because your Android Virtual Device is not connected to development server. So, check that your Android Virtual Device is connected to internet or not. You can do that by simply checking WiFi connectivity symbol and Mobile Data symbol.

enter image description here

utsav
  • 41
  • 2
2

ifconfig

will give you your ip address

copy that Ip address looking something like 192.168.1.100

cmd + M

will show the debug menu for android

(in case it doesn't work you can always use the shell)

adb shell input keyevent 82

Click Choose Bundle Location and paste your IP address followed by the port 8081 is the default one... (should look something like this)

192.168.1.100:8081

Click OK

1

If you are trying to debug app in your physical android device over wifi using a windows machine, then the device may not be able to access the port of your pc or laptop, you have to make the port accessible. this involves two steps:

  1. First create a rule in firewall. for doing this follow the following steps:

    • open run dialog
    • type wf.msc
    • click on inbound rules
    • click new rule on right hand side
    • select port from pop up menu and click next
    • select tcp port and specific local ports and enter the port number like 8081 (default)
    • allow the connection
    • select all in profile section
    • give some appropriate name and description
    • click finish
  2. you have to make your pc accessible to outside, for doing this follow the following steps:

    • open network and sharing centre from control panel
    • change adapter settings
    • select your wifi network
    • right click, properties
    • click on sharing tab
    • check all the checkboxes

You are good to go, now try running react-native run-android.

Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
vinay gosain
  • 171
  • 6
1

Make sure NPM server is running. Else run it again. It will solve the issue.

Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35
1

After trying all the most upvoted answers and failing to make it work, I figured that my Android Simulator was on Airplane mode. Quite silly but posting this so it could save some time.

P.S. I had a safe reboot on my Simulator before this, which is probably the cause why my simulator booted in Airplane mode

Mudassir Ali
  • 7,913
  • 4
  • 32
  • 60
1

I fixed the issue on windows by setting the IP address of my local development server in my android device.

Devorein
  • 1,112
  • 2
  • 15
  • 23
1

For me, this happened when I build from Android Studio instead of the command line using react-native run-android

zvv
  • 473
  • 1
  • 4
  • 9
1

This issue has happened several times to me already. It's working one minute, and then the next minute it stops working without any change on my part. The 2nd IP in the error is usually 10.0.2.x instead of 192.x.

I've went through all the answers without being able to resolve the issue.

Ultimately, the one thing that has always worked is to go back to Android Studio, open AVD, and then select the Simulator in question. Do a Duplicate to create another copy of the simulator, and then everything will magically work again.

So my guess is that something inside the simulator folder got corrupted.

Edward Tan
  • 934
  • 9
  • 17
0

This is most probably a firewall issue. If someone using ubuntu faces this issue , then you can use

sudo service iptables stop

to disable the firewall for the port to be accessible

0

and first of all I appreciate you for posting your doubt. It's mainly because maybe your expo or the app in which you run your react native project may not be in the same local connected wifi or LAN. Also, there is a chance that there is disturbance in your connection frequently due to which it loses its connectivity.

I too face this problem and I personally clear the cache and then restart again and boom it restarts perfectly. I hope this works for you too!

Rishav Kumar
  • 4,979
  • 1
  • 17
  • 32
0

In my case, my emulator has a proxy setting, after turning it off everything works fine.

Mahdi Farhani
  • 964
  • 1
  • 9
  • 22
0

I got the same problem and resolve it by deleting node module package and then again install yarn. Simply on some changes we need to clear our.

Boken
  • 4,825
  • 10
  • 32
  • 42
0

Basically this error comes when npm server is not started.

So at first check the npm server status, if it's not running then start npm with command npm start and you can see in terminal:

Loading dependency graph done.

Now npm is started and run your app in another terminal with command

react-native run-android
Cà phê đen
  • 1,883
  • 2
  • 21
  • 20
Sarojini2064130
  • 221
  • 3
  • 7
0

For me i have uninstalled a package that i recently installed and the project ran again on my physical device using the following command "npm uninstall recentPackageName" hope this helps :)

user8572385
  • 457
  • 4
  • 6
0

When I started a new project

react-native init MyPrroject

I got could not connect to development server on both platforms iOS and Android.

My solution is to

sudo lsof -i :8081
//find a PID of node
kill -9 <node_PID>

Also make sure that you use your local IP address

ipconfig getifaddr en0
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
0

In my case, running on a Macbook, i had to turn off my firewall, thus allowing incoming connections from my android. RN v0.61.5

Jim
  • 1,988
  • 6
  • 34
  • 68
0

RESTART YOUR PHONE

That's all I did and the app bundled successfully.

Gabriel Arghire
  • 1,992
  • 1
  • 21
  • 34
0

IF YOU ARE USING EXPO and it used to connected and suddenly getting the generic error "Something went wrong" many time, with no further hint, meanwhile tried everything else, and the setting are all good, the reason is that your expo running on device does not match anymore with the one on the project, maybe you are running an old project to bump up or similar(I did finally got this message once I tried to connected the app with the direct connection as from docs.

Hope this would help others avoid waist time.

Carmine Tambascia
  • 1,628
  • 2
  • 18
  • 32
-1

n my case with Android 4.4.2 had to do some additional steps: (Make sure rn server is running npm start, delete app-debug.apk from android/app/build/outputs/apk , uninstall from android if previously installed and run following commands from root folder)

1_ create dir mkdir android/app/src/main/assets
2_ create file index.android.js which is a clone of index.js cp index.js index.android.js
3_ react-native link
4_ curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
5_ react-native run-android
Zhong Ri
  • 2,556
  • 1
  • 19
  • 23
  • No, Your step didn't work. Firstly I got "Unexpected token >" error and when I reload app then It gave "Couldn't connect to development server" – Rajat Apr 29 '21 at 04:05
-1

If did not work all of these answer's just delete the node_modules/ and npm i --save

Mike Victoria
  • 245
  • 1
  • 4
  • 23
-1

Just open cmd/powershell, and inside your project directory run the following command

react-native start
NAVNEET CHANDAN
  • 268
  • 3
  • 7
-3

We faced this issue, In order to fix this, solution is dead simple is below.

  1. Cancel the current process of“react-native run-android” by CTRL + C or CMD + C
  2. Close metro bundler window command line which opened automatically.
  3. Run the command again, “react-native run-android”.

Basically this error tells that your current build got failed due to reasons like Code issue or dependency issue.

Click here for more details

Venkat.R
  • 7,420
  • 5
  • 42
  • 63