This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Android Studio 模拟器

了解如何设置 Android 模拟器以在虚拟 Android 设备上测试你的应用。


如果你没有可用的安卓设备进行测试,我们建议使用随 Android Studio 提供的默认模拟器。如果在设置过程中遇到任何问题,请按照本指南中的步骤操作。

🌐 If you don't have an Android device available to test with, we recommend using the default emulator that comes with Android Studio. If you run into any problems setting it up, follow the steps in this guide.

安装 JDK

🌐 Install JDK

先决条件

🌐 Prerequisites

使用像 Homebrew 这样的包管理器来安装以下依赖。

🌐 Use a package manager such as Homebrew to install the following dependency.

安装依赖

🌐 Install dependencies

1

信息 仅在 SDK 55 及更早版本的项目中才需要安装 Watchman。

使用 Homebrew 等工具安装 Watchman

Terminal
brew install watchman

2

使用 Homebrew 安装名为 Azul Zulu 的 OpenJDK 版本。这个发行版提供适用于 Apple Silicon 和 Intel Mac 的 JDK。

🌐 Install OpenJDK distribution called Azul Zulu using Homebrew. This distribution offers JDKs for both Apple Silicon and Intel Macs.

在终端中运行以下命令:

🌐 Run the following commands in a terminal:

Terminal
brew install --cask zulu@17

在安装 JDK 后,在 ~/.bash_profile(如果使用 Zsh,则为 ~/.zshrc)中添加 JAVA_HOME 环境变量:

🌐 After you install the JDK, add the JAVA_HOME environment variable in ~/.bash_profile (or ~/.zshrc if you use Zsh):

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home

安装 Android Studio

🌐 Set up Android Studio

1

下载并安装 Android Studio

🌐 Download and install Android Studio.

2

打开 Android Studio 应用。首次启动时,会出现 Android Studio 设置向导。在 欢迎 屏幕上点击 下一步。然后,在 安装类型 下,选择 标准 并点击 下一步

🌐 Open the Android Studio app. On the first launch, the Android Studio Setup Wizard appears. Click Next on the Welcome screen. Then, under Install Type, select Standard and click Next.

验证设置并点击 下一步。然后,接受许可协议并再次点击 下一步。向导将下载并安装 Android SDK 及其工具。安装完成后点击 完成

🌐 Verify the settings and click Next. Then, accept the license agreement and click Next again. The wizard downloads and installs the Android SDK and its tools. Click Finish when the installation completes.

3

默认情况下,Android Studio 会安装最新版本的 Android SDK。然而,编译 React Native 应用需要 Android 16 (Baklava) SDK。

🌐 By default, Android Studio will install the latest version of the Android SDK. However, Android 16 (Baklava) SDK is required to compile a React Native app.

打开 Android Studio,进入 设置 > 语言与框架 > Android SDK。在 SDK 平台 选项卡中,在 Android 16 (Baklava) 下,选择 Android SDK 平台 36Android 36 源码

🌐 Open Android Studio, go to Settings > Languages & Frameworks > Android SDK. From the SDK Platforms tab, and under Android 16 (Baklava), select Android SDK Platform 36 and Sources for Android 36.

4

然后,点击 SDK 工具 标签,并确保你至少安装了一个版本的 Android SDK 构建工具Android 模拟器

🌐 Then, click on the SDK Tools tab and make sure you have at least one version of the Android SDK Build-Tools and Android Emulator installed.

5

复制或记下显示为 Android SDK 位置 的框中的路径。

🌐 Copy or remember the path listed in the box that says Android SDK Location.

6

将以下行添加到你的 /.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:

Terminal
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/platform-tools

7

在当前终端中重新加载路径环境变量:

🌐 Reload the path environment variables in your current shell:

Terminal
# for zsh
source $HOME/.zshrc

# for bash
source $HOME/.bashrc

8

最后,确保你可以在终端中运行 adb

🌐 Finally, make sure that you can run adb from your terminal.

