Could not connect to React Native development server on Android

前端 未结 28 1675
鱼传尺愫
鱼传尺愫 2020-11-30 22:10

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

Could not connect to development server

相关标签:
28条回答
  • 2020-11-30 22:24

    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>
    
    0 讨论(0)
  • 2020-11-30 22:24

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

    0 讨论(0)
  • 2020-11-30 22:24

    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 :)

    0 讨论(0)
  • 2020-11-30 22:25

    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

    0 讨论(0)
  • 2020-11-30 22:27

    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

    0 讨论(0)
  • 2020-11-30 22:28

    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

    0 讨论(0)
提交回复
热议问题