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.
安装 Watchman 和 JDK
🌐 Install Watchman and JDK
先决条件
🌐 Prerequisites
使用像 Homebrew 这样的包管理器来安装以下依赖。
🌐 Use a package manager such as Homebrew to install the following dependency.
安装依赖
🌐 Install dependencies
1
使用 Homebrew 等工具安装 Watchman:
- brew install watchman2
使用 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:
- 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
先决条件
🌐 Prerequisites
使用像 Chocolatey 这样的包管理器来安装以下依赖。
🌐 Use a package manager such as Chocolatey to install the following dependencies.
安装依赖
🌐 Install dependencies
安装 Java SE 开发工具包 (JDK) :
🌐 Install Java SE Development Kit (JDK):
- choco install -y microsoft-openjdk17安装依赖
🌐 Install dependencies
1
按照 Watchman 文档 中的说明从源代码编译并安装它。
🌐 Follow instructions from the Watchman documentation to compile and install it from the source.
2
安装 Java SE 开发工具包 (JDK) :
🌐 Install Java SE Development Kit (JDK):
你可以从 AdoptOpenJDK 下载并安装 OpenJDK@17,或者通过你的系统包管理器进行安装。
🌐 You can download and install OpenJDK@17 from AdoptOpenJDK or your system packager.
安装 Android Studio
🌐 Set up Android Studio
1
下载并安装 Android Studio。
🌐 Download and install Android Studio.
2
打开 Android Studio 应用,你将看到 SDK 组件设置 屏幕。点击 下一步 继续安装 Android SDK 和 Android SDK 平台。再次点击 下一步 以验证设置并安装。
🌐 Open the Android Studio app, you will see the SDK Components setup screen. Click Next to continue to install the Android SDK and Android SDK Platform. Click Next again to verify the settings and install.
3
默认情况下,Android Studio 会安装最新版本的 Android SDK。但编译 React Native 应用需要 Android 15 (VanillaIceCream) SDK。
🌐 By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (VanillaIceCream) SDK is required to compile a React Native app.
打开 Android Studio,进入 设置 > 语言与框架 > Android SDK。在 SDK 平台 选项卡中,在 Android 15 (VanillaIceCream) 下,选择 Android SDK Platform 35 和 Android 35 源码。
🌐 Open Android Studio, go to Settings > Languages & Frameworks > Android SDK. From the SDK Platforms tab, and under Android 15 (VanillaIceCream), select Android SDK Platform 35 and Sources for Android 35.
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:
- export ANDROID_HOME=$HOME/Library/Android/sdk- export PATH=$PATH:$ANDROID_HOME/emulator- export PATH=$PATH:$ANDROID_HOME/platform-tools7
在当前终端中重新加载路径环境变量:
🌐 Reload the path environment variables in your current shell:
# for zsh- source $HOME/.zshrc# for bash- source $HOME/.bashrc8
最后,确保你可以在终端中运行 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:
-
在你的主目录中创建一个 Gradle 属性文件:
Terminal-touch ~/.gradle/gradle.properties -
将以下行添加到 gradle.properties 文件中,并将路径替换为你实际的 Java 安装路径:
gradle.propertiesjava.home=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home -
如果你的项目目录中已有
.gradle文件夹,请将其删除并在 Android Studio 中重新打开你的项目:Terminal-rm -rf .gradle
这应该可以解决 Android Studio 无法检测到你的 JDK 安装的问题。
🌐 This should resolve issues with Android Studio not detecting your JDK installation.
1
下载 Android Studio。
🌐 Download Android Studio.
2
打开 Android Studio 安装程序。在 选择要安装的组件 下,选择 Android Studio 和 Android 虚拟设备。然后,点击 下一步。
🌐 Open Android Studio Setup. Under Select components to install, select Android Studio and Android Virtual Device. Then, click Next.
3
在 Android Studio 设置向导中,在 安装类型 下,选择 标准,然后点击 下一步。
🌐 In the Android Studio Setup Wizard, under Install Type, select Standard and click Next.
4
Android Studio 安装向导将要求你确认设置,例如 Android SDK 版本、平台工具等。确认无误后点击 下一步。
🌐 The Android Studio Setup Wizard will ask you to verify the settings, such as the version of Android SDK, platform-tools, and so on. Click Next after you have verified.
5
在下一个窗口中,接受所有可用组件的许可。
🌐 In the next window, accept licenses for all available components.
6
默认情况下,Android Studio 会安装最新版本的 Android SDK。但编译 React Native 应用需要 Android 15 (VanillaIceCream) SDK。
🌐 By default, Android Studio will install the latest version of the Android SDK. However, Android 15 (VanillaIceCream) SDK is required to compile a React Native app.
打开 Android Studio,进入 设置 > 语言与框架 > Android SDK。在 SDK 平台 选项卡中,在 Android 15 (VanillaIceCream) 下,选择 Android SDK Platform 35 和 Android 35 源码。
🌐 Open Android Studio, go to Settings > Languages & Frameworks > Android SDK. From the SDK Platforms tab, and under Android 15 (VanillaIceCream), select Android SDK Platform 35 and Sources for Android 35.
7
然后,点击 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.
8
工具安装完成后,配置 ANDROID_HOME 环境变量。进入 Windows 控制面板 > 用户账户 > 用户账户(再次点击)> 更改我的环境变量,然后点击 新建 来创建一个新的 ANDROID_HOME 用户变量。该变量的值应指向你的 Android SDK 路径:
🌐 After the tools installation is complete, configure the ANDROID_HOME environment variable. Go to Windows Control Panel > User Accounts > User Accounts (again) > Change my environment variables and click New to create a new ANDROID_HOME user variable. The value of this variable will point to the path to your Android SDK:
如何找到已安装的 SDK 位置?
默认情况下,Android SDK 安装在以下位置:
🌐 By default, the Android SDK is installed at the following location:
%LOCALAPPDATA%\Android\Sdk
要在 Android Studio 中手动查找 SDK 的位置,请转到 设置 > 语言与框架 > Android SDK。在 Android SDK 位置 旁边可以看到其路径。
🌐 To find the location of the SDK in Android Studio manually, go to Settings > Languages & Frameworks > Android SDK. See the location next to Android SDK Location.
9
要验证新的环境变量是否已加载,打开 PowerShell,然后复制并粘贴以下命令:
🌐 To verify that the new environment variable is loaded, open PowerShell, and copy and paste the following command:
- Get-ChildItem -Path Env:该命令将输出所有用户环境变量。在此列表中,查看是否已添加 ANDROID_HOME。
🌐 The command will output all user environment variables. In this list, see if ANDROID_HOME has been added.
10
要将 platform-tools 添加到路径中,请转到 Windows 控制面板 > 用户账户 > 用户账户(再次)> 更改我的环境变量 > Path > 编辑 > 新建,然后将 platform-tools 的路径添加到列表中,如下所示:
🌐 To add platform-tools to the Path, go to Windows Control Panel > User Accounts > User Accounts (again) > Change my environment variables > Path > Edit > New and add the path to the platform-tools to the list as shown below:
11
最后,确保你可以从 PowerShell 运行 adb。例如,运行 adb --version 来查看你的系统正在运行哪个版本的 adb。
🌐 Finally, make sure that you can run adb from the PowerShell. For example, run the adb --version to see which version of the adb your system is running.
设置模拟器
🌐 Set up an emulator
1
在 Android Studio 主界面上,点击 更多操作,然后在下拉菜单中选择 虚拟设备管理器。
2
点击 创建设备 按钮。
🌐 Click the Create device button.
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
更改你想要的其他设置,然后按 完成 创建模拟器。你现在可以随时通过在 AVD 管理器窗口中按播放按钮来运行此模拟器。
🌐 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 AVD Manager window.
故障排除
🌐 Troubleshooting
多个 adb 版本
🌐 Multiple adb versions
在你的系统上拥有多个 adb 版本可能导致以下错误:
🌐 Having multiple adb versions on your system can result in the following error:
- 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 版本:
- adb version2
然后在 Android SDK 平台工具目录下:
- cd ~/Library/Android/sdk/platform-tools- ./adb version3
将 adb 从 Android SDK 目录复制到 usr/bin 目录:
- sudo cp ~/Library/Android/sdk/platform-tools/adb /usr/bin