故障排除:Android Studio无法识别JDK

如果 Android Studio 无法识别你通过 Homebrew 安装的 JDK,你可以创建一个 Gradle 配置文件来显式设置 Java 路径:

🌐 If Android Studio doesn't recognize your homebrew installed JDK, you can create a Gradle configuration file to explicitly set the Java path:

  1. 在你的主目录中创建一个 Gradle 属性文件:

    Terminal
    touch ~/.gradle/gradle.properties
  2. 将以下行添加到 gradle.properties 文件中,并将路径替换为你实际的 Java 安装路径:

    gradle.properties
    java.home=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
  3. 如果你的项目目录中已有 .gradle 文件夹,请将其删除并在 Android Studio 中重新打开你的项目:

    Terminal
    rm -rf .gradle

这应该可以解决 Android Studio 无法检测到你的 JDK 安装的问题。

🌐 This should resolve issues with Android Studio not detecting your JDK installation.

设置模拟器

🌐 Set up an emulator

1

在 Android Studio 主界面上,点击 更多操作,然后在下拉菜单中选择 虚拟设备管理器

2

点击 创建虚拟设备

🌐 Click Create virtual device.

3

添加设备 下,选择你想要模拟的硬件类型。我们建议在多种设备上进行测试,但如果你不知道从哪里开始,Pixel 系列中最新的设备可能是一个不错的选择。

🌐 Under Add device, choose the type of hardware you'd like to emulate. We recommend testing against a variety of devices, but if you're unsure where to start, the newest device in the Pixel line could be a good choice.

4

选择一个要在模拟器上加载的操作系统版本(可能是其中一个系统镜像),并下载该镜像(如果需要)。

🌐 Select an OS version to load on the emulator (probably one of the system images), and download the image (if required).

5

更改你想要的其他设置,然后按 完成 创建模拟器。你现在可以通过在 设备管理器 窗口中按播放按钮随时运行此模拟器。

🌐 Change any other settings you'd like, and press Finish to create the emulator. You can now run this emulator anytime by pressing the Play button in the Device Manager window.

故障排除

🌐 Troubleshooting

多个 adb 版本

🌐 Multiple adb versions

在你的系统上拥有多个 adb 版本可能导致以下错误:

🌐 Having multiple adb versions on your system can result in the following error:

Terminal
adb server version (xx) doesn't match this client (xx); killing...

这是因为你系统上的 adb 版本与 Android SDK 平台工具上的 adb 版本不同。

🌐 This is because the adb version on your system is different from the adb version on the Android SDK platform-tools.

1

打开终端并检查系统上的 adb 版本:

Terminal
adb version

2

然后在 Android SDK 平台工具目录下:

Terminal
cd ~/Library/Android/sdk/platform-tools
./adb version

3

adb 从 Android SDK 目录复制到 usr/bin 目录:

Terminal
sudo cp ~/Library/Android/sdk/platform-tools/adb /usr/bin

我如何安装特定版本的 Expo Go?

🌐 How do I install a specific version of Expo Go?

你可以创建一个具有所需 SDK 版本的项目,并在模拟器中打开它以安装匹配版本的 Expo Go。

🌐 You can create a project with the desired SDK version and open it in a simulator to install the matching version of Expo Go.

Terminal
# Bootstrap an SDK 57 project
npx create-expo-app --template blank@57

# Open the app on a simulator to install the required Expo Go app
npx expo start --android

或者,你可以通过 expo-go CLI 传递 SDK 版本来下载特定版本的 Expo Go,或者使用 latest 获取最新的 SDK 版本。此命令会将 Expo Go 应用下载到当前目录,并缓存到 ~/.expo 下。

🌐 Alternatively, you can download a specific version of Expo Go with the expo-go CLI by passing an SDK version, or use latest for the latest SDK version. This command downloads the Expo Go app to the current directory and caches it under ~/.expo.

Terminal
npx expo-go download android latest