通过硬件加速提高仿真器性能 (Hyper-V & HAXM)Hardware acceleration for emulator performance (Hyper-V & HAXM)

本文介绍了如何使用计算机的硬件加速功能最大限度提高 Android Emulator 的性能。This article explains how to use your computer's hardware acceleration features to maximize Android Emulator performance.

Visual Studio 便于开发人员在无法使用 Android 设备的情况下通过使用 Android Emulator 来测试和调试 Xamarin.Android 应用程序。Visual Studio makes it easier for developers to test and debug their Xamarin.Android applications by using the Android emulator in situations where an Android device is unavailable or impractical. 但是,如果硬件加速在运行 Android 仿真器的计算机上不可用,那么它的运行速度太慢。However, the Android emulator runs too slowly if hardware acceleration is not available on the computer that runs it. 通过将特殊的 x86 虚拟设备映像与计算机的虚拟化功能结合使用,可以极大地提高 Android Emulator 的性能。You can drastically improve the performance of the Android emulator by using special x86 virtual device images in conjunction with the virtualization features of your computer.

方案Scenario HAXMHAXM WHPXWHPX Hypervisor.FrameworkHypervisor.Framework
有 Intel 处理器You have an Intel Processor XX XX XX
有 AMD 处理器You have an AMD Processor XX
想支持 Hyper-VYou want to support Hyper-V XX
想支持嵌套虚拟化You want to support nested Virtualization 有限Limited
想使用 Docker 等技术You want to use technologies like Docker XX XX

在 Windows 上加速 Android EmulatorAccelerating Android emulators on Windows

以下虚拟化技术可用于加速 Android Emulator:The following virtualization technologies are available for accelerating the Android emulator:

  1. Microsoft 的 Hyper-V 和 Windows 虚拟机监控程序平台 (WHPX)Microsoft's Hyper-V and the Windows Hypervisor Platform (WHPX). Hyper-V 是 Windows 的虚拟化功能,使虚拟的计算机系统可以在物理主计算机上运行。Hyper-V is a virtualization feature of Windows that makes it possible to run virtualized computer systems on a physical host computer.

  2. Intel 硬件加速执行管理器 (HAXM)Intel's Hardware Accelerated Execution Manager (HAXM). HAXM 是运行 Intel CPU 的计算机所用的虚拟化引擎。HAXM is a virtualization engine for computers running Intel CPUs.

为了在 Windows 上获得最佳体验,建议使用 WHPX 来加速 Android Emulator。For the best experience on Windows, it is recommended that you use WHPX to accelerate the Android emulator. 如果计算机没有 Hyper-V,则可使用 HAXM。If WHPX is not available on your computer, then HAXM can be used. 如果满足以下条件,Android Emulator 将自动使用硬件加速:The Android emulator will automatically make use of hardware acceleration if the following criteria are met:

  • 硬件加速在开发计算机上可用并已启用。Hardware acceleration is available and enabled on your development computer.

  • 仿真器正在运行为基于 x86 的虚拟设备创建的系统映像。The emulator is running a system image created for an x86-based virtual device.

重要

不可在另一 VM(例如由 VirtualBox、VMware 或 Docker 托管的 VM)内运行经过 VM 加速的模拟器。You can't run a VM-accelerated emulator inside another VM, such as a VM hosted by VirtualBox, VMware, or Docker. 必须直接在系统硬件上运行 Android Emulator 。You must run the Android emulator directly on your system hardware.

有关使用 Android Emulator 进行启动和调试的信息,请参阅 Android Emulator 调试For information about launching and debugging with the Android emulator, see Debugging on the Android Emulator.

使用 HYPER-V 加速Accelerating with Hyper-V

在启用 Hyper-V 之前,请阅读以下部分以验证你的计算机是否支持 Hyper-V。Before enabling Hyper-V, read the following section to verify that your computer supports Hyper-V.

验证对 Hyper-V 的支持Verifying support for Hyper-V

