Android Emulator 疑难解答Android emulator troubleshooting

本文介绍配置和运行 Android Emulator 时最常见的警告消息和问题。此外,其中还介绍了这些错误的解决方案以及各种疑难解答提示,以帮助诊断仿真器出现的问题。_This article describes the most common warning messages and issues that occur while configuring and running the Android Emulator. In addition, it describes solutions for resolving these errors as well as various troubleshooting tips to help you diagnose emulator problems.

在 Windows 上部署时出现的问题Deployment issues on Windows

部署应用时,仿真器可能会显示一些错误消息。Some error messages may be displayed by the emulator when you deploy your app. 此处介绍最常见的错误和解决方案。The most common errors and solutions are explained here.

部署错误Deployment errors

若出现有关无法在仿真器上安装 APK 或无法运行 Android Debug Bridge (adb) 的错误消息,请验证 Android SDK 能否连接到仿真器 。If you see an error about a failure to install the APK on the emulator or a failure to run the Android Debug Bridge (adb), verify that the Android SDK can connect to your emulator. 要验证仿真器的连接情况,请使用以下步骤:To verify emulator connectivity, use the following steps:

  1. 通过“Android Device Manager”启动仿真器(选择虚拟设备并单击“启动”) 。Launch the emulator from the Android Device Manager (select your virtual device and click Start).

  2. 打开命令提示符,转到 adb 的安装文件夹 。Open a command prompt and go to the folder where adb is installed. 如果 Android SDK 安装在其默认位置,则 adb 位于 C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe;如果不是,请在计算机上修改 Android SDK 所在的路径 。If the Android SDK is installed at its default location, adb is located at C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe; if not, modify this path for the location of the Android SDK on your computer.

  3. 键入以下命令:Type the following command:

    adb devices
    
  4. 如果可以通过 Android SDK 访问仿真器,那么仿真器应该就显示在附加设备列表中。If the emulator is accessible from the Android SDK, the emulator should appear in the list of attached devices. 例如:For example:

    List of devices attached
    emulator-5554   device
    
  5. 如果仿真器不在此列表中,请启动“Android SDK 管理器” ,应用所有更新,再尝试重启仿真器。If the emulator does not appear in this list, start the Android SDK Manager, apply all updates, then try launching the emulator again.

MMIO 访问错误MMIO access error

若出现“发生 MMIO 访问错误”消息,请重启仿真器 。If the message An MMIO access error has occurred is displayed, restart the emulator.

缺少 Google Play ServicesMissing Google Play Services

如果在仿真器中运行的虚拟设备未安装 Google Play Services 或 Google Play 商店,则在未安装这些软件包时创建虚拟设备通常会出现此情况。If the virtual device you are running in the emulator does not have Google Play Services or Google Play Store installed, this condition is often caused by creating a virtual device without including these packages. 创建虚拟设备时(请参阅使用 Android Device Manager 管理虚拟设备),请务必选择以下一个或两个选项:When you create a virtual device (see Managing Virtual Devices with the Android Device Manager), be sure to select one or both of the following options:

  • Google API – 在虚拟设备中包含 Google Play Services。Google APIs – includes Google Play Services in the virtual device.
  • Google Play 商店 – 在虚拟设备中包含 Google Play 商店。Google Play Store – includes Google Play Store in the virtual device.

例如,此虚拟设备将包含 Google Play Services 和 Google Play 商店:For example, this virtual device will include Google Play Services and Google Play Store:

启用了 Google Play Services 和 Google Play 商店的示例 AVDExample AVD with Google Play Services and Google Play Store enabled

备注

Google Play 商店图片仅适用于某些基本设备类型,例如 Pixel、Pixel 2、Nexus 5 和 Nexus 5X。Google Play Store images are available only for some base device types such as Pixel, Pixel 2, Nexus 5, and Nexus 5X.

性能问题Performance issues

性能问题通常由以下某个问题引起:Performance issues are typically caused by one of the following problems:

  • 仿真器在没有硬件加速的情况下运行。The emulator is running without hardware acceleration.

  • 在仿真器中运行的虚拟设备未使用基于 x86 的系统映像。The virtual device running in the emulator is not using an x86-based system image.

以下部分更详细地介绍了这些方案。The following sections cover these scenarios in more detail.

未启用硬件加速Hardware acceleration is not enabled

