在 Android Emulator 上调试Debugging on the Android Emulator

本指南介绍如何在 Android Emulator 中启动虚拟设备以调试和测试应用。 In this guide, you will learn how to launch a virtual device in the Android Emulator to debug and test your app.

概述Overview

可使用各种配置运行 Android Emulator(作为“使用 .NET 进行移动开发”工作负荷的一部分进行安装)来模拟各种 Android 设备 。The Android Emulator (installed as part of the Mobile development with .NET workload), can be run in a variety of configurations to simulate different Android devices. 其中的每个配置都创建为虚拟设备 。Each one of these configurations is created as a virtual device. 本指南介绍如何从 Visual Studio 启动模拟器以及如何在虚拟设备中运行应用。In this guide, you will learn how to launch the emulator from Visual Studio and run your app in a virtual device. 有关配置 Android Emulator 和创建新的虚拟设备的信息,请参阅 Android Emulator 设置For information about configuring the Android Emulator and creating new virtual devices, see Android Emulator Setup.

使用预配置的虚拟设备Using a Pre-Configured Virtual Device

Visual Studio 包含预配置虚拟设备,可以在“设备”下拉菜单中查看。Visual Studio includes pre-configured virtual devices that appear in the device drop-down menu. 例如,在下面的 Visual Studio 2017 屏幕截图中,多个预配置虚拟设备都可用:For example, in the following Visual Studio 2017 screenshot, several pre-configured virtual devices are available:

  • VisualStudio_android-23_arm_phoneVisualStudio_android-23_arm_phone

  • VisualStudio_android-23_arm_tabletVisualStudio_android-23_arm_tablet

  • VisualStudio_android-23_x86_phoneVisualStudio_android-23_x86_phone

  • VisualStudio_android-23_x86_tabletVisualStudio_android-23_x86_tablet

虚拟设备Virtual devices