Hyper-V 在 Windows 虚拟机监控程序平台上运行。Hyper-V runs on the Windows Hypervisor Platform. 若要将 Android Emulator 与 Hyper-V 配合使用,计算机必须满足以下条件才能支持 Windows 虚拟机监控程序平台:To use the Android emulator with Hyper-V, your computer must meet the following criteria to support the Windows Hypervisor Platform:

  • 计算机硬件必须满足以下要求:Your computer hardware must meet the following requirements:

    • 支持二级地址转换 (SLAT) 的 64 位 Intel 或 AMD Ryzen CPU。A 64-bit Intel or AMD Ryzen CPU with Second Level Address Translation (SLAT).
    • CPU 支持 VM 监视器模式扩展(Intel CPU 的 VT-c 技术)。CPU support for VM Monitor Mode Extension (VT-c on Intel CPUs).
    • 内存至少为 4 GB。Minimum of 4-GB memory.
  • 在计算机的 BIOS 中,必须启用以下项:In your computer's BIOS, the following items must be enabled:

    • 虚拟化技术(标签可能因母板制造商而不同)。Virtualization Technology (may have a different label depending on motherboard manufacturer).
    • 硬件强制执行数据执行保护。Hardware Enforced Data Execution Prevention.
  • 计算机必须更新到 Windows 10 2018 年 4 月更新(版本 1803)或更高版本。Your computer must be updated to Windows 10 April 2018 update (build 1803) or later. 可使用以下步骤验证 Windows 版本是否为最新版本:You can verify that your Windows version is up-to-date by using the following steps:

    1. 在 Windows 搜索框中,输入“关于”。Enter About in the Windows search box.

    2. 在搜索结果中选择“关于你的电脑”。Select About your PC in the search results.

    3. 在“关于”对话框中向下滚动到“Windows 规范”部分 。Scroll down in the About dialog to the Windows specifications section.

    4. 验证“版本”最低为 1803 版:Verify that the Version is at least 1803:

      Windows 规范Windows specifications

要验证计算机硬件和软件是否与 Hyper-V 兼容,请打开命令提示符并键入以下命令:To verify that your computer hardware and software is compatible with Hyper-V, open a command prompt and type the following command:

systeminfo

如果列出的所有 Hyper-V 要求的值均为“是”,则计算机可以支持 Hyper-V。If all listed Hyper-V requirements have a value of Yes, then your computer can support Hyper-V. 例如:For example:

示例 Systeminfo 输出Example systeminfo output

启用 HYPER-V 加速Enabling Hyper-V acceleration

若计算机符合上述条件,请执行以下步骤使用 Hyper-V 加速 Android Emulator:If your computer meets the above criteria, use the following steps to accelerate the Android emulator with Hyper-V:

  1. 在 Windows 搜索框中输入“Windows 功能”,然后在搜索结果中选择“打开或关闭 Windows 功能” 。Enter windows features in the Windows search box and select Turn Windows features on or off in the search results. 在“Windows 功能”对话框中,启用“Hyper-V”和“Windows 虚拟机监控程序平台” :In the Windows Features dialog, enable both Hyper-V and Windows Hypervisor Platform:

    启用 Hyper-V 和 Windows 虚拟机监控程序平台Enabling Hyper-V and Windows Hypervisor Platform

    进行这些更改后,重新启动计算机。After making these changes, reboot your computer.

重要

在 Windows 10 2018 年 10 月更新(RS5) 及更高版本中,你只需启用 Hyper-V 即可,因为它将自动使用 Windows 虚拟机监控程序平台 (WHPX)。On Windows 10 October 2018 Update (RS5) and higher, you only need to enable Hyper-V, as it will use Windows Hypervisor Platform (WHPX) automatically.

  1. 安装 Visual Studio 15.8 或更高版本(此版本 Visual Studio 通过 Hyper-V 提供用于运行 Android Emulator 的 IDE 支持)。Install Visual Studio 15.8 or later (this version of Visual Studio provides IDE support for running the Android emulator with Hyper-V).

  2. 安装 Android Emulator 包 27.2.7 或更高版本Install the Android Emulator package 27.2.7 or later. 要安装此包,请在 Visual Studio 中导航到“工具”>“Android”>“Android SDK 管理器”。To install this package, navigate to Tools > Android > Android SDK Manager in Visual Studio. 选择“工具”选项卡,确保 Android Emulator 版本至少为 27.2.7。Select the Tools tab and ensure that the Android emulator version is at least 27.2.7. 另请确保 Android SDK Tools 版本为 26.1.1 或更高版本:Also ensure that the Android SDK Tools version is 26.1.1 or later:

    “Android SDK 和工具”对话框Android SDKs and Tools dialog

创建虚拟设备时(参阅使用 Android Device Manager 管理虚拟设备),请确保选择基于 x86 的系统映像。When you create a virtual device (see Managing Virtual Devices with the Android Device Manager), be sure to select an x86-based system image. 如果使用基于 ARM 的系统映像,虚拟设备将无法加速并且运行缓慢。If you use an ARM-based system image, the virtual device will not be accelerated and will run slowly.

现在应启用 Hyper-V,以便可以运行加速的 Android Emulator。Hyper-V should now be enabled and you can run your accelerated Android emulator.

使用 HAXM 加速Accelerating with HAXM