如果未启用硬件加速,则从设备管理器启动虚拟设备时将生成一个对话框,其中显示一条错误消息,指出未正确配置 Windows 虚拟机监控程序平台 (WHPX):If hardware acceleration is not enabled, starting a virtual device from the Device Manager will produce a dialog with an error message indicating that the Windows Hypervisor Platform (WHPX) is not configured properly:

示例设备管理器警告

如果显示此错误消息,请参阅下面的硬件加速问题,了解可用于验证和启用硬件加速的步骤。If this error message is displayed, see Hardware acceleration issues below for steps you can take to verify and enable hardware acceleration.

加速已启用但仿真器运行速度过慢Acceleration is enabled but the emulator runs too slowly

导致此问题的常见原因是虚拟设备 (AVD) 中未使用基于 x86 的映像。A common cause for this problem is not using an x86-based image in your virtual device (AVD). 创建虚拟设备时(请参阅使用 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:

为虚拟设备选择 x86 系统映像Selecting an x86 system image for a virtual device

硬件加速问题Hardware acceleration issues

无论使用 Hyper-V 还是 HAXM 进行硬件加速,都可能会遇到配置问题或与计算机上的其他软件发生冲突。Whether you are using Hyper-V or HAXM for hardware acceleration, you may run into configuration problems or conflicts with other software on your computer. 可通过打开命令提示符并输入以下命令来验证是否已启用硬件加速(以及仿真器正在使用哪种加速方法):You can verify that hardware acceleration is enabled (and which acceleration method the emulator is using) by opening a command prompt and entering the following command:

"C:\Program Files (x86)\Android\android-sdk\emulator\emulator-check.exe" accel

此命令假定将 Android SDK 安装在默认位置 C:\Program Files (x86)\Android\android-sdk;如果不是,请在计算机上修改上述 Android SDK 所在的路径 。This command assumes that the Android SDK is installed at the default location of C:\Program Files (x86)\Android\android-sdk; if not, modify the above path for the location of the Android SDK on your computer.

硬件加速不可用Hardware acceleration not available

如果 Hyper-V 可用,将从 emulator-check.exe accel 命令返回类似以下示例的消息 :If Hyper-V is available, a message like the following example will be returned from the emulator-check.exe accel command:

HAXM is not installed, but Windows Hypervisor Platform is available.

如果 HAXM 可用,将返回类似以下示例的消息:If HAXM is available, a message like the following example will be returned:

HAXM version 6.2.1 (4) is installed and usable.

如果硬件加速不可用,将显示如下示例的消息(如果无法找到 Hyper-V,仿真器将查找 HAXM):If hardware acceleration is not available, a message like the following example will be displayed (the emulator looks for HAXM if it is unable to find Hyper-V):

HAXM is not installed on this machine

如果硬件加速不可用,请参阅使用 Hyper-V 加速了解如何在计算机上启用硬件加速。If hardware acceleration is not available, see Accelerating with Hyper-V to learn how to enable hardware acceleration on your computer.

BIOS 设置不正确Incorrect BIOS settings

如果未正确配置 BIOS 以支持硬件加速,则在运行 emulator-check.exe accel 命令时将显示类似于以下示例的消息 :If the BIOS has not been configured properly to support hardware acceleration, a message similar to the following example will be displayed when you run the emulator-check.exe accel command:

VT feature disabled in BIOS/UEFI

要解决此问题,请重启计算机的 BIOS 并启用以下选项:To correct this problem, reboot into your computer's BIOS and enable the following options:

  • 虚拟化技术(标签可能因母板制造商而不同)。Virtualization Technology (may have a different label depending on motherboard manufacturer).
  • 硬件强制执行数据执行保护。Hardware Enforced Data Execution Prevention.

如果启用了硬件加速并且 BIOS 配置正确,则仿真器可通过硬件加速成功运行。If hardware acceleration is enabled and the BIOS is configured properly, the emulator should run successfully with hardware acceleration. 然而,一些特定于 Hyper-V 和 HAXM 的问题仍可能引起某些问题,下面将对此进行解释。However, problems may still result due to issues that are specific to Hyper-V and HAXM, as explained next.

HYPER-V 问题Hyper-V issues

在某些情况下,在“打开或关闭 Windows 功能”对话框中启用 Hyper-V 和 Windows 虚拟机监控程序平台后可能无法正确启用 Hyper-V 。In some cases, enabling both Hyper-V and Windows Hypervisor Platform in the Turn Windows features on or off dialog may not properly enable Hyper-V. 要验证是否已启用 Hyper-V,请使用以下步骤:To verify that Hyper-V is enabled, use the following steps:

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

  2. 右键单击搜索结果中的 Windows PowerShell,然后选择“以管理员身份运行” 。Right-click Windows PowerShell in the search results and select Run as administrator.

  3. 在 PowerShell 控制台中,输入以下命令:In the PowerShell console, enter the following command:

    Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online
    

    如果未启用 Hyper-V,将显示类似于以下示例的消息,指示 Hyper-V 的状态为“已禁用” :If Hyper-V is not enabled, a message similar to the following example will be displayed to indicate that the state of Hyper-V is Disabled:

    FeatureName      : Microsoft-Hyper-V-All
    DisplayName      : Hyper-V
    Description      : Provides services and management tools for creating and running virtual machines and their resources.
    RestartRequired  : Possible
    State            : Disabled
    CustomProperties : 
    
  4. 在 PowerShell 控制台中,输入以下命令:In the PowerShell console, enter the following command:

    Get-WindowsOptionalFeature -FeatureName HypervisorPlatform -Online
    

    如果未启用虚拟机监控程序,将显示类似于以下示例的消息,指示虚拟机监控程序平台的状态为“已禁用” :If the Hypervisor is not enabled, a message similar to the following example will be displayed to indicate that the state of HypervisorPlatform is Disabled:

    FeatureName      : HypervisorPlatform
    DisplayName      : Windows Hypervisor Platform
    Description      : Enables virtualization software to run on the Windows hypervisor
    RestartRequired  : Possible
    State            : Disabled
    CustomProperties : 
    

如果未启用 Hyper-V 和/或虚拟机监控程序平台,请使用以下 PowerShell 命令进行启用:If Hyper-V and/or HypervisorPlatform are not enabled, use the following PowerShell commands to enable them:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
Enable-WindowsOptionalFeature -Online -FeatureName HypervisorPlatform -All

完成这些命令后,请进行重启。After these commands complete, reboot.

有关启用 Hyper-V 的详细信息(包括使用部署映像服务和管理工具启用 Hyper-V 的技术),请参阅安装 Hyper-VFor more information about enabling Hyper-V (including techniques for enabling Hyper-V using the Deployment Image Servicing and Management tool), see Install Hyper-V.

HAXM 问题HAXM issues

导致 HAXM 问题的原因通常包括:与其他虚拟化技术冲突、设置不正确或 HAXM 驱动器不是最新版本。HAXM issues are often the result of conflicts with other virtualization technologies, incorrect settings, or an out-of-date HAXM driver.

HAXM 进程未运行HAXM process is not running

如果已安装 HAXM,则可通过打开命令提示符并输入以下命令来验证 HAXM 进程是否正在运行:If HAXM is installed, you can verify that the HAXM process is running by opening a command prompt and entering the following command:

sc query intelhaxm

如果 HAXM 进程正在运行,应看到类似于下面结果的输出:If the HAXM process is running, you should see output similar to the following result:

SERVICE_NAME: intelhaxm
    TYPE               : 1  KERNEL_DRIVER
    STATE              : 4  RUNNING
                            (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

如果 STATE 未设置为 RUNNING,请参阅如何使用 Intel 硬件加速执行管理器解决该问题。If STATE is not set to RUNNING, see How to Use the Intel Hardware Accelerated Execution Manager to resolve the problem.

HAXM 虚拟化冲突HAXM virtualization conflicts

HAXM 可能与其他使用虚拟化的技术(如 Hyper-V、Windows Device Guard 和某防病毒软件)冲突:HAXM can conflict with other technologies that use virtualization, such as Hyper-V, Windows Device Guard, and some antivirus software:

  • Hyper-V – 如果使用的是 Windows 10 2018 年 4 月更新(内部版本 1803)之前的 Windows 版本,并启用了 Hyper-V,请按照禁用 Hyper-V 中的步骤操作以便启用 HAXM 。Hyper-V – If you are using a version of Windows before the Windows 10 April 2018 update (build 1803) and Hyper-V is enabled, follow the steps in Disabling Hyper-V so that HAXM can be enabled.

  • Device Guard – Device Guard 和 Credential Guard 可阻止在 Windows 计算机上禁用 Hyper-V。Device Guard – Device Guard and Credential Guard can prevent Hyper-V from being disabled on Windows machines. 若要禁用 Device Guard 和 Credential Guard,请参阅禁用 Device GuardTo disable Device Guard and Credential Guard, see Disabling Device Guard.

  • 防病毒软件 – 如果运行的防病毒软件(如 Avast)使用硬件协助虚拟化,请禁用或卸载此软件,再重启并重试运行 Android Emulator。Antivirus Software – If you are running antivirus software that uses hardware-assisted virtualization (such as Avast), disable or uninstall this software, reboot, and retry the Android emulator.

BIOS 设置不正确Incorrect BIOS settings

若要在 Windows PC 上使用 HAXM,只有在 BIOS 中启用虚拟化技术 (Intel VT-x),HAXM 才能正常运行。If you are using HAXM on a Windows PC, HAXM will not work unless virtualization technology (Intel VT-x) is enabled in the BIOS. 如果已禁用 VT-x ,则尝试启用 Android Emulator 时会出现如下错误消息:If VT-x is disabled, you will get an error similar to the following when you attempt to start the Android Emulator:

此计算机符合 HAXM 要求,但未启用 Intel 虚拟化技术 (VT-x)。This computer meets the requirements for HAXM, but Intel Virtualization Technology (VT-x) is not turned on.

若要更正此错误,请将计算机引导到 BIOS,同时启用 VT-x 和 SLAT(第二级地址转换),再重启计算机,以返回到 Windows。To correct this error, boot the computer into the BIOS, enable both VT-x and SLAT (Second-Level Address Translation), then restart the computer back into Windows.

禁用 Hyper-VDisabling Hyper-V

如果使用的是 Windows 10 2018 年 4 月更新(内部版本 1803)之前的 Windows 版本,并启用了 Hyper-V,则必须禁用 Hyper-V 并重启计算机才能安装和使用 HAXM 。If you are using a version of Windows before the Windows 10 April 2018 Update (build 1803) and Hyper-V is enabled, you must disable Hyper-V and reboot your computer to install and use HAXM. 如果使用的是 Windows 10 2018 年 4 月更新(内部版本 1803)或更高版本,则 Android Emulator 27.2.7 或更高版本可以使用 Hyper-V(而不是 HAXM)进行硬件加速,因此不需要禁用 Hyper-V 。If you are using Windows 10 April 2018 Update (build 1803) or later, Android Emulator version 27.2.7 or later can use Hyper-V (instead of HAXM) for hardware acceleration, so it is not necessary to disable Hyper-V.

可以按照下列步骤操作,在“控制面板”中禁用 Hyper-V:You can disable Hyper-V from the Control Panel by following these steps:

  1. 在 Windows 搜索框中输入“Windows 功能”,然后在搜索结果中选择“打开或关闭 Windows 功能” 。Enter windows features in the Windows search box and select Turn Windows features on or off in the search results.

  2. 取消选中 Hyper V :Uncheck Hyper-V:

    在“Windows 功能”对话框中禁用 Hyper-V

  3. 重新启动计算机。Restart the computer.

或者,可使用以下 PowerShell 命令禁用 Hyper-V 虚拟机监控程序:Alternately, you can use the following PowerShell command to disable the Hyper-V Hypervisor:

Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Hypervisor

Intel HAXM 和 Microsoft Hyper-V 不能同时处于活动状态。Intel HAXM and Microsoft Hyper-V cannot both be active at the same time. 遗憾的是,没有办法在不重启计算机的情况下在 Hyper-V 和 HAXM 之间切换。Unfortunately, there is no way to switch between Hyper-V and HAXM without restarting your computer.

在某些情况下,如果启用了 Device Guard 和 Credential Guard,按照前述步骤操作将无法成功禁用 Hyper-V。In some cases, using the above steps will not succeed in disabling Hyper-V if Device Guard and Credential Guard are enabled. 如果无法禁用 Hyper-V(或似乎已禁用,但仍无法安装 HAXM),请按照下一部分中的步骤操作,禁用 Device Guard 和 Credential Guard。If you are unable to disable Hyper-V (or it seems to be disabled but HAXM installation still fails), use the steps in the next section to disable Device Guard and Credential Guard.

禁用 Device GuardDisabling Device Guard

Device Guard 和 Credential Guard 可阻止在 Windows 计算机上禁用 Hyper-V。Device Guard and Credential Guard can prevent Hyper-V from being disabled on Windows machines. 对于由负责组织配置和控制的域加入计算机而言,这种情况通常都是一个需要解决的问题。This situation is often a problem for domain-joined machines that are configured and controlled by an owning organization. 在 Windows 10 上,请按照下列步骤操作,检查 Device Guard 是否在运行:On Windows 10, use the following steps to see if Device Guard is running:

  1. 在 Windows 搜索框中输入“系统信息”,然后在搜索结果中选择“系统信息” 。Enter System info in the Windows search box and select System Information in the search results.

  2. 在“系统摘要” 中,检查是否有“基于 Device Guard 虚拟化的安全性” ;若有,检查是否处于“正在运行” 状态:In the System Summary, look to see if Device Guard Virtualization based security is present and is in the Running state:

    Device Guard 存在且正在运行Device Guard is present and running

如果已启用 Device Guard,请按照下列步骤操作,禁用 Device Guard:If Device Guard is enabled, use the following steps to disable it:

  1. 确保已按照上一部分所述禁用“Hyper-V” (在“打开或关闭 Windows 功能” 下)。Ensure that Hyper-V is disabled (under Turn Windows Features on or off) as described in the previous section.

  2. 在 Windows 搜索框中,输入“gpedit” ,再选择“编辑组策略” 搜索结果。In the Windows Search Box, enter gpedit and select the Edit group policy search result. 这些步骤将启动“本地组策略编辑器” 。These steps launch the Local Group Policy Editor.

  3. 在“本地组策略编辑器” 中,依次转到“计算机配置”>“管理模板”>“系统”>“Device Guard” :In the Local Group Policy Editor, navigate to Computer Configuration > Administrative Templates > System > Device Guard:

    “本地组策略编辑器”中的“Device Guard”Device Guard in Local Group Policy Editor

  4. 将“打开基于虚拟化的安全性” 更改为“已禁用” (如上所示),再退出“本地组策略编辑器” 。Change Turn On Virtualization Based Security to Disabled (as shown above) and exit the Local Group Policy Editor.

  5. 在 Windows 搜索框中,输入“cmd” 。In the Windows Search Box, enter cmd. 右键单击搜索结果中的“命令提示符” ,再选择“以管理员身份运行” 。When Command Prompt appears in the search results, right-click Command Prompt and select Run as Administrator.

  6. 将以下命令复制并粘贴到命令提示符窗口(如果正在使用驱动器 Z: ,请改为选择未使用的驱动器号):Copy and paste the following commands into the command prompt window (if drive Z: is in use, pick an unused drive letter to use instead):

    mountvol Z: /s
    copy %WINDIR%\System32\SecConfig.efi Z:\EFI\Microsoft\Boot\SecConfig.efi /Y
    bcdedit /create {0cb3b571-2f2e-4343-a879-d86a476d7215} /d "DebugTool" /application osloader
    bcdedit /set {0cb3b571-2f2e-4343-a879-d86a476d7215} path "\EFI\Microsoft\Boot\SecConfig.efi"
    bcdedit /set {bootmgr} bootsequence {0cb3b571-2f2e-4343-a879-d86a476d7215}
    bcdedit /set {0cb3b571-2f2e-4343-a879-d86a476d7215} loadoptions DISABLE-LSA-ISO,DISABLE-VBS
    bcdedit /set {0cb3b571-2f2e-4343-a879-d86a476d7215} device partition=Z:
    mountvol Z: /d
    
  7. 重新启动计算机。Restart your computer. 在启动屏幕上,应该会出现类似以下消息的提示:On the boot screen, you should see a prompt similar to the following message:

    是否要禁用 Credential Guard?Do you want to disable Credential Guard?

    按下指示的键,以根据提示禁用 Credential Guard。Press the indicated key to disable Credential Guard as prompted.

  8. 重启计算机后,再次检查,以确保 Hyper-V 已禁用(如前述步骤所述)。After the computer reboots, check again to ensure that Hyper-V is disabled (as described in the previous steps).

如果 Hyper-V 仍未禁用,域加入计算机的策略可能会阻止禁用 Device Guard 或 Credential Guard。If Hyper-V is still not disabled, the policies of your domain-joined computer may prevent you from disabling Device Guard or Credential Guard. 在这种情况下,可以向域管理员申请豁免,以便能够选择禁用 Credential Guard。In this case, you can request an exemption from your domain administrator to allow you to opt out of Credential Guard. 或者,如果必须使用 HAXM,则可以使用未加入域的计算机。Alternately, you can use a computer that is not domain-joined if you must use HAXM.

其他故障排除提示Additional troubleshooting tips

以下建议通常有助于诊断 Android Emulator 问题。The following suggestions are often helpful in diagnosing Android emulator issues.

从命令行启动仿真器Starting the emulator from the command line

如果仿真器尚未运行,则可以从命令行(而不是从 Visual Studio 中)启动它以查看其输出。If the emulator is not already running, you can start it from the command line (rather than from within Visual Studio) to view its output. 通常,Android Emulator AVD 图像存储在以下位置(将“用户名”替换为 Windows 用户名) :Typically, Android emulator AVD images are stored at the following location (replace username with your Windows user name):

C:\Users\username\.android\avd C:\Users\username\.android\avd

可通过传入 AVD 的文件夹名称从此位置启动带有 AVD 图像的仿真器。You can launch the emulator with an AVD image from this location by passing in the folder name of the AVD. 例如,此命令将启动名为“Pixel_API_27”的 AVD :For example, this command launches an AVD named Pixel_API_27:

"C:\Program Files (x86)\Android\android-sdk\emulator\emulator.exe" -partition-size 512 -no-boot-anim -verbose -feature WindowsHypervisorPlatform -avd Pixel_API_27 -prop monodroid.avdname=Pixel_API_27

此示例假定将 Android SDK 安装在默认位置 C:\Program Files (x86)\Android\android-sdk;如果不是,请在计算机上修改上述 Android SDK 所在的路径 。This example assumes that the Android SDK is installed at the default location of C:\Program Files (x86)\Android\android-sdk; if not, modify the above path for the location of the Android SDK on your computer.

运行此命令时,它将在仿真器启动时生成许多行输出。When you run this command, it will produce many lines of output while the emulator starts up. 具体而言,如果硬件加速已启用并且正常运行(在此示例中,使用 HAXM 进行硬件加速),将出现如下所示的行:In particular, lines such as the following example will be printed if hardware acceleration is enabled and working properly (in this example, HAXM is used for hardware acceleration):

emulator: CPU Acceleration: working
emulator: CPU Acceleration status: HAXM version 6.2.1 (4) is installed and usable.

查看设备管理器日志Viewing Device Manager logs

通常可通过查看设备管理器日志来诊断仿真器问题。Often you can diagnose emulator problems by viewing the Device Manager logs. 这些日志将写入以下位置:These logs are written to the following location:

C:\Users\username\AppData\Roaming\XamarinDeviceManager C:\Users\username\AppData\Roaming\XamarinDeviceManager

可使用文本编辑器(如记事本)查看每个 DeviceManager.log 文件 。You can view each DeviceManager.log file by using a text editor such as Notepad. 以下示例日志项目表示,在计算机上未找到 HAXM:The following example log entry indicates that HAXM was not found on the computer:

Component Intel x86 Emulator Accelerator (HAXM installer) r6.2.1 [Extra: (Intel Corporation)] not present on the system

在 macOS 上部署时出现的问题Deployment issues on macOS

部署应用时,仿真器可能会显示一些错误消息。Some error messages may be displayed by the emulator when you deploy your app. 下面介绍最常见的错误和解决方案。The most common errors and solutions are explained below.

部署错误Deployment errors

若出现有关无法在仿真器上安装 APK 或无法运行 Android Debug Bridge (adb) 的错误消息,请验证 Android SDK 能否连接到仿真器 。If you see an error about a failure to install the APK on the emulator or a failure to run the Android Debug Bridge (adb), verify that the Android SDK can connect to your emulator. 要验证连接情况,请使用以下步骤:To verify connectivity, use the following steps:

  1. 通过“Android Device Manager”启动仿真器(选择虚拟设备并单击“启动”) 。Launch the emulator from the Android Device Manager (select your virtual device and click Start).

  2. 打开命令提示符,转到 adb 的安装文件夹 。Open a command prompt and go to the folder where adb is installed. 如果将 Android SDK 安装在其默认位置,则“adb”位于“~/Library/Developer/Xamarin/android-sdk-macosx/platform-tools/adb”;如果不是,请在计算机上修改 Android SDK 所在的路径 。If the Android SDK is installed at its default location, adb is located at ~/Library/Developer/Xamarin/android-sdk-macosx/platform-tools/adb; if not, modify this path for the location of the Android SDK on your computer.

  3. 键入以下命令:Type the following command:

    adb devices
    
  4. 如果可以通过 Android SDK 访问仿真器,那么仿真器应该就显示在附加设备列表中。If the emulator is accessible from the Android SDK, the emulator should appear in the list of attached devices. 例如:For example:

    List of devices attached
    emulator-5554   device
    
  5. 如果仿真器不在此列表中,请启动“Android SDK 管理器” ,应用所有更新,再尝试重启仿真器。If the emulator does not appear in this list, start the Android SDK Manager, apply all updates, then try launching the emulator again.

MMIO 访问错误MMIO access error

若显示“发生 MMIO 访问错误”,请重启仿真器 。If An MMIO access error has occurred is displayed, restart the emulator.

缺少 Google Play ServicesMissing Google Play Services

如果在仿真器中运行的虚拟设备未安装 Google Play Services 或 Google Play 商店,则在未安装这些软件包时创建虚拟设备通常会出现此情况。If the virtual device you are running in the emulator does not have Google Play Services or Google Play Store installed, this condition is usually caused by creating a virtual device without including these packages. 创建虚拟设备时(请参阅使用 Android Device Manager 管理虚拟设备),请务必选择以下一项或两项:When you create a virtual device (see Managing Virtual Devices with the Android Device Manager), be sure to select one or both of the following:

  • Google API – 在虚拟设备中包含 Google Play Services。Google APIs – includes Google Play Services in the virtual device.
  • Google Play 商店 – 在虚拟设备中包含 Google Play 商店。Google Play Store – includes Google Play Store in the virtual device.

例如,此虚拟设备将包含 Google Play Services 和 Google Play 商店:For example, this virtual device will include Google Play Services and Google Play Store:

启用了 Google Play Services 和 Google Play 商店的示例 AVDExample AVD with Google Play Services and Google Play Store enabled

备注

Google Play 商店图片仅适用于某些基本设备类型,例如 Pixel、Pixel 2、Nexus 5 和 Nexus 5X。Google Play Store images are available only for some base device types such as Pixel, Pixel 2, Nexus 5, and Nexus 5X.

性能问题Performance issues

性能问题通常由以下某个问题引起:Performance issues are typically caused by one of the following problems:

  • 仿真器在没有硬件加速的情况下运行。The emulator is running without hardware acceleration.

  • 在仿真器中运行的虚拟设备未使用基于 x86 的系统映像。The virtual device running in the emulator is not using an x86-based system image.

以下部分更详细地介绍了这些方案。The following sections cover these scenarios in more detail.

未启用硬件加速Hardware acceleration is not enabled

如果未启用硬件加速,则在将应用部署到 Android Emulator 时,可能会弹出一个对话框,其中显示“设备将不加速运行” 。If hardware acceleration is not enabled, a dialog may pop up with a message such as device will run unaccelerated when you deploy your app to the Android emulator. 如果不确定计算机上是否启用了硬件加速(或想知道哪种技术可以提供加速),请参阅下面的硬件加速问题,了解验证和启用硬件加速的步骤。If you are not certain whether hardware acceleration is enabled on your computer (or you would like to know which technology is providing the acceleration), see Hardware acceleration issues below for steps you can take to verify and enable hardware acceleration.

加速已启用但仿真器运行速度过慢Acceleration is enabled but the emulator runs too slowly

导致此问题的常见原因是虚拟设备中未使用基于 x86 的映像。A common cause for this problem is not using an x86-based image in your virtual device. 创建虚拟设备时(请参阅使用 Android Device Manager 管理虚拟设备),请确保选择基于 x86 的系统映像:When you create virtual device (see Managing Virtual Devices with the Android Device Manager), be sure to select an x86-based system image:

为虚拟设备选择 x86 系统映像Selecting an x86 system image for a virtual device

硬件加速问题Hardware acceleration issues

无论使用虚拟机监控程序框架还是 HAXM 进行仿真器的硬件加速,都可能遇到因安装不当或 macOS 版本过期引起的问题。Whether you are using the Hypervisor Framework or HAXM for hardware acceleration of the emulator, you may run into problems caused by installation issues or an out-of-date version of macOS. 以下部分可帮助你解决此问题。The following sections can help you resolve this issue.

虚拟机监控程序框架问题Hypervisor Framework issues

如果在新版 Mac 上使用 macOS 10.10 或更高版本,则 Android Emulator 将自动使用虚拟机监控程序框架进行硬件加速。If you are using macOS 10.10 or later on a newer Mac, the Android emulator will automatically use the Hypervisor Framework for hardware acceleration. 但是,某些旧版 Mac 或运行早于 10.10 版的 macOS 的 Mac 可能无法提供虚拟机监控程序框架支持。However, some older Macs or Macs running a version of macOS earlier than 10.10 may not provide Hypervisor Framework support.

要确定 Mac 是否支持虚拟机监控程序框架,请打开终端并输入以下命令:To determine whether or not your Mac supports the Hypervisor Framework, open a Terminal and enter the following command:

sysctl kern.hv_support

若 Mac 支持虚拟机监控程序框架,则上述命令将返回以下结果:If your Mac supports the Hypervisor Framework, the above command will return the following result:

kern.hv_support: 1

若 Mac 上没有虚拟机监控程序框架,则可以按照使用 HAXM 加速中的步骤来使用 HAXM 进行加速。If the Hypervisor Framework is not available on your Mac, you can follow the steps in Accelerating with HAXM to use HAXM for acceleration instead.

HAXM 问题HAXM issues

如果 Android Emulator 无法正常启动,这通常是 HAXM 问题所致。If the Android Emulator does not start properly, this problem is often caused by problems with HAXM. 导致 HAXM 问题的原因通常包括:与其他虚拟化技术冲突、设置不正确或 HAXM 驱动器不是最新版本。HAXM issues are often the result of conflicts with other virtualization technologies, incorrect settings, or an out-of-date HAXM driver. 按照安装 HAXM 中介绍的步骤,尝试重新安装 HAXM 驱动程序。Try reinstalling the HAXM driver, using the steps detailed in Installing HAXM.

其他故障排除提示Additional troubleshooting tips

以下建议通常有助于诊断 Android Emulator 问题。The following suggestions are often helpful in diagnosing Android emulator issues.

从命令行启动仿真器Starting the emulator from the command line

如果仿真器尚未运行,则可以从命令行(而不是从 Visual Studio for Mac 中)启动它以查看其输出。If the emulator is not already running, you can start it from the command line (rather than from within Visual Studio for Mac) to view its output. 通常,Android Emulator AVD 图像存储在以下位置:Typically, Android emulator AVD images are stored at the following location:

~/.android/avd ~/.android/avd

可通过传入 AVD 的文件夹名称从此位置启动带有 AVD 图像的仿真器。You can launch the emulator with an AVD image from this location by passing in the folder name of the AVD. 例如,此命令将启动名为“Pixel_2_API_28”的 AVD :For example, this command launches an AVD named Pixel_2_API_28:

~/Library/Developer/Xamarin/android-sdk-macosx/emulator/emulator -partition-size 512 -no-boot-anim -verbose -feature WindowsHypervisorPlatform -avd Pixel_2_API_28 -prop monodroid.avdname=Pixel_2_API_28

如果将 Android SDK 安装在其默认位置,则仿真器位于“~/Library/Developer/Xamarin/android-sdk-macosx/emulator”目录;如果不是,请在 Mac 上修改 Android SDK 所在的路径 。If the Android SDK is installed at its default location, the emulator is located in the ~/Library/Developer/Xamarin/android-sdk-macosx/emulator directory; if not, modify this path for the location of the Android SDK on your Mac.

运行此命令时,它将在仿真器启动时生成许多行输出。When you run this command, it will produce many lines of output while the emulator starts up. 具体而言,如果硬件加速已启用并且正常运行(在此示例中,使用虚拟机监控程序框架进行硬件加速),将出现如下示例的行:In particular, lines such as the following example will be printed if hardware acceleration is enabled and working properly (in this example, Hypervisor Framework is used for hardware acceleration):

emulator: CPU Acceleration: working
emulator: CPU Acceleration status: Hypervisor.Framework OS X Version 10.13

查看设备管理器日志Viewing Device Manager logs

通常可通过查看设备管理器日志来诊断仿真器问题。Often you can diagnose emulator problems by viewing the Device Manager logs. 这些日志将写入以下位置:These logs are written to the following location:

~/Library/Logs/XamarinDeviceManager ~/Library/Logs/XamarinDeviceManager

可查看每个“Android Devices.log”文件(通过双击文件在控制台应用中将其打开) 。You can view each Android Devices.log file by double-clicking it to open it in the Console app. 以下示例日志项目指示未找到 HAXM:The following example log entry indicates that HAXM was not found:

Component Intel x86 Emulator Accelerator (HAXM installer) r6.2.1 [Extra: (Intel Corporation)] not present on the system