首页指南参考教程

Android Studio 模拟器

了解如何设置 Android 模拟器以在虚拟 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.

设置 Android Studio 的工具

¥Set up Android Studio's tools

1

下载并安装 安卓工作室

¥Download and install Android Studio.

2

打开 Android Studio 应用,单击“更多操作”并选择“SDK 管理器”。

¥Open the Android Studio app, click More Actions and select SDK Manager.

3

打开 Android Studio,进入设置>语言和框架>Android SDK。从 SDK Platforms 选项卡中,选择最新的 Android 版本(API 级别)。

¥Open Android Studio, go to Settings > Languages & Frameworks > Android SDK. From the SDK Platforms tab, select the latest Android version (API level).

然后,单击“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.

4

复制或记住 Android SDK 位置框中列出的路径。

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

5

单击“应用”和“确定”以安装 Android SDK 和相关构建工具。

¥Click Apply and OK to install the Android SDK and related build tools.

6

如果你使用的是 macOS 或 Linux,请在 ~/.bash_profile(如果使用 Zsh,则为 ~/.zshrc)中添加指向 Android SDK 位置的 环境变量。例如:export ANDROID_HOME=/your/path/here

¥If you are on macOS or Linux, add an environment variable pointing to the Android SDK location in ~/.bash_profile (or ~/.zshrc if you use Zsh). For example: export ANDROID_HOME=/your/path/here.

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

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

¥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.

1

下载 安卓工作室

¥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_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:

How to find installed SDK location

默认情况下,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.

7

要验证是否已加载新环境变量,请打开 PowerShell,然后复制并粘贴以下命令:

¥To verify that the new environment variable is loaded, open PowerShell, and copy and paste the following command:

Terminal
Get-ChildItem -Path Env:

该命令将输出所有用户环境变量。在此列表中,查看是否已添加 ANDROID_HOME

¥The command will output all user environment variables. In this list, see if ANDROID_HOME has been added.

8

要将平台工具添加到路径,请转到 Windows 控制面板 > 用户账户 > 用户账户(再次)> 更改我的环境变量 > 路径 > 编辑 > 新建,并将平台工具的路径添加到列表中,如下所示:

¥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:

How to find installed platform-tools location

默认情况下,平台工具安装在以下位置:

¥By default, the platform-tools are installed at the following location:

%LOCALAPPDATA%\Android\Sdk\platform-tools

9

最后,确保你可以从 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 a virtual device

1

在 Android Studio 主屏幕上,单击“更多操作”,然后单击下拉列表中的“虚拟设备管理器”。

¥On the Android Studio main screen, click More Actions, then Virtual Device Manager in the dropdown.

2

单击创建设备按钮。

¥Click the Create device button.

3

在“选择硬件”下,选择你想要模拟的硬件类型。我们建议针对各种设备进行测试,但如果你不确定从哪里开始,Pixel 系列中的最新设备可能是一个不错的选择。

¥Under Select Hardware, 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 in the Recommended tab), and download the image.

5

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

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

多个 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 版本:

¥Open the terminal and check the adb version on the system:

Terminal
adb version

2

并从 Android SDK platform-tool 目录:

¥And from the Android SDK platform-tool directory:

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

3

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

¥Copy adb from Android SDK directory to usr/bin directory:

Terminal
sudo cp ~/Library/Android/sdk/platform-tools/adb /usr/bin
Expo 中文网 - 粤ICP备13048890号