如果计算机不支持 Hyper-V,你可以使用 HAXM 加速 Android Emulator。If your computer does not support Hyper-V, you may use HAXM to accelerate the Android emulator. 若要使用 HAXM,必须禁用 Device GuardYou must disable Device Guard if you want to use HAXM.

验证 HAXM 支持Verifying HAXM support

要确定硬件是否支持 HAXM,请按照我的处理器是否支持 Intel 虚拟化技术?中的步骤操作。To determine if your hardware supports HAXM, follow the steps in Does My Processor Support Intel Virtualization Technology?. 若硬件支持 HAXM,可以使用以下步骤检查是否已安装 HAXM:If your hardware supports HAXM, you can check to see if HAXM is already installed by using the following steps:

  1. 打开命令提示符窗口,然后输入以下命令:Open a command prompt window and enter the following command:

    sc query intelhaxm
    
  2. 检查输出,查看 HAXM 进程是否正在运行。Examine the output to see if the HAXM process is running. 如果它正在运行,你会看到将 intelhaxm 状态列为 RUNNING 的输出。if it is, you should see output listing the intelhaxm state as RUNNING. 例如:For example:

    HAXM 可用时 sc 查询命令的输出

    如果未将 STATE 设置为 RUNNING,则未安装 HAXM。If STATE is not set to RUNNING, then HAXM is not installed.

如果计算机可以支持 HAXM 但未安装 HAXM,请按照下一部分中的步骤安装 HAXM。If your computer can support HAXM but HAXM is not installed, use the steps in the next section to install HAXM.

安装 HAXMInstalling HAXM

可以从 Intel 硬件加速执行管理器 GitHub 发布页获取适用于 Windows 的 HAXM 安装包。HAXM install packages for Windows are available from the Intel Hardware Accelerated Execution Manager GitHub releases page. 若要下载并安装 HAXM,请按照下列步骤操作:Use the following steps to download and install HAXM:

  1. 从 Intel 网站下载适用于 Windows 的最新 HAXM 虚拟化引擎安装程序。From the Intel website, download the latest HAXM virtualization engine installer for Windows. 直接从 Intel 网站下载 HAXM 安装程序的优点是可以确保使用最新版本。The advantage of downloading the HAXM installer directly from the Intel website is that you can be assured of using the latest version.

  2. 运行 intelhaxm android.exe,启动 HAXM 安装程序。Run intelhaxm-android.exe to start the HAXM installer. 接受安装程序对话框中的默认值:Accept the default values in the installer dialogs:

    “Intel 硬件加速执行管理器安装程序”窗口

创建虚拟设备时(参阅使用 Android Device Manager 管理虚拟设备),请确保选择基于 x86 的系统映像。When you create a virtual device (see Managing Virtual Devices with the Android Device Manager), be sure to select an x86-based system image. 如果使用基于 ARM 的系统映像,虚拟设备将无法加速并且运行缓慢。If you use an ARM-based system image, the virtual device will not be accelerated and will run slowly.

疑难解答Troubleshooting

有关解决硬件加速问题的帮助,请参阅 Android Emulator 疑难解答指南。For help with troubleshooting hardware acceleration issues, see the Android emulator Troubleshooting guide.

在 macOS 上加速 Android EmulatorAccelerating Android emulators on macOS

以下虚拟化技术可用于加速 Android Emulator:The following virtualization technologies are available for accelerating the Android emulator:

  1. Apple 的虚拟机监控程序框架Apple's Hypervisor Framework. 虚拟机监控程序是 macOS 10.10 及更高版本的一项功能,可在 Mac 上运行虚拟机。Hypervisor is a feature of macOS 10.10 and later that makes it possible to run virtual machines on a Mac.

  2. Intel 硬件加速执行管理器 (HAXM)Intel's Hardware Accelerated Execution Manager (HAXM). HAXM 是运行 Intel CPU 的计算机所用的虚拟化引擎。HAXM is a virtualization engine for computers running Intel CPUs.

建议使用虚拟机监控程序框架来加速 Android 仿真器。It is recommended that you use the Hypervisor Framework to accelerate the Android emulator. 如果 Mac 上没有虚拟机监控程序框架,则可以使用 HAXM。If the Hypervisor Framework is not available on your Mac, then HAXM can be used. 如果满足以下条件,Android Emulator 将自动使用硬件加速:The Android emulator will automatically make use of hardware acceleration if the following criteria are met:

  • 硬件加速在开发计算机上可用并已启用。Hardware acceleration is available and enabled on the development computer.

  • 仿真器正在运行为基于 x86 的虚拟设备创建的系统映像。The emulator is running a system image created for an x86-based virtual device.

重要

