环境搭建与工具链

React Native开发环境搭建与工具链介绍

iOS环境搭建

必须安装的依赖有:Node、WatchmanReact Native命令行工具以及Xcode。虽然你可以使用任何编辑器来开发应用(编写js代码,但你仍然必须安装Xcode来获得编译iOS应用所需的工具和环境。

Node, Watchman

我们推荐使用 Homebrew 来安装NodeWatchman。在命令行中执行下列命令安装:

brew install node
brew install watchman

如果你已经安装了Node,请检查其版本是否在v8.3以上。安装完Node后建议设置npm镜像以加速后面的过程(或使用科学上网工具

设置npm镜像:

npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global

或者使用 nrm

$ nrm ls

* npm -----  https://registry.npmjs.org/
  cnpm ----  http://r.cnpmjs.org/
  taobao --  https://registry.npm.taobao.org/
  nj ------  https://registry.nodejitsu.com/
  skimdb -- https://skimdb.npmjs.com/registry
$ nrm use cnpm  //switch registry to cnpm

    Registry has been set to: http://r.cnpmjs.org/

Watchman 则是由Facebook提供的监视文件系统变更的工具。安装此工具可以提高开发时的性能(packager可以快速捕捉文件的变化从而实现实时刷新

Yarn、React Native的命令行工具(react-native-cli)

YarnFacebook提供的替代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

安装完yarn之后就可以用yarn代替npm了,例如用yarn代替npm install命令,用yarn add某第三方库名代替npm install某第三方库名。

Xcode

React Native目前需要 Xcode 9.4或更高版本。你可以通过App Store或是到 Apple开发者官网 上下载。这一步骤会同时安装Xcode IDEXcode的命令行工具和iOS模拟器。

  • Xcode的命令行工具

启动Xcode,并在 Xcode | Preferences | Locations 菜单中检查一下是否装有某个版本的 Command Line ToolsXcode的命令行工具中包含一些必须的工具,比如 git 等。

搭建Android开发环境

必须安装的依赖有:Node、WatchmanReact Native命令行工具以及JDKAndroid Studio。虽然你可以使用任何编辑器来开发应用(编写js代码,但你仍然必须安装Android Studio来获得编译Android应用所需的工具和环境。

Java Development Kit

React Native需要Java Development Kit [JDK] 1.8(暂不支持1.9及更高版本。你可以在命令行中输入 javac -version 来查看你当前安装的JDK版本。如果版本不合要求,则可以到官网上下载。

Android开发环境

安装Android Studio

首先下载和安装 Android Studio,国内用户可能无法打开官方链接,请自行使用搜索引擎搜索可用的下载链接。安装界面中选择"Custom"选项,确保选中了以下几项:

  • Android SDK
  • Android SDK Platform
  • Performance (Intel ® HAXM)
  • Android Virtual Device

然后点击"Next"来安装选中的组件,安装完成后,看到欢迎界面时,就可以进行下面的操作了。

安装Android SDK

Android Studio默认会安装最新版本的Android SDK。目前编译React Native应用需要的是Android 6.0 (Marshmallow)版本的SDK(注意SDK版本不等于终端系统版本,RN目前支持android 4.1以上设备。你可以在Android StudioSDK Manager中选择安装各版本的SDK

你可以在Android Studio的欢迎界面中找到SDK Manager。点击 “Configure”,然后就能看到 “SDK Manager”。SDK Manager还可以在Android Studio的 “Preferences” 菜单中找到。具体路径是 Appearance & Behavior → System Settings → Android SDK

SDK Manager中选择 “SDK Platforms"选项卡,然后在右下角勾选 “Show Package Details”。展开Android 6.0 (Marshmallow)选项,确保勾选了下面这些组件:

  • Android SDK Platform 28
  • Intel x86 Atom_64 System Image(官方模拟器镜像文件,使用非官方模拟器不需要安装此组件)

开发

调试

原生集成

错误

Links

上一页
下一页