一,设备与环境
1,一台 mac
2,安装软件包管理器 Homebrew(中文)
终端里输入/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
进行安装。
3,安装 wget 或者 curl
wget,终端里输入brew install wget
进行安装。
curl,终端里输入brew install curl
进行安装。
4,安装 nvm (node版本管理器)
如果安装了 wget,终端输入wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
进行安装 nvm。
如果安装了 curl,终端输入curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bashÂ
进行安装 nvm。
5,Android SDK
这个不用多说了,相信大家都会的。顺便把 Android SDK 配置到环境变量。
如果你没有 Android SDK,也可以通过终端输入brew install android-sdk
。
二,安装和初始react-native demo
终端输入npm install -g react-native-cli
安装 react-native。
如果显示EACCES: permission denied
,用超级权限再次执行一下上面的指令。反正我是没遇到。
进入到你要创建 demo 的目录,终端输入cd 你得文件路径
.
终端输入react-native init AwesomeProject
,初始 AwesomeProject 。
终端输入cd AwesomeProject
。
终端输入react-native run-android
可以直接运行 node 服务并编译运行 Android 项目,直至安装到手机。
如果界面报错提示如下:
Unable to download JS bundle
com.facebook.react.devsupport.DebugServerException: Could not connect to development server.
URL: http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false
Try the following to fix the issue:
• Ensure that the packager server is running
• Ensure that your device/emulator is connected to your machine and has USB debugging enabled - run 'adb devices' to see a list of connected devices
• If you're on a physical device connected to the same machine,run 'adb reverse tcp:8081 tcp:8081' to forward requests from your device
• If your device is on the same Wi-Fi network,set 'Debug server host & port for device' in 'Dev settings' to your machine's IP address and the port of the local dev server - e.g. 10.0.1.1:8081
at com.facebook.react.devsupport.DevServerHelper$1.onFailure(DevServerHelper.java:203)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:175)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
一般在终端输入adb reverse tcp:8081 tcp:8081
即可解决,其他请详细看 log。