AVD上的react-native无法连接到exp:// IP:PORT

时间:2018-06-04 01:42:31

标签: react-native

在关于使用react-native(https://facebook.github.io/react-native/docs/getting-started.html)的facebook.io教程之后,我遇到了一个问题,我用来测试应用程序的AVD android模拟器正在抱怨

  

出了点问题。无法加载exp://192.168.200.83:19000。

请注意,这是在通过create-react-native-app myapp

生成的新/未编辑项目上完成的
# after having started the AVD and creating the react-native app...

➜  myap npm start               

> myapp@0.1.0 start /<path to my react native app>/myapp
> react-native-scripts start

3:26:37 PM: Starting packager...
Packager started!                                   

Your app is now running at URL: exp://192.168.200.83:19000

View your app with live reloading:

  Android device:
    -> Point the Expo app to the QR code above.
       (You'll find the QR scanner on the Projects tab of the app.)
  iOS device:
    -> Press s to email/text the app URL to your phone.
  Emulator:
    -> Press a to start an Android emulator.

Your phone will need to be on the same local network as this computer.
For links to install the Expo app, please visit https://expo.io.

Logs from serving your app will appear here. Press Ctrl+C at any time to stop.

 › Press a to open Android device or emulator.
 › Press s to send the app URL to your phone number or email address
 › Press q to display QR code.
 › Press r to restart packager, or R to restart packager and clear cache.
 › Press d to toggle development mode. (current mode: development)

<press a>

此时会在AVD上打开一个窗口(可能是尝试启动react-native应用程序)并显示上述错误。

(还尝试使用npm run android启动应用并打开AVD上的expo应用中的链接,但仍然出现相同的错误。)

有谁知道此时可以做些什么(全新的反应和移动开发,所以我大多只关注教程中的内容)?我应该在这个问题上添加的任何其他信息吗?

1 个答案:

答案 0 :(得分:1)

看一下类似的github帖子(https://github.com/react-community/create-react-native-app/issues/595#issuecomment-373539195),似乎是使用错误的网络的问题(即使用AVD无法访问的LAN URL)。并且使用该帖子中描述的exp start --tunnel方法确实生成了我的手机展示应用实际可以解释的网址。我的工作步骤如下所示。 (注意我使用的是Ubuntu 16.04

查看安装watchmanhttps://facebook.github.io/watchman/docs/install.html#system-specific-preparation)的文档(由react-native(在linux上)使用)。我看到有一个特定的事情是关于增加linux上的inotify限制(虽然没有给出任何具体的值)(关于inotify的信息,请参阅https://en.wikipedia.org/wiki/Inotify)。

所以从源代码安装watchman后(似乎是目前在linux上安装它的唯一方法)......

$ cd ~
$ git clone https://github.com/facebook/watchman.git
$ cd watchman/
$ git checkout v4.7.0
$ sudo apt-get install -y autoconf automake build-essential python-dev
$ ./autogen.sh 
$ ./configure 
$ make
$ sudo make install

$ watchman --version

...我们增加了inotify限制。

$ echo 999999 | sudo tee -a /proc/sys/fs/inotify/max_user_watches  \
    && echo 999999 | sudo tee -a  /proc/sys/fs/inotify/max_queued_events \
    && echo 999999 | sudo tee  -a /proc/sys/fs/inotify/max_user_instances \
    && watchman shutdown-server

基于这个较旧的SE帖子(https://askubuntu.com/a/155343/760862)(基本上表明它根本没有占用太多资源),我假设(并希望)增加它就像这样可以。

(增加像这样的inotify限制似乎有助于解决我在使用本机反应项目时遇到的一些其他问题(例如,当尝试捆绑导出到设备时javascript冻结,设备抱怨“有些事情发生了错误的“或者对我试图向他们出口产品等完全没有反应。”)

现在,导航到项目目录并使用--tunnel选项启动exp服务器进程。来自文档(https://docs.expo.io/versions/latest/workflow/exp-cli):

  

tunnel(默认),lan,localhost。要使用的主机类型。 “隧道”允许您查看其他网络上的链接

exp start --tunnel
22:21:14 [exp] Your URL is: exp://xm-apt.myexpousername.myapp.exp.direct:80

22:21:14 [exp] Instructions to open this project on a physical device
22:21:14 [exp] Android devices: scan the above QR code.
22:21:14 [exp] iOS devices: run exp send -s <your-phone-number-or-email> in this project directory in another terminal window to send the URL to your device.

22:21:14 [exp] Instructions to open this project on a simulator
22:21:14 [exp] If you already have the simulator installed, run exp ios or exp android in this project directory in another terminal window.

22:21:14 [exp] Logs for your project will appear below. Press Ctrl+C to exit.

如果您看到类似于exp://192.168.200.83:19000的网址,您还应该看到一些日志消息,例如

  

21:30:35 [exp]切换到LAN URL,因为隧道似乎是   下。只有同一网络中的设备才能访问该应用。重新开始   与exp start --tunnel尝试重新连接。

使用相同的命令重新启动,URL应该是非LAN位置。

可选(如果您想使用项目中npm start的界面)

然后打开另一个终端选项卡并运行npm start命令,如此

REACT_NATIVE_PACKAGER_HOSTNAME='xm-apt.myexpousername.myapp.exp.direct' npm start -- --reset-cache

其中REACT_NATIVE_PACKAGER_HOSTNAME设置为上一步骤中的exp URL(有关此内容的信息可以在通过create-react-native-app创建的每个react-native应用程序的自述文件中找到)。此处创建的URL现在应该类似于

Your app is now running at URL: exp://xm-apt.myexpousername.myapp.exp.direct:19000

现在打开另一个标签。请注意,在运行exp过程的选项卡上,应该有关于如何在不同设备上打开项目的说明(例如,在本答案的前面)。使用这些说明使用我们刚刚打开的标签打开不同设备的项目(在我的情况下,对于AVD,运行exp android(您应该在运行exp过程的选项卡中看到一些消息,如22:28:57 [exp] Finished building JavaScript bundle in 97ms让你知道它正在运作))。

完全新的反应 - 任何所以,如果有人看到任何关于此设置的任何内容是多余的或奇怪的,请告诉我。

相关问题