通常都会选择使用“VisualStudio_android-23_x86_phone” 虚拟设备来测试和调试手机应用。Typically, you would select the VisualStudio_android-23_x86_phone virtual device to test and debug a phone app. 如果这些预配置虚拟设备中有一个能够满足需求(即与应用的目标 API 级别一致),请跳转到启动仿真器,开始在仿真器中运行应用。If one of these pre-configured virtual devices meets your requirements (i.e., matches your app's target API level), skip to Launching the Emulator to begin running your app in the emulator. (如果尚不熟悉 Android API 级别,请参阅了解 Android API 级别。)(If you are not yet familiar with Android API levels, see Understanding Android API Levels.)

如果 Xamarin.Android 项目使用的目标框架级别与可用虚拟设备都不兼容,不可用的虚拟设备会在下拉菜单中的“不支持的设备”下列出 。If your Xamarin.Android project is using a Target Framework level that is incompatible with the available virtual devices, the drop-down menu lists the unusable virtual devices under Unsupported Devices. 例如,以下项目的目标框架设为 Android 7.1 Nougat (API 25),这与此示例中所列的 Android 6.0 虚拟设备不兼容 :For example, the following project has a Target Framework set to Android 7.1 Nougat (API 25), which is incompatible with the Android 6.0 virtual devices that are listed in this example:

不兼容的虚拟设备Incompatible virtual device

可以单击“更改最低 Android 目标” ,更改项目的最低 Android 版本,使其与可用虚拟设备的 API 级别一致。You can click Change Minimum Android Target to change the project's Minimum Android Version so that it matches the API level of the available virtual devices. 也可以使用 Android Device Manager 新建支持目标 API 级别的虚拟设备。Alternately, you can use the Android Device Manager to create new virtual devices that support your target API level. 必须先安装新 API 级别对应的系统映像(请参阅设置用于 Xamarin.Android 的 Android SDK),然后才能为虚拟设备配置此 API 级别。Before you can configure virtual devices for a new API level, you must first install the corresponding system images for that API level (see Setting up the Android SDK for Xamarin.Android).

编辑虚拟设备Editing Virtual Devices

若要修改虚拟设备(或创建新的虚拟设备),必须使用 Android Device ManagerTo modify virtual devices (or to create new ones), you must use the Android Device Manager.

启动仿真器Launching the Emulator

在 Visual Studio 的顶部附近,有一个可用于选择“调试”或“发布”模式的下拉菜单 。Near the top of Visual Studio, there is a drop-down menu that can be used to select Debug or Release mode. 选择“调试”可在应用启动后将调试器附加到仿真器内运行的应用程序进程 。Choosing Debug causes the debugger to attach to the application process running inside the emulator after the app starts. 选择“发布”模式可禁用调试器(但是,仍可运行应用并使用 LOG 语句进行调试) 。Choosing Release mode disables the debugger (however, you can still run the app and use log statements for debug). 在设备下拉菜单中选择虚拟设备后,选择“调试”或“发布”模式,然后单击“播放”按钮运行应用程序 :After you have chosen a virtual device from the device drop-down menu, select either Debug or Release mode, then click the Play button to run the application:

仿真器启动后,Xamarin.Android 将应用部署到仿真器。After the emulator starts, Xamarin.Android will deploy the app to the emulator. 仿真器会使用配置的虚拟设备映像运行此应用。The emulator runs the app with the configured virtual device image. Android Emulator 的示例屏幕截图如下所示。An example screenshot of the Android Emulator is displayed below. 在此示例中,仿真器在运行名为 MyApp 的空白应用 :In this example, the emulator is running a blank app called MyApp:

运行空白应用的仿真器

可以一直运行仿真器:无需关闭仿真器并在每次启动应用时等待重启。The emulator may be left running: it is not necessary to shut it down and wait for it to restart each time the app is launched. 当 Xamarin.Android 应用首次在仿真器中运行时,将会先安装面向目标 API 级别的 Xamarin.Android 共享运行时,再安装应用。The first time a Xamarin.Android app is run in the emulator, the Xamarin.Android shared runtime for the targeted API level is installed, followed by the application. 运行时安装过程可能需要一段时间,请耐心等待。The runtime installation may take a few moments, so please be patient. 仅当首次向仿真器部署 Xamarin.Android 应用时,才会安装运行时 – 后续部署速度更快,因为仅将应用复制到仿真器。Installation of the runtime takes place only when the first Xamarin.Android app is deployed to the emulator – subsequent deployments are faster because only the app is copied to the emulator.

快速启动Quick Boot

较新版本的 Android Emulator 包含一个名为“快速启动”的功能,只需几秒钟即可启动模拟器 。Newer versions of the Android Emulator include a feature called Quick Boot that launches the emulator in only a few seconds. 关闭仿真器时,它会拍摄虚拟设备状态的快照,以便在重启时从该状态快速还原。When you close the emulator, it takes a snapshot of the virtual device state so that it can be quickly restored from that state when it is restarted. 要使用此功能,需要以下工具:To access this feature, you will need the following:

  • Android Emulator 版本 27.0.2 或更高版本Android Emulator version 27.0.2 or later
  • Android SDK Tools 版本 26.1.1 或更高版本Android SDK Tools version 26.1.1 or later

安装了上述版本的仿真器和 SDK 工具后,默认情况下会启用“快速启动”功能。When the above-listed versions of the emulator and SDK tools are installed, the Quick Boot feature is enabled by default.

由于尚未创建快照,虚拟设备的首次冷启动没有加快速度:The first cold boot of the virtual device takes place without a speed improvement because a snapshot has not yet been created:

冷启动屏幕截图

退出仿真器时,“快速启动”在快照中保存仿真器的状态:When you exit out of the emulator, Quick Boot saves the state of the emulator in a snapshot:

在关闭时保存状态

之后,启动虚拟设备的速度大幅加快,因为仿真器只需还原关闭仿真器时的状态。Subsequent virtual device starts are much faster because the emulator simply restores the state at which you closed the emulator.

在重启时加载状态

疑难解答Troubleshooting

有关常见模拟器问题的技巧和解决方法,请参阅 Android Emulator 疑难解答For tips and workarounds for common emulator problems, see Android Emulator Troubleshooting.

总结Summary

本指南介绍如何配置 Android Emulator,以便能够运行和测试 Xamarin.Android 应用。This guide explained the process for configuring the Android Emulator to run and test Xamarin.Android apps. 其中介绍了使用预配置的虚拟设备启动仿真器的步骤,并提供了将应用程序从 Visual Studio 部署到仿真器的步骤。It described the steps for launching the emulator using pre-configured virtual devices, and it provided the steps for deploying an application to the emulator from Visual Studio.

若要深入了解如何使用 Android Developer,请参阅以下 Android 开发者主题:For more information about using the Android Emulator, see the following Android Developer topics: