首页 > 解决方案 > 适用于 Android 的 React-Native:无法连接到开发服务器

问题描述

我正在为现有项目设置 react native android。我按照官方入门页面的说明进行操作。当我运行时,我可以在模拟器中打开应用程序react-native run-android。该应用程序运行完美。但问题是,当我使用双击R键重新加载它时,它会抛出错误Could not connect to development server

在此处输入图像描述

在模拟器和真实设备上都出现此错误

我尝试过的解决方案:

  1. 在同一个 wifi 上运行真实设备和 Macbook。
  2. 在Android设备/模拟器中设置macbook的IP地址和端口Dev Setting -> Debug server host & port for device
  3. 跑步adb reverse tcp:8081 tcp:8081

仍然没有运气。

当我使用打开开发人员选项CMD + D并单击时Debug JS Remotely,它会引发不同的错误: Unable to connect to remote debugger. CLEARTEXT communication to 192.x.x.x not permitted by network security policy.

在某些情况下,它显示“连接到远程调试器”,但它卡在那里。 在此处输入图像描述

标签: androidreactjsreact-native

解决方案


这个答案解决了我的问题:https ://stackoverflow.com/a/53158627/2630184

似乎这是Cleartext在 Android Pie 上的问题,因为我也提到了这个问题,即在单击时显示明文错误Debug js remotely

添加android:usesCleartextTraffic="true"AndroidManifest.xml,为我解决了这个问题。

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

推荐阅读