- 安装完node后建议设置npm镜像以加速后面的过程(或使用科学上网工具)。注意:不要使用cnpm!cnpm安装的模块路径比较奇怪,packager不能正常识别!
npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global
- Yarn、React Native的命令行工具(react-native-cli)
- Yarn是Facebook提供的替代npm的工具,可以加速node模块的下载。React Native的命令行工具用于执行创建、初始化、更新项目、运行打包服务(packager)等任务。
npm install -g yarn react-native-cli
-
安装完yarn后同理也要设置镜像源:
yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global
-
运行
react-native init AwesomeProject
创建React-Native项目 -
运行
cd AwesomeProject
切换到项目根目录中,运行adb devices
来确保有设备连接到了电脑上 -
运行
react-native run-android
打包编译安卓项目,并部署到模拟器或开发机中 -
运行上一条命令之前,要确保有设备连接到了电脑上,可以运行
adb devices
查看当前接入的设备列表,打包好的文件,放到了android\app\build\outputs\apk
目录下 - 入坑指南
问题1:开启悬浮框权限;
问题2:Could not get BatchedBridge, make sure your bundle is packaged correctly
解决方案:在终端中,进入到项目的根目录,执行下面这段命令行:
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
运行之前,需要确保android/app/src/main/
目录下有assets
文件夹,如果没有,手动创建之~,再运行上面的命令;
问题3:could not connect to development server
解决方案:晃动手机,唤起设置属性窗口,点击“Dev settings”,再点击Debuug server host 出现设置ip地址窗口,填写Ip地址和端口号8081,例如`192.168.1.111:8081
发表评论