不可在另一 VM(例如由 VirtualBox、VMware 或 Docker 托管的 VM)内运行经过 VM 加速的模拟器。You can't run a VM-accelerated emulator inside another VM, such as a VM hosted by VirtualBox, VMware, or Docker. 必须直接在系统硬件上运行 Android Emulator 。You must run the Android emulator directly on your system hardware.

有关使用 Android Emulator 进行启动和调试的信息,请参阅 Android Emulator 调试For information about launching and debugging with the Android emulator, see Debugging on the Android Emulator.

使用虚拟机监控程序框架加速Accelerating with the Hypervisor Framework

要将 Android Emulator 与虚拟机监控程序框架配合使用,Mac 必须符合以下条件:To use the Android emulator with the Hypervisor Framework, your Mac must meet the following criteria:

  • Mac 必须运行 macOS 10.10 或更高版本。Your Mac must be running macOS 10.10 or later.

  • Mac 的 CPU 必须能够支持虚拟机监控程序框架。Your Mac's CPU must be able to support the Hypervisor Framework.

如果你的 Mac 符合这些条件,Android 仿真器将自动使用虚拟机监控程序框架进行加速。If your Mac meets these criteria, the Android emulator will automatically use the Hypervisor Framework for acceleration. 若不确定 Mac 是否支持虚拟机监控程序框架,请参阅疑难解答指南,了解验证 Mac 是否支持虚拟机监控程序的方法。If you are not sure if Hypervisor Framework is supported on your Mac, see the Troubleshooting guide for ways to verify that your Mac supports Hypervisor.

如果 Mac 不支持虚拟机监控程序框架,可以使用 HAXM 加速 Android Emulator(如下所述)。If the Hypervisor Framework is not supported by your Mac, you can use HAXM to accelerate the Android emulator (described next).

使用 HAXM 加速Accelerating with HAXM

若你的 Mac 不支持虚拟机监控程序框架(或者 macOS 版本低于 10.10),则可使用“Intel 的硬件加速执行管理器”(HAXM) 来加速 Android Emulator。If your Mac does not support the Hypervisor framework (or you are using a version of macOS earlier than 10.10), you can use Intel's Hardware Accelerated Execution Manager (HAXM) to speed up the Android emulator.

在首次将 HAXM 与 Android Emulator 配合使用之前,最好先验证 HAXM 是否已安装并可供 Android Emulator 使用。Before using the Android emulator with HAXM for the first time, it's a good idea to verify that HAXM is installed and available for the Android emulator to use.

验证 HAXM 支持Verifying HAXM support

可使用以下步骤检查是否已安装 HAXM:You can check to see if HAXM is already installed by using the following steps:

  1. 打开终端,然后输入以下命令:Open a Terminal and enter the following command:

    ~/Library/Developer/Xamarin/android-sdk-macosx/tools/emulator -accel-check
    

    此命令假定将 Android SDK 安装在默认位置“~/Library/Developer/Xamarin/android-sdk-macosx”;否则,请在 Mac 上修改 Android SDK 位置的路径。This command assumes that the Android SDK is installed at the default location of ~/Library/Developer/Xamarin/android-sdk-macosx; if not, modify the above path for the location of the Android SDK on your Mac.

  2. 若已安装 HAXM,则上面的命令将返回类似于以下结果的消息:If HAXM is installed, the above command will return a message similar to the following result:

    HAXM version 7.2.0 (3) is installed and usable.
    

    若 HAXM 未安装,则返回类似于以下输出的消息:If HAXM is not installed, a message similar to the following output is returned:

    HAXM is not installed on this machine (/dev/HAX is missing).
    

如果未安装 HAXM,请按照下一部分的步骤安装 HAXM。If HAXM is not installed, use the steps in the next section to install HAXM.

安装 HAXMInstalling HAXM

可以从 Intel 硬件加速执行管理器页获取适用于 macOS 的 HAXM 安装包。HAXM installation packages for macOS are available from the Intel Hardware Accelerated Execution Manager page. 若要下载并安装 HAXM,请按照下列步骤操作:Use the following steps to download and install HAXM:

  1. 从 Intel 网站下载适用于 macOS 的最新 HAXM 虚拟化引擎安装程序。From the Intel website, download the latest HAXM virtualization engine installer for macOS.

  2. 运行 HAXM 安装程序。Run the HAXM installer. 接受安装程序对话框中的默认值:Accept the default values in the installer dialogs:

    “Intel 硬件加速执行管理器安装程序”窗口Intel Hardware Accelerated Execution Manager Setup window

疑难解答Troubleshooting

有关解决硬件加速问题的帮助,请参阅 Android Emulator 疑难解答指南。For help with troubleshooting hardware acceleration issues, see the Android emulator Troubleshooting guide.