提供了一款名叫 Expo Go 的 app 作为宿主应用,让用户预先安装在手机上。用户使用 Expo Go 扫码或者输入URL的时候,会通过网络请求把我们的应用作为一个子应用加载过去。类比一下微信小程序就好理解了。当然,Expo Go 的这种玩法一般只用于开发阶段,实际上打包发布应用市场的时候不需要这么玩,可以打包成独立的apk包。
使用方式 2 运行 app 时,遇到报错“Waiting for Target Device to Come Online”,解决方式同上。
使用方式 2 运行 app 时,遇到报错“Unable to load script.Make sure you are either running a Metro server or ......”,请参考这条stackoverflow 上面的回答。我这里是在 cmd 执行了 adb reverse tcp:8081 tcp:8081 之后就好了。
5 使用 React Native CLI(容器化)
5.1 方案评估
由于 React Native CLI 环境配置比较繁琐,并且还容易受各种版本不配套的影响,容易把 PC 上的环境弄乱。
WSL 和宿主系统默认就是共享电脑磁盘的,我们可以在宿主机的 IDE 中编辑代码,在 WSL 中编译,使用起来也会比较方便。
这里从 WSL 的安装开始,把完整步骤全部写出来。
5.2 安装配置 WSL
使用管理员身份运行cmd
wsl --install -d Ubuntu-20.04
等一段时间之后,需要的资源都下载完成,就可以启动 WSL 了
wsl -u root
直接使用 root 用户操作 linux 并不是一个好习惯。正常来说应该使用普通用户并做好权限限制。这里使用 root 用户操作,一方面是为了简化教程,另一方面是这里的 WSL 只作为一个开发环境,也不涉及下载什么危险软件,就不讲究这个了。
习惯上,使用 linux 系统之前,先换上一个靠谱的国内软件源
echo"
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
" > /etc/apt/sources.list
docker exec -it rn-container bash
# 后面的环节会有查看 ip 的环节,并可能面临检查网络连通性的需求。安装这两个软件是为了使用 ifconfig 和 ping 命令。
apt update
apt install net-tools iputils-ping
cd /root/rn-projects
npx react-native init AwesomeProject
cd AwesomeProject
npx react-native run-android
报错说连不上模拟器。因为此时电脑上面没有装任何安卓模拟器,也没对接任何真机:
error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.
warn Please launch an emulator manually or connect a device. Otherwise app may fail to launch.
现在可以关闭命令行窗口接着下面的步骤了。
5.5 安装模拟器
安装模拟器也有多种方案可以选择:
使用 WSA(Windows Subsystem for Android)
安装 android studio 使用它自带的模拟器
安装一个网上常见的安卓模拟器(平时主要用来打游戏的那种 xx 模拟器)
选择方案2,因为 WSA 目前尚未成熟,也无法很方便地在 windows 商店下载安装,而另外下载一款模拟器又嫌多此一举,毕竟 android studio 后面可能还会用到,而且支持的系统版本和机型也更多一些。
首先就是下载 android studio 进行安装。安装步骤就比较简单了,直接一路next。记得勾选一下安装虚拟设备的选项就行。