React Native IOS端和anroid端环境搭建(含踩坑过程)

文档地址: https://reactnative.cn/docs/getting-started/

一.macOS iOS 安装

1.切换Homebrew国内镜像源 (暂时不推荐用了,国内镜像最新才9.11.1)

替换brew.git:

cd "$(brew --repo)"

git remote set-url origin [https://mirrors.ustc.edu.cn/brew.git](https://mirrors.ustc.edu.cn/brew.git)

//替换homebrew-core.git:

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin [https://mirrors.ustc.edu.cn/homebrew-core.git](https://mirrors.ustc.edu.cn/homebrew-core.git)

echo "export HOMEBREW_BOTTLE_DOMAIN=[https://mirrors.ustc.edu.cn/homebrew-bottles](https://mirrors.ustc.edu.cn/homebrew-bottles)">>~/.profile

source ~/.profile

下面是还原

重置brew.git:

cd "$(brew --repo)"

git remote set-url origin [https://github.com/Homebrew/brew.git](https://github.com/Homebrew/brew.git)

重置homebrew-core.git:

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin [https://github.com/Homebrew/homebrew-core.git](https://github.com/Homebrew/homebrew-core.git)

2.brew install node

大概要半个小时

3.brew install watchman

大概也要半小时

报如下错误

image.png

下载最新的xcode10.2后


image.png

接着按错误提示安装
xcode-select --install
最后在安装一遍
brew install watchman
报权限不够错误


image.png

尝试修改所有者权限
sudo chown -R $(whoami) /usr/local
image.png

解决:

sudo chown -R $(whoami) /usr/local/* 

[https://stackoverflow.com/questions/46459152/cant-chown-usr-local-for-homebrew-in-mac-os-x-10-13-high-sierra]

image.png

4.接下来设置镜像,在安装其他命令

npm config set registry https://registry.npm.taobao.org —global
npm config set disturl https://npm.taobao.org/dist —global
npm install -g yarn react-native-cli
yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global
image.png

5.创建项目
cd /Users/mac/Documents/wangyu/pzworkspace/reactIOSProject
//5.1初始化
react-native init AwesomeProject
报错了


image.png

有可能是翻墙导致,(因为镜像都设成淘宝了)关闭在试 不行
仔细看了下错误,感觉node版本比较老导致 目前支持6 和 8开头版本,以及10以上版本


image.png

感觉被国内镜像给坑了,homeBrew 镜像还原成国外的(注意全局命令export 也注释掉),在翻墙更新node

brew upate node

如果不担心冲突也可以直接官网下载最新的node https://nodejs.org/en/

image.png

node装好后,安全起见再重新安装如下命令

npm config set registry https://registry.npm.taobao.org —global
npm config set disturl https://npm.taobao.org/dist —global
npm install -g yarn react-native-cli
yarn config set registry https://registry.npm.taobao.org --global
yarn config set disturl https://npm.taobao.org/dist --global

最后在init

react-native init AwesomeProject

成功了:


image.png

//5.2运行
cd AwesomeProject
react-native run-ios

image.png

百度参考:https://juejin.im/post/5cbc2ee5f265da035a1f1b79

按顺序执行如下命令 (不要删除原来工程文件)

cd /Users/mac/Documents/wangyu/pzworkspace/reactIOSProject/AwesomeProject

rm -rf node_modules && yarn cache clean

yarn install

rm -rf ~/.rncache

/Users/mac/Documents/wangyu/pzworkspace/reactIOSProject/AwesomeProject/node_modules/react-native/scripts/ios-install-third-party.sh
最后 还是报如下错误
react-native run-ios
error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by opening AwesomeProject.xcodeproj


image.png

原因是端口被nignx占用了
lsof -i:8081


image.png

cd /Users/mac/Documents/wangyu/pzworkspace/reactIOSProject/AwesomeProject


image.png

2.修改xcode工程所有8081代码 还有下图这个地方 参考https://blog.csdn.net/weixin_33871366/article/details/90924912

image.png

直接运行xcode工程就可以运行了
image.png

3.不过用 react-native run-ios 还是报错(先不管了)

image.png

4.由于每次init项目都要修改端口,我就把nignx的端口改成8082了
之后 就可以正常运行了

react-native init FirstProjcet
cd FirstProject
react-native run-ios
image.png

二.andorid环境搭建

1.Android Studio 官网下载

https://developer.android.com/studio

2.jdk官网

https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html

3.Android Studio 设置

android SDK 下载

/Users/mac/Library/Android/sdk


image.png

4.设置 环境变量

echo "export ANDROID_HOME=~/Library/Android/sdk">>~/.profile
echo "export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools">>~/.profile
source ~/.profile

5.尝试

cd /Users/mac/Documents/wangyu/pzworkspace/reactIOSProject/FirstProject 
react-native run-android

报如下错误说明环境就安装成功了


image.png

6.创建虚拟设备 打开工具栏选择这个创建


image.png

下载好后,还重要配置安装,安装后后点击这个启动


image.png

最后在运行
react-native run-android

就可以像上面那样正常显示了


image.png

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。