使用react-native run-android
运行应用程序时,它连接到10.0.2.2:8081而不是Localhost:8081,无法调试。
有人知道如何修复以便它可以连接到Localhost吗?
在Mac上我通过以下操作解决了它:
- 在模拟器屏幕上按
Cmd + M
- 开发设置>调试服务器主机&设备的端口
- 设置
localhost:8081
- 重新运行Android App:
react-native run-android
现在连接的调试器!
希望它能帮助他人:(
您可以尝试通过 dev设置> debug Server&菜单上用于设备的主机端口,您可以访问摇动设备或在终端中运行adb shell input keyevent 82
命令
只需运行端口转发
adb -s emulator-5554 reverse tcp:8081 tcp:8081
或您的API服务器到端口5000
adb -s emulator-5554 reverse tcp:5000 tcp:5000
如果已经创建了一个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>
我为此写了一个小包装,因为它使我坚定不得不打开屏幕,尤其是在使用和重新启动多个模拟器时。在此处查看并在此处查看一个示例项目:
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