React-native android debugger connecting to 10.0.2.2 instead of localhost
Asked Answered
S

5

12

While running app with react-native run-android its connecting to 10.0.2.2:8081 instead of localhost:8081 and not able to debug.

Does anyone know how to fix so that it will connect to localhost instead?

Sublittoral answered 26/3, 2018 at 19:32 Comment(0)
K
34

On MAC I solved it by doing following:

  1. Press Cmd + M on emulator screen
  2. Dev settings > Debug server host & port for device
  3. Set localhost:8081
  4. Rerun the android app: react-native run-android

Debugger is connected now!

Hope it will help others :)

Killing answered 30/7, 2018 at 12:40 Comment(2)
Thnaks, for open Dev Settins use adb shell input keyevent 82 in windowsExplication
@ZanyarJ.Ahmed and on linux alsoMammet
M
3

You can try to change it through Dev Settings > Debug server & host port for device on menu, that you can access shaking a device or run adb shell input keyevent 82 command in a terminal

Macmahon answered 26/3, 2018 at 21:3 Comment(0)
R
3

Just run port forwarding

adb -s emulator-5554 reverse tcp:8081 tcp:8081

OR your api server to port 5000

adb -s emulator-5554 reverse tcp:5000 tcp:5000
Roots answered 16/4, 2019 at 16:10 Comment(0)
I
1

If you have created a network_security_config.xml file to allow for hardware devices to connect, this can be the source of the issue. Just add localhost and 10.0.2.2 in that file, and you should be good.

eg. network_security_config.xml file:

<network-security-config>
  <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="false">localhost</domain>
    <domain includeSubdomains="false">10.0.2.2</domain>
    <domain includeSubdomains="false">192.168.0.12</domain>
  </domain-config>
  <base-config>
    <trust-anchors>
        <certificates src="system"/>
        <certificates src="user"/>
    </trust-anchors>
  </base-config>
</network-security-config>
Instillation answered 17/2, 2021 at 19:50 Comment(0)
G
0

I wrote a little package for this because it was driving me nuts having to open the screen, especially when using and restarting multiple emulators. Check it out here and an example project here:

npm i @nick-bull/react-native-debug-address

# DEBUG_HOST=127.0.0.1:8081 npx react-native start --port 8081
# or, equivalently
DEBUG_PORT=8081 npx react-native start --port 8081

npx react-native run-android --port 8081 
Grande answered 14/3, 2021 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.