【问题标题】:React-native android debugger connecting to 10.0.2.2 instead of localhostReact-native android 调试器连接到 10.0.2.2 而不是 localhost
【发布时间】:2018-09-05 01:23:03
【问题描述】:

使用react-native run-android 运行应用程序时,它连接到 10.0.2.2:8081 而不是 localhost:8081 并且无法调试。

有谁知道如何修复它以连接到本地主机?

【问题讨论】:

    标签: react-native android-emulator react-native-android react-native-debugger


    【解决方案1】:

    在 MAC 上,我通过以下方式解决了它:

    1. 在模拟器屏幕上按Cmd + M
    2. 开发设置 > 调试服务器主机和设备端口
    3. 设置localhost:8081
    4. 重新运行安卓应用:react-native run-android

    调试器现已连接!

    希望它能帮助其他人:)

    【讨论】:

    • Thnaks,对于打开的 Dev Settins 在 windows 中使用 adb shell input keyevent 82
    • @ZanyarJ.Ahmed 和 linux 也
    【解决方案2】:

    您可以尝试通过菜单上的Dev Settings > Debug server & host port for device 来更改它,您可以访问摇动设备或运行adb shell input keyevent 82 命令在终端中

    【讨论】:

      【解决方案3】:

      只需运行端口转发

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

      或者你的 api 服务器到端口 5000

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

      【讨论】:

        【解决方案4】:

        如果您创建了 network_security_config.xml 文件以允许硬件设备连接,这可能是问题的根源。只需在该文件中添加 localhost 和 10.0.2.2 即可。

        例如。 network_security_config.xml 文件:

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

        【讨论】:

          【解决方案5】:

          我为此写了一个小包,因为它让我不得不打开屏幕,尤其是在使用和重新启动多个模拟器时。查看here 和示例项目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 
          

          【讨论】:

            猜你喜欢
            • 2018-08-21
            • 2016-05-28
            • 2020-08-10
            • 2020-04-13
            • 1970-01-01
            • 2019-03-02
            • 2018-09-19
            • 2016-06-28
            • 1970-01-01
            相关资源
            最近更新 更多