设置你的环境
在本指南中,你将学习如何设置环境,以便使用 Android Studio 和 Xcode 运行项目。这将允许你使用 Android 模拟器和 iOS 模拟器进行开发,在本地构建你的应用等等。
¥In this guide, you'll learn how to set up your environment, so that you can run your project with Android Studio and Xcode. This will allow you to develop with Android emulators and iOS simulators, build your app locally, and more.
本指南需要 Android Studio 或 Xcode。如果你已经安装了其中一个程序,则应该能够在几分钟内启动并运行。如果未安装它们,你应该花费大约一个小时来安装和配置它们。
¥This guide requires Android Studio or Xcode. If you already have one of these programs installed, you should be able to get up and running within a few minutes. If they are not installed, you should expect to spend about an hour installing and configuring them.
Is setting up my environment required?
如果你使用的是 框架,则无需设置环境。使用 React Native 框架,你无需设置 Android Studio 或 XCode,因为框架会为你构建原生应用。
¥Setting up your environment is not required if you're using a Framework. With a React Native Framework, you don't need to setup Android Studio or XCode as a Framework will take care of building the native app for you.
如果你有阻止你使用框架的约束,或者你想编写自己的框架,则设置本地环境是必需的。设置环境后,了解如何 无需框架即可开始使用。
¥If you have constraints that prevent you from using a Framework, or you'd like to write your own Framework, then setting up your local environment is a requirement. After your environment is set up, learn how to get started without a framework.
开发操作系统
¥Development OS
- macOS
- Windows
- Linux
目标操作系统
¥Target OS
- Android
- iOS
安装依赖
¥Installing dependencies
你将需要 Node、Watchman、React Native 命令行接口、JDK 和 Android Studio。
¥You will need Node, Watchman, the React Native command line interface, a JDK, and Android Studio.
虽然你可以使用你选择的任何编辑器来开发应用,但你需要安装 Android Studio 才能设置必要的工具来构建适用于 Android 的 React Native 应用。
¥While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.
节点与守望者
我们建议使用 Homebrew 安装 Node 和 Watchman。安装 Homebrew 后在终端中运行以下命令:
¥We recommend installing Node and Watchman using Homebrew. Run the following commands in a Terminal after installing Homebrew:
brew install node
brew install watchman
如果你的系统上已安装 Node,请确保它是 Node 18 或更高版本。
¥If you have already installed Node on your system, make sure it is Node 18 or newer.
守望者 是 Facebook 的一个工具,用于监视文件系统的变化。强烈建议你安装它以获得更好的性能。
¥Watchman is a tool by Facebook for watching changes in the filesystem. It is highly recommended you install it for better performance.
Java 开发套件
我们建议使用 Homebrew 安装名为 Azul Zulu 的 OpenJDK 发行版。安装 Homebrew 后在终端中运行以下命令:
¥We recommend installing the OpenJDK distribution called Azul Zulu using Homebrew. Run the following commands in a Terminal after installing Homebrew:
brew install --cask zulu@17
# Get path to where cask was installed to double-click installer
brew info --cask zulu@17
安装 JDK 后,在 ~/.zshrc
(或 ~/.bash_profile
)中添加或更新 JAVA_HOME
环境变量。
¥After the JDK installation, add or update your JAVA_HOME
environment variable in ~/.zshrc
(or in ~/.bash_profile
) .
如果你使用上述步骤,JDK 可能位于 /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
:
¥If you used above steps, JDK will likely be located at /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
Zulu OpenJDK 发行版为 Intel 和 M1 Mac 提供 JDK。与使用基于 Intel 的 JDK 相比,这将确保你在 M1 Mac 上的构建速度更快。
¥The Zulu OpenJDK distribution offers JDKs for both Intel and M1 Macs. This will make sure your builds are faster on M1 Macs compared to using an Intel-based JDK.
如果你的系统上已经安装了 JDK,我们建议使用 JDK 17。使用更高版本的 JDK 可能会遇到问题。
¥If you have already installed JDK on your system, we recommend JDK 17. You may encounter problems using higher JDK versions.
安卓开发环境
如果你是 Android 开发新手,那么设置开发环境可能会有些乏味。如果你已经熟悉 Android 开发,则可能需要配置一些内容。无论哪种情况,请务必仔细执行接下来的几个步骤。
¥Setting up your development environment can be somewhat tedious if you're new to Android development. If you're already familiar with Android development, there are a few things you may need to configure. In either case, please make sure to carefully follow the next few steps.
1. 安装 Android Studio
下载并安装 Android Studio。在 Android Studio 安装向导中,确保选中以下所有项目旁边的框:
¥Download and install Android Studio. While on Android Studio installation wizard, make sure the boxes next to all of the following items are checked:
-
Android SDK
-
Android SDK Platform
-
Android Virtual Device
然后,单击 "下一个" 安装所有这些组件。
¥Then, click "Next" to install all of these components.
如果复选框呈灰色,你稍后将有机会安装这些组件。
¥If the checkboxes are grayed out, you will have a chance to install these components later on.
设置完成并且你将看到欢迎屏幕后,请继续执行下一步。
¥Once setup has finalized and you're presented with the Welcome screen, proceed to the next step.
2. 安装 Android SDK
Android Studio 默认安装最新的 Android SDK。然而,使用原生代码构建 React Native 应用特别需要 Android 14 (UpsideDownCake)
SDK。可以通过 Android Studio 中的 SDK 管理器安装其他 Android SDK。
¥Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 14 (UpsideDownCake)
SDK in particular. Additional Android SDKs can be installed through the SDK Manager in Android Studio.
为此,请打开 Android Studio,单击 "更多操作" 按钮并选择 "SDK 管理器"。
¥To do that, open Android Studio, click on "More Actions" button and select "SDK Manager".
SDK 管理器还可以在 Android Studio "Settings" 对话框中的语言和框架 → Android SDK 下找到。
¥The SDK Manager can also be found within the Android Studio "Settings" dialog, under Languages & Frameworks → Android SDK.
从 SDK Manager 中选择 "SDK 平台" 选项卡,然后选中右下角 "显示包详情" 旁边的框。查找并展开 Android 14 (UpsideDownCake)
条目,然后确保选中以下项目:
¥Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 14 (UpsideDownCake)
entry, then make sure the following items are checked:
-
Android SDK Platform 34
-
Intel x86 Atom_64 System Image
或Google APIs Intel x86 Atom System Image
或(对于 Apple M1 Silicon)Google APIs ARM 64 v8a System Image
¥
Intel x86 Atom_64 System Image
orGoogle APIs Intel x86 Atom System Image
or (for Apple M1 Silicon)Google APIs ARM 64 v8a System Image
接下来,选择 "SDK 工具" 选项卡并选中 "显示包详情" 旁边的框。查找并展开 "Android SDK 构建工具" 条目,然后确保选择 34.0.0
。
¥Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the "Android SDK Build-Tools" entry, then make sure that 34.0.0
is selected.
最后点击 "应用" 下载并安装 Android SDK 及相关构建工具。
¥Finally, click "Apply" to download and install the Android SDK and related build tools.
3. 配置 ANDROID_HOME 环境变量
React Native 工具需要设置一些环境变量才能使用原生代码构建应用。
¥The React Native tools require some environment variables to be set up in order to build apps with native code.
将以下行添加到 ~/.zprofile
或 ~/.zshrc
(如果你使用 bash
,则使用 ~/.bash_profile
或 ~/.bashrc
)配置文件:
¥Add the following lines to your ~/.zprofile
or ~/.zshrc
(if you are using bash
, then ~/.bash_profile
or ~/.bashrc
) config file:
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/platform-tools
运行 source ~/.zprofile
(或 source ~/.bash_profile
代表 bash
)将配置加载到当前 shell 中。验证 ANDROID_HOME 是否已通过运行 echo $ANDROID_HOME
设置,并且相应的目录已通过运行 echo $PATH
添加到你的路径中。
¥Run source ~/.zprofile
(or source ~/.bash_profile
for bash
) to load the config into your current shell. Verify that ANDROID_HOME has been set by running echo $ANDROID_HOME
and the appropriate directories have been added to your path by running echo $PATH
.
请确保你使用正确的 Android SDK 路径。你可以在 Android Studio "Settings" 对话框中的语言和框架 → Android SDK 下找到 SDK 的实际位置。
¥Please make sure you use the correct Android SDK path. You can find the actual location of the SDK in the Android Studio "Settings" dialog, under Languages & Frameworks → Android SDK.
准备 Android 设备
你将需要一台 Android 设备来运行 React Native Android 应用。这可以是物理 Android 设备,或更常见的是,你可以使用 Android 虚拟设备,它允许你在计算机上模拟 Android 设备。
¥You will need an Android device to run your React Native Android app. This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer.
无论哪种方式,你都需要准备设备来运行 Android 应用以进行开发。
¥Either way, you will need to prepare the device to run Android apps for development.
使用物理设备
如果你有物理 Android 设备,则可以使用它来代替 AVD 进行开发,方法是使用 USB 电缆将其插入计算机并按照说明进行操作 此处。
¥If you have a physical Android device, you can use it for development in place of an AVD by plugging it in to your computer using a USB cable and following the instructions here.
使用虚拟设备
如果你使用 Android Studio 打开 ./AwesomeProject/android
,则可以通过从 Android Studio 中打开 "AVD 管理器" 来查看可用 Android 虚拟设备 (AVD) 的列表。寻找如下所示的图标:
¥If you use Android Studio to open ./AwesomeProject/android
, you can see the list of available Android Virtual Devices (AVDs) by opening the "AVD Manager" from within Android Studio. Look for an icon that looks like this:
如果你最近安装了 Android Studio,则可能需要 创建一个新的 AVD。选择 "创建虚拟设备...",然后从列表中选择任何调用并单击 "下一个",然后选择 UpsideDownCake API 级别 34 图片。
¥If you have recently installed Android Studio, you will likely need to create a new AVD. Select "Create Virtual Device...", then pick any Phone from the list and click "Next", then select the UpsideDownCake API Level 34 image.
单击 "下一个",然后单击 "结束" 创建你的 AVD。此时,你应该能够单击 AVD 旁边的绿色三角形按钮来启动它。
¥Click "Next" then "Finish" to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it.
就是这样!
恭喜!你已成功设置开发环境。
¥Congratulations! You successfully set up your development environment.
怎么办?
-
如果你想将这个新的 React Native 代码添加到现有应用中,请查看 集成指南。
¥If you want to add this new React Native code to an existing application, check out the Integration guide.
-
如果你想了解有关 React Native 的更多信息,请查看 React Native 简介。
¥If you're curious to learn more about React Native, check out the Introduction to React Native.
安装依赖
¥Installing dependencies
你将需要 Node、Watchman、React Native 命令行接口、Xcode 和 CocoaPods。
¥You will need Node, Watchman, the React Native command line interface, Xcode and CocoaPods.
虽然你可以使用你选择的任何编辑器来开发应用,但你需要安装 Xcode 才能设置必要的工具来构建适用于 iOS 的 React Native 应用。
¥While you can use any editor of your choice to develop your app, you will need to install Xcode in order to set up the necessary tooling to build your React Native app for iOS.
节点与守望者
¥Node & Watchman
我们建议使用 Homebrew 安装 Node 和 Watchman。安装 Homebrew 后在终端中运行以下命令:
¥We recommend installing Node and Watchman using Homebrew. Run the following commands in a Terminal after installing Homebrew:
brew install node
brew install watchman
如果你的系统上已安装 Node,请确保它是 Node 18 或更高版本。
¥If you have already installed Node on your system, make sure it is Node 18 or newer.
守望者 是 Facebook 的一个工具,用于监视文件系统的变化。强烈建议你安装它以获得更好的性能。
¥Watchman is a tool by Facebook for watching changes in the filesystem. It is highly recommended you install it for better performance.
Xcode
请使用最新版本的 Xcode。
¥Please use the latest version of Xcode.
安装 Xcode 最简单的方法是通过 Mac 应用商店。安装 Xcode 还将安装 iOS 模拟器以及构建 iOS 应用所需的所有工具。
¥The easiest way to install Xcode is via the Mac App Store. Installing Xcode will also install the iOS Simulator and all the necessary tools to build your iOS app.
命令行工具
¥Command Line Tools
你还需要安装 Xcode 命令行工具。打开 Xcode,然后选择“设置”...(或首选项...)从 Xcode 菜单。转到“位置”面板并通过在“命令行工具”下拉列表中选择最新版本来安装工具。
¥You will also need to install the Xcode Command Line Tools. Open Xcode, then choose Settings... (or Preferences...) from the Xcode menu. Go to the Locations panel and install the tools by selecting the most recent version in the Command Line Tools dropdown.
在 Xcode 中安装 iOS 模拟器
¥Installing an iOS Simulator in Xcode
要安装模拟器,请打开 Xcode > 设置...(或首选项...)并选择平台(或组件)选项卡。选择与你要使用的 iOS 版本相对应的模拟器。
¥To install a simulator, open Xcode > Settings... (or Preferences...) and select the Platforms (or Components) tab. Select a simulator with the corresponding version of iOS you wish to use.
如果你使用 Xcode 14.0 或更高版本来安装模拟器,请打开 Xcode > 设置 > 平台选项卡,然后单击 "*" 图标并选择 iOS… 选项。
¥If you are using Xcode version 14.0 or greater than to install a simulator, open Xcode > Settings > Platforms tab, then click "+" icon and select iOS… option.
CocoaPods
CocoaPods 是适用于 iOS 的依赖管理系统之一。CocoaPods 是 Ruby gem。你可以使用最新版本 macOS 附带的 Ruby 版本来安装 CocoaPods。
¥CocoaPods is one of the dependency management system available for iOS. CocoaPods is a Ruby gem. You can install CocoaPods using the version of Ruby that ships with the latest version of macOS.
欲了解更多信息,请访问 CocoaPods 入门指南。
¥For more information, please visit CocoaPods Getting Started guide.
[可选] 配置你的环境
¥[Optional] Configuring your environment
从 React Native 0.69 版本开始,可以使用模板提供的 .xcode.env
文件来配置 Xcode 环境。
¥Starting from React Native version 0.69, it is possible to configure the Xcode environment using the .xcode.env
file provided by the template.
.xcode.env
文件包含一个环境变量,用于导出 NODE_BINARY
变量中 node
可执行文件的路径。这是将构建基础架构与 node
系统版本分离的建议方法。如果该变量与默认值不同,你应该使用你自己的路径或你自己的 node
版本管理器自定义此变量。
¥The .xcode.env
file contains an environment variable to export the path to the node
executable in the NODE_BINARY
variable.
This is the suggested approach to decouple the build infrastructure from the system version of node
. You should customize this variable with your own path or your own node
version manager, if it differs from the default.
除此之外,还可以添加任何其他环境变量并在构建脚本阶段获取 .xcode.env
文件。如果你需要运行需要某些特定环境的脚本,这是建议的方法:它允许将构建阶段与特定环境分离。
¥On top of this, it's possible to add any other environment variable and to source the .xcode.env
file in your build script phases. If you need to run script that requires some specific environment, this is the suggested approach: it allows to decouple the build phases from a specific environment.
如果你已经在使用 NVM(帮助你安装并在 Node.js 版本之间切换的命令)和 zsh,你可能需要将初始化 NVM 的代码从 ~/.zshrc
移动到 ~/.zshenv
文件中,以帮助 Xcode 找到你的 Node 可执行文件:
¥If you are already using NVM (a command which helps you install and switch between versions of Node.js) and zsh, you might want to move the code that initialize NVM from your ~/.zshrc
into a ~/.zshenv
file to help Xcode find your Node executable:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
你可能还想确保 Xcode 项目的所有 "shell 脚本构建阶段" 都使用 /bin/zsh
作为其 shell。
¥You might also want to ensure that all "shell script build phase" of your Xcode project, is using /bin/zsh
as its shell.
就是这样!
恭喜!你已成功设置开发环境。
¥Congratulations! You successfully set up your development environment.
怎么办?
-
如果你想将这个新的 React Native 代码添加到现有应用中,请查看 集成指南。
¥If you want to add this new React Native code to an existing application, check out the Integration guide.
-
如果你想了解有关 React Native 的更多信息,请查看 React Native 简介。
¥If you're curious to learn more about React Native, check out the Introduction to React Native.
目标操作系统
¥Target OS
- Android
- iOS
安装依赖
你将需要 Node、React Native 命令行接口、JDK 和 Android Studio。
¥You will need Node, the React Native command line interface, a JDK, and Android Studio.
虽然你可以使用你选择的任何编辑器来开发应用,但你需要安装 Android Studio 才能设置必要的工具来构建适用于 Android 的 React Native 应用。
¥While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.
节点、JDK
我们建议通过 Chocolatey 安装 Node,Chocolatey 是一种流行的 Windows 包管理器。
¥We recommend installing Node via Chocolatey, a popular package manager for Windows.
建议使用 Node.js 的 LTS 版本。如果你希望能够在不同版本之间切换,你可能需要通过 nvm-windows(适用于 Windows 的 Node 版本管理器)安装 Node。
¥It is recommended to use an LTS version of Node. If you want to be able to switch between different versions, you might want to install Node via nvm-windows, a Node version manager for Windows.
React Native 还需要 Java SE 开发工具包 (JDK),也可以使用 Chocolatey 安装。
¥React Native also requires Java SE Development Kit (JDK), which can be installed using Chocolatey as well.
打开管理员命令提示符(右键单击命令提示符并选择 "以管理员身份运行"),然后运行以下命令:
¥Open an Administrator Command Prompt (right click Command Prompt and select "Run as Administrator"), then run the following command:
choco install -y nodejs-lts microsoft-openjdk17
如果你的系统上已安装 Node,请确保它是 Node 18 或更高版本。如果你的系统上已有 JDK,我们建议使用 JDK17。使用更高版本的 JDK 可能会遇到问题。
¥If you have already installed Node on your system, make sure it is Node 18 or newer. If you already have a JDK on your system, we recommend JDK17. You may encounter problems using higher JDK versions.
你可以在 Node 的下载页面 上找到其他安装选项。
¥You can find additional installation options on Node's Downloads page.
如果你使用的是最新版本的 Java Development Kit,则需要更改项目的 Gradle 版本,以便它可以识别 JDK。你可以通过转到
{project root folder}\android\gradle\wrapper\gradle-wrapper.properties
并更改distributionUrl
值来升级 Gradle 版本来完成此操作。你可以看看 这里是 Gradle 的最新版本。¥If you're using the latest version of Java Development Kit, you'll need to change the Gradle version of your project so it can recognize the JDK. You can do that by going to
{project root folder}\android\gradle\wrapper\gradle-wrapper.properties
and changing thedistributionUrl
value to upgrade the Gradle version. You can check out here the latest releases of Gradle.
安卓开发环境
如果你是 Android 开发新手,那么设置开发环境可能会有些乏味。如果你已经熟悉 Android 开发,则可能需要配置一些内容。无论哪种情况,请务必仔细执行接下来的几个步骤。
¥Setting up your development environment can be somewhat tedious if you're new to Android development. If you're already familiar with Android development, there are a few things you may need to configure. In either case, please make sure to carefully follow the next few steps.
1. 安装 Android Studio
下载并安装 Android Studio。在 Android Studio 安装向导中,确保选中以下所有项目旁边的框:
¥Download and install Android Studio. While on Android Studio installation wizard, make sure the boxes next to all of the following items are checked:
-
Android SDK
-
Android SDK Platform
-
Android Virtual Device
-
如果你尚未使用 Hyper-V:
Performance (Intel ® HAXM)
(请参阅此处了解 AMD 或 Hyper-V)¥If you are not already using Hyper-V:
Performance (Intel ® HAXM)
(See here for AMD or Hyper-V)
然后,单击 "下一个" 安装所有这些组件。
¥Then, click "Next" to install all of these components.
如果复选框呈灰色,你稍后将有机会安装这些组件。
¥If the checkboxes are grayed out, you will have a chance to install these components later on.
设置完成并且你将看到欢迎屏幕后,请继续执行下一步。
¥Once setup has finalized and you're presented with the Welcome screen, proceed to the next step.
2. 安装 Android SDK
Android Studio 默认安装最新的 Android SDK。然而,使用原生代码构建 React Native 应用特别需要 Android 14 (UpsideDownCake)
SDK。可以通过 Android Studio 中的 SDK 管理器安装其他 Android SDK。
¥Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 14 (UpsideDownCake)
SDK in particular. Additional Android SDKs can be installed through the SDK Manager in Android Studio.
为此,请打开 Android Studio,单击 "更多操作" 按钮并选择 "SDK 管理器"。
¥To do that, open Android Studio, click on "More Actions" button and select "SDK Manager".
SDK 管理器还可以在 Android Studio "Settings" 对话框中的语言和框架 → Android SDK 下找到。
¥The SDK Manager can also be found within the Android Studio "Settings" dialog, under Languages & Frameworks → Android SDK.
从 SDK Manager 中选择 "SDK 平台" 选项卡,然后选中右下角 "显示包详情" 旁边的框。查找并展开 Android 14 (UpsideDownCake)
条目,然后确保选中以下项目:
¥Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 14 (UpsideDownCake)
entry, then make sure the following items are checked:
-
Android SDK Platform 34
-
Intel x86 Atom_64 System Image
或Google APIs Intel x86 Atom System Image
¥
Intel x86 Atom_64 System Image
orGoogle APIs Intel x86 Atom System Image
接下来,选择 "SDK 工具" 选项卡并选中 "显示包详情" 旁边的框。查找并展开 Android SDK Build-Tools
条目,然后确保选择 34.0.0
。
¥Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the Android SDK Build-Tools
entry, then make sure that 34.0.0
is selected.
最后点击 "应用" 下载并安装 Android SDK 及相关构建工具。
¥Finally, click "Apply" to download and install the Android SDK and related build tools.
3. 配置 ANDROID_HOME 环境变量
React Native 工具需要设置一些环境变量才能使用原生代码构建应用。
¥The React Native tools require some environment variables to be set up in order to build apps with native code.
-
打开 Windows 控制面板。
¥Open the Windows Control Panel.
-
单击“用户账户”,然后再次单击“用户账户”
¥Click on User Accounts, then click User Accounts again
-
单击更改我的环境变量
¥Click on Change my environment variables
-
单击新建...创建一个新的
ANDROID_HOME
用户变量,该变量指向 Android SDK 的路径:¥Click on New... to create a new
ANDROID_HOME
user variable that points to the path to your Android SDK:
默认情况下,SDK 安装在以下位置:
¥The SDK is installed, by default, at the following location:
%LOCALAPPDATA%\Android\Sdk
你可以在 Android Studio "Settings" 对话框中的语言和框架 → Android SDK 下找到 SDK 的实际位置。
¥You can find the actual location of the SDK in the Android Studio "Settings" dialog, under Languages & Frameworks → Android SDK.
打开新的命令提示符窗口以确保在继续下一步之前加载新的环境变量。
¥Open a new Command Prompt window to ensure the new environment variable is loaded before proceeding to the next step.
-
打开 powershell
¥Open powershell
-
将 Get-ChildItem -Path Env:\ 复制并粘贴到 powershell 中
¥Copy and paste *Get-ChildItem -Path Env:* into powershell
-
验证
ANDROID_HOME
已添加¥Verify
ANDROID_HOME
has been added
4. 将平台工具添加到路径
-
打开 Windows 控制面板。
¥Open the Windows Control Panel.
-
单击“用户账户”,然后再次单击“用户账户”
¥Click on User Accounts, then click User Accounts again
-
单击更改我的环境变量
¥Click on Change my environment variables
-
选择路径变量。
¥Select the Path variable.
-
单击编辑。
¥Click Edit.
-
单击新建并将平台工具的路径添加到列表中。
¥Click New and add the path to platform-tools to the list.
该文件夹的默认位置是:
¥The default location for this folder is:
%LOCALAPPDATA%\Android\Sdk\platform-tools
准备 Android 设备
你将需要一台 Android 设备来运行 React Native Android 应用。这可以是物理 Android 设备,或更常见的是,你可以使用 Android 虚拟设备,它允许你在计算机上模拟 Android 设备。
¥You will need an Android device to run your React Native Android app. This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer.
无论哪种方式,你都需要准备设备来运行 Android 应用以进行开发。
¥Either way, you will need to prepare the device to run Android apps for development.
使用物理设备
如果你有物理 Android 设备,则可以使用它来代替 AVD 进行开发,方法是使用 USB 电缆将其插入计算机并按照说明进行操作 此处。
¥If you have a physical Android device, you can use it for development in place of an AVD by plugging it in to your computer using a USB cable and following the instructions here.
使用虚拟设备
如果你使用 Android Studio 打开 ./AwesomeProject/android
,则可以通过从 Android Studio 中打开 "AVD 管理器" 来查看可用 Android 虚拟设备 (AVD) 的列表。寻找如下所示的图标:
¥If you use Android Studio to open ./AwesomeProject/android
, you can see the list of available Android Virtual Devices (AVDs) by opening the "AVD Manager" from within Android Studio. Look for an icon that looks like this:
如果你最近安装了 Android Studio,则可能需要 创建一个新的 AVD。选择 "创建虚拟设备...",然后从列表中选择任何调用并单击 "下一个",然后选择 UpsideDownCake API 级别 34 图片。
¥If you have recently installed Android Studio, you will likely need to create a new AVD. Select "Create Virtual Device...", then pick any Phone from the list and click "Next", then select the UpsideDownCake API Level 34 image.
如果你没有安装 HAXM,请单击 "安装 HAXM" 或按照 这些说明 进行设置,然后返回 AVD Manager。
¥If you don't have HAXM installed, click on "Install HAXM" or follow these instructions to set it up, then go back to the AVD Manager.
单击 "下一个",然后单击 "结束" 创建你的 AVD。此时,你应该能够单击 AVD 旁边的绿色三角形按钮来启动它。
¥Click "Next" then "Finish" to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it.
就是这样!
恭喜!你已成功设置开发环境。
¥Congratulations! You successfully set up your development environment.
怎么办?
-
如果你想将这个新的 React Native 代码添加到现有应用中,请查看 集成指南。
¥If you want to add this new React Native code to an existing application, check out the Integration guide.
-
如果你想了解有关 React Native 的更多信息,请查看 React Native 简介。
¥If you're curious to learn more about React Native, check out the Introduction to React Native.
目标操作系统
¥Target OS
- Android
- iOS
安装依赖
¥Installing dependencies
你将需要 Node、React Native 命令行接口、JDK 和 Android Studio。
¥You will need Node, the React Native command line interface, a JDK, and Android Studio.
虽然你可以使用你选择的任何编辑器来开发应用,但你需要安装 Android Studio 才能设置必要的工具来构建适用于 Android 的 React Native 应用。
¥While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.
节点
按照 适用于你的 Linux 发行版的安装说明 安装 Node 18 或更高版本。
¥Follow the installation instructions for your Linux distribution to install Node 18 or newer.
Java 开发套件
React Native 目前推荐使用 Java SE 开发工具包 (JDK) 版本 17。使用更高版本的 JDK 可能会遇到问题。你可以从 AdoptOpenJDK 或你的系统打包程序下载并安装 OpenJDK。
¥React Native currently recommends version 17 of the Java SE Development Kit (JDK). You may encounter problems using higher JDK versions. You may download and install OpenJDK from AdoptOpenJDK or your system packager.
安卓开发环境
如果你是 Android 开发新手,那么设置开发环境可能会有些乏味。如果你已经熟悉 Android 开发,则可能需要配置一些内容。无论哪种情况,请务必仔细执行接下来的几个步骤。
¥Setting up your development environment can be somewhat tedious if you're new to Android development. If you're already familiar with Android development, there are a few things you may need to configure. In either case, please make sure to carefully follow the next few steps.
1. 安装 Android Studio
下载并安装 Android Studio。在 Android Studio 安装向导中,确保选中以下所有项目旁边的框:
¥Download and install Android Studio. While on Android Studio installation wizard, make sure the boxes next to all of the following items are checked:
-
Android SDK
-
Android SDK Platform
-
Android Virtual Device
然后,单击 "下一个" 安装所有这些组件。
¥Then, click "Next" to install all of these components.
如果复选框呈灰色,你稍后将有机会安装这些组件。
¥If the checkboxes are grayed out, you will have a chance to install these components later on.
设置完成并且你将看到欢迎屏幕后,请继续执行下一步。
¥Once setup has finalized and you're presented with the Welcome screen, proceed to the next step.
2. 安装 Android SDK
Android Studio 默认安装最新的 Android SDK。然而,使用原生代码构建 React Native 应用特别需要 Android 14 (UpsideDownCake)
SDK。可以通过 Android Studio 中的 SDK 管理器安装其他 Android SDK。
¥Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 14 (UpsideDownCake)
SDK in particular. Additional Android SDKs can be installed through the SDK Manager in Android Studio.
为此,请打开 Android Studio,单击 "配置" 按钮并选择 "SDK 管理器"。
¥To do that, open Android Studio, click on "Configure" button and select "SDK Manager".
SDK 管理器还可以在 Android Studio "Settings" 对话框中的语言和框架 → Android SDK 下找到。
¥The SDK Manager can also be found within the Android Studio "Settings" dialog, under Languages & Frameworks → Android SDK.
从 SDK Manager 中选择 "SDK 平台" 选项卡,然后选中右下角 "显示包详情" 旁边的框。查找并展开 Android 14 (UpsideDownCake)
条目,然后确保选中以下项目:
¥Select the "SDK Platforms" tab from within the SDK Manager, then check the box next to "Show Package Details" in the bottom right corner. Look for and expand the Android 14 (UpsideDownCake)
entry, then make sure the following items are checked:
-
Android SDK Platform 34
-
Intel x86 Atom_64 System Image
或Google APIs Intel x86 Atom System Image
¥
Intel x86 Atom_64 System Image
orGoogle APIs Intel x86 Atom System Image
接下来,选择 "SDK 工具" 选项卡并选中 "显示包详情" 旁边的框。查找并展开 "Android SDK 构建工具" 条目,然后确保选择 34.0.0
。
¥Next, select the "SDK Tools" tab and check the box next to "Show Package Details" here as well. Look for and expand the "Android SDK Build-Tools" entry, then make sure that 34.0.0
is selected.
最后点击 "应用" 下载并安装 Android SDK 及相关构建工具。
¥Finally, click "Apply" to download and install the Android SDK and related build tools.
3. 配置 ANDROID_HOME 环境变量
React Native 工具需要设置一些环境变量才能使用原生代码构建应用。
¥The React Native tools require some environment variables to be set up in order to build apps with native code.
将以下行添加到 $HOME/.bash_profile
或 $HOME/.bashrc
(如果你使用 zsh
,则使用 ~/.zprofile
或 ~/.zshrc
)配置文件:
¥Add the following lines to your $HOME/.bash_profile
or $HOME/.bashrc
(if you are using zsh
then ~/.zprofile
or ~/.zshrc
) config file:
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/platform-tools
.bash_profile
特定于bash
。如果你使用其他 shell,则需要编辑相应的特定于 shell 的配置文件。¥
.bash_profile
is specific tobash
. If you're using another shell, you will need to edit the appropriate shell-specific config file.
键入 source $HOME/.bash_profile
代表 bash
或 source $HOME/.zprofile
,将配置加载到当前 shell 中。验证 ANDROID_HOME 是否已通过运行 echo $ANDROID_HOME
设置,并且相应的目录已通过运行 echo $PATH
添加到你的路径中。
¥Type source $HOME/.bash_profile
for bash
or source $HOME/.zprofile
to load the config into your current shell. Verify that ANDROID_HOME has been set by running echo $ANDROID_HOME
and the appropriate directories have been added to your path by running echo $PATH
.
请确保你使用正确的 Android SDK 路径。你可以在 Android Studio "Settings" 对话框中的语言和框架 → Android SDK 下找到 SDK 的实际位置。
¥Please make sure you use the correct Android SDK path. You can find the actual location of the SDK in the Android Studio "Settings" dialog, under Languages & Frameworks → Android SDK.
守望者
按照 守望者安装指南 从源码编译并安装 Watchman。
¥Follow the Watchman installation guide to compile and install Watchman from source.
守望者 是 Facebook 的一个工具,用于监视文件系统的变化。强烈建议你安装它,以获得更好的性能并提高某些边缘情况下的兼容性(翻译:你可能可以在不安装它的情况下完成,但你的里程可能会有所不同;现在安装它可能会让你免于以后的头痛)。
¥Watchman is a tool by Facebook for watching changes in the filesystem. It is highly recommended you install it for better performance and increased compatibility in certain edge cases (translation: you may be able to get by without installing this, but your mileage may vary; installing this now may save you from a headache later).
准备 Android 设备
你将需要一台 Android 设备来运行 React Native Android 应用。这可以是物理 Android 设备,或更常见的是,你可以使用 Android 虚拟设备,它允许你在计算机上模拟 Android 设备。
¥You will need an Android device to run your React Native Android app. This can be either a physical Android device, or more commonly, you can use an Android Virtual Device which allows you to emulate an Android device on your computer.
无论哪种方式,你都需要准备设备来运行 Android 应用以进行开发。
¥Either way, you will need to prepare the device to run Android apps for development.
使用物理设备
如果你有物理 Android 设备,则可以使用它来代替 AVD 进行开发,方法是使用 USB 电缆将其插入计算机并按照说明进行操作 此处。
¥If you have a physical Android device, you can use it for development in place of an AVD by plugging it in to your computer using a USB cable and following the instructions here.
使用虚拟设备
如果你使用 Android Studio 打开 ./AwesomeProject/android
,则可以通过从 Android Studio 中打开 "AVD 管理器" 来查看可用 Android 虚拟设备 (AVD) 的列表。寻找如下所示的图标:
¥If you use Android Studio to open ./AwesomeProject/android
, you can see the list of available Android Virtual Devices (AVDs) by opening the "AVD Manager" from within Android Studio. Look for an icon that looks like this:
如果你最近安装了 Android Studio,则可能需要 创建一个新的 AVD。选择 "创建虚拟设备...",然后从列表中选择任何调用并单击 "下一个",然后选择 UpsideDownCake API 级别 34 图片。
¥If you have recently installed Android Studio, you will likely need to create a new AVD. Select "Create Virtual Device...", then pick any Phone from the list and click "Next", then select the UpsideDownCake API Level 34 image.
我们建议在你的系统上配置 虚拟机加速 以提高性能。按照这些说明操作后,请返回 AVD 管理器。
¥We recommend configuring VM acceleration on your system to improve performance. Once you've followed those instructions, go back to the AVD Manager.
单击 "下一个",然后单击 "结束" 创建你的 AVD。此时,你应该能够单击 AVD 旁边的绿色三角形按钮来启动它。
¥Click "Next" then "Finish" to create your AVD. At this point you should be able to click on the green triangle button next to your AVD to launch it.
就是这样!
恭喜!你已成功设置开发环境。
¥Congratulations! You successfully set up your development environment.
怎么办?
-
如果你想将这个新的 React Native 代码添加到现有应用中,请查看 集成指南。
¥If you want to add this new React Native code to an existing application, check out the Integration guide.
-
如果你想了解有关 React Native 的更多信息,请查看 React Native 简介。
¥If you're curious to learn more about React Native, check out the Introduction to React Native.