Android SDK Emulator

PDF for offline use

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-03

This guide discusses how to configure and run the default emulator in Xamarin Studio and Visual Studio.

Overview

This guide explains how to launch Google's Android SDK Android emulator from Xamarin Studio to test a Xamarin.Android app; it also explains how to create custom emulator configurations. An emulator can be run in a variety of configurations to simulate different devices. Each one of these configurations is created as a virtual device. By default, the Xamarin.Android installer will create a virtual device for each of the Android platforms registered by the installer. Pre-configured virtual devices are available for immediate debug, but it is possible to create custom virtual device configurations that can be launched from the IDE.

ℹ️

To maximize the performance of the Google emulator, it is strongly recommended to install and configure the Intel Hardware Execution Manager (HAXM) to make this emulator run faster. See Accelerating Android Emulators with HAXM for more information.

Using a Pre-Configured Virtual Device

Xamarin.Android can run an application using one of the emulator images that are created during installation. For example, the following pre-configured virtual devices are typically available from the device drop-down menu:

Virtual devices

If the Xamarin.Android project is using a target framework level that is incompatible with one or more of the available virtual devices, the drop-down menu lists the unusable virtual devices under Incompatible API Level. For example, the following project has a target framework level set to Android 5.0 Lollipop (API 21), which is incompatible with the XamarinAndroidAPI_15 (API 15) virtual device:

Incompatiable virtual device

Your Visual Studio installation may include pre-configured Android emulator images that appear in the device drop-down menu. For example, in the following screen shot, the pre-configured virtual devices XamarinAndroidAPI15 and XamarinAndroidAPI21 are available in the device drop-down menu:

Virtual devices

If pre-configured virtual devices are not shown in the drop-down menu, you must use the Android Virtual Device (AVD) Manager to create one or more virtual devices as explained below in Configuring Virtual Devices.

If the Xamarin.Android project is using a target framework level that is incompatible with any of the available virtual devices, the drop-down menu will list the unusable virtual devices under Unsupported Devices. For example, the following project has a target framework level set to Android 5.0 Lollipop (API 21), which is incompatible with the XamarinAndroidAPI_15 (API 15) virtual device:

Incompatible virtual device

It is possible to create a custom Android Virtual Device (AVD) image that that is more appropriate for a given Xamarin.Android project by using the Android Virtual Device Manager (as explained in the next section).

Configuring Virtual Devices

Virtual devices are configured via the Android Virtual Device (AVD) Manager. The AVD Manager can be launched directly from the IDE or from the Android SDK Manager.. To launch the AVD Manager in Xamarin Studio, select Manage Android Devices... from the device drop-down menu:

Manage Android devices

Alternately, launch the AVD Manager from the menu bar by selecting Tools > Open Google Emulator Manager.

The Manage Devices dialog lists the currently configured virtual device emulator images. Click Create Emulator to launch the AVD Manager:

Manage devices

The Android Virtual Devices tab displays the list of existing Android virtual devices (or none if no virtual devices have been created):

AVD Manager

Virtual devices are configured via the Android Virtual Device (AVD) Manager. To launch the AVD Manager in Visual Studio, click the Android Emulator Manager (AVD) icon in the toolbar:

AVD icon location

Launch the AVD Manager from the menu bar by selecting Tools > Google Emulator Manager.

The Android Virtual Device (AVD) Manager dialog displays the list of existing Android virtual devices:

Virtual Device Manager

It is not possible to overwrite an existing AVD image. When the Edit AVD button is clicked in the Edit Android Virtual Device (AVD) dialog, the changes are saved to a new AVD. The next section explains how to create custom device definitions and AVDs.

Creating a Device Definition

To create a custom device definition, click Create Device.... This opens the Create new Android Virtual Device (AVD) dialog box. Modify the parameters in this dialog to specify a virtual device and click OK to create it:

Custom device definition

To create a custom device definition, click Create.... This opens the Create new Android Virtual Device (AVD) dialog box. Modify the parameters in this dialog to specify a virtual device and click OK to create it:

Custom device definition

The results of the new virtual device configuration are detailed in the next dialog:

Create results

Create results

For a detailed explanation of the configuration properties, see Hardware options.

After clicking OK, the custom device configuration is displayed in the list of existing Android virtual devices:

Added to AVD list

MyNexus added to the list

The new custom virtual device is also added to the device pull-down menu:

MyNexus now in pull-down menu

Cloning a Device Definition

It is possible to select an an existing device definition and clone it to create a new custom device definition. This is a good strategy to use when there is an existing device definition that needs only a few minor adjustments.

The Device Definitions tab in the Android Virtual Devices (AVD) Manager lists all available device definitions:

Device definitions

Device definitions

The preconfigured devices in this list cannot be modified (only user-created virtual devices can be edited). It is possible to create a new device definition from a preconfigured device by clicking Clone... For example, selecting the Nexus 5 definition and clicking Clone... presents the following dialog:

Clone device

Clone device

In the next screenshot, the name is changed to Nexus 5 Custom and the device parameters are modified to create a new custom device definition:

Custom Nexus

Custom Nexus

Clicking Clone Device creates the new device definition, which now appears in the Device Definitions list:

New definition

New definition

Note that each user-created device definition is displayed with a green icon, as shown above. This new device defintion can be used to create a new AVD by selecting the definition and clicking Create AVD.... This displays the Create new Android Virtual Device (AVD) dialog. In this example, we use the generated AVD name AVD_for_Nexus_5_Custom to create a new virtual device:

Create AVD

Create AVD

The CPU/ABI and Skin settings must be specified before the OK button can be clicked. After OK is clicked, the custom device configuration is displayed in the list of existing Android virtual devices, and it is added to the device pull-down menu:

New AVD added to device drop-down

New AVD added to device drop-down

Launching the Emulator

Near the top of the IDE, there is a drop-down menu that can be used to select Debug or Release mode. Choosing Debug attaches the debugger to the application process running inside the emulator. After the virtual device is chosen (from the device drop-down menu), select either Debug or Release mode.Click the Play button to run the application:

Debug and Release

Debug and Release

The Google Android emulator will start and Xamarin.Android will deploys the app to the emulator and runs it with the selected virtual device image (as illustrated in the screenshot below):

Emulator running

Emulator running

The emulator may be left running, it is not necessary to shut it down and restart it with each time the application is run. The first time a Xamarin.Android application is run in the emulator, the Xamarin.Android shared runtime for the targetted API level is installed, followed by the application. The runtime installation may take a few moments, so please be patient. 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.

If the emulator does not start, HAXM may have to be manually updated (rather than updating HAXM from the Android SDK Manager). HAXM install packages for both Windows and Mac OS X are available from the Intel Hardware Accelerated Execution Manager page. Download the installer that is appropriate for the platform, then run the installer and follow the installation instructions.

Summary

This guide explained the process for configuring the default Google Android emulator to run and test an application. It introduced the pre-configured virtual devices, described how to create custom Android Virtual Devices (AVDs), and provided the steps for deploying an application to the emulator.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.