共用方式為


Use the Surface Duo emulator with Visual Studio

Important

This article describes functionality and guidance that is in public preview and may be substantially modified before it's generally available. Microsoft makes no warranties, express or implied, with respect to the information provided here.

To build Android apps with Xamarin in C# using Visual Studio, set up these tools then follow the instructions on this page:

Install the emulator preview

Get the installer from the Microsoft Download Center:

Choose the .zip package for Windows or the .dmg for macOS.

When the download has completed, run the installer.

The installer places icons in the Start menu, inside a SurfaceDuoEmulator folder. Use the icon labelled Surface Duo Emulator for Visual Studio to open the emulator before attempting to deploy inside Visual Studio.

Run the emulator

Visual Studio will not automatically start the Surface Duo emulator, so you should manually open it before attempting to deploy or debug your Xamarin app.

Follow these instructions to start the emulator, after which it will appear as a deployment option in Visual Studio:

After completing the SDK Setup, you can launch the Surface Duo Emulator for Visual Studio via the Start menu link.

Start menu entry for Surface Duo emulator

In the installation folder, there is a batch file run_vs.bat which you can execute directly. There is also a run_vs_clean.bat file which will start the emulator in a fresh state (rather than continue a previous session).

If the emulator does not start, you might need to update the pointer to your Android SDK installation.

Note

The installation folder contains start-up scripts named run and run_vs. The run scripts are intended for developers using Android Studio and the run_vs scripts are for Visual Studio users. The differences are the Android SDK location and the port used for the emulator.

If the emulator is running but does not appear as a deployment option in Visual Studio, close the emulator and re-start, confirming that you are using the script with vs in the name.

Debugging your Xamarin Android App

Open your Project in Visual Studio, build it, and select <build> (Android 10.0 - API 29) (in the list of Running Devices) and deploy.

Debugging (breakpoints, step into, step over, etc) will all work as usual.

Updating the Android Emulator in SDK Tools

Visual Studio manages an approved list of SDK tools and by default the update process is restricted to this list. Sometimes you may need to override the defaults to get the latest version of a component, such as the Android 12L emulator release which requires a specific version of some SDK tools to run.

Follow these steps to update to the latest version of the Android Emulator in Visual Studio:

  1. Go to Tools > Android SDK Manager> Tools tab.

    Visual Studio Tools Android SDK Manager menu

  2. Notice that the Android Emulator status shows Update available.

  3. Click the Update Available button and a new window will show the version that will be downloaded. In this example, it shows 31.2.10 will be installed BUT we want to install version 31.3.12.

    SDK tools update options default allowed list

  4. Cancel the update and in the Android SDKs and Tools window, click the small gear icon in the bottom right corner. This will reveal a Repository submenu with two options:

    • Approved List (Recommended)
    • Full List (Unsupported)

    Select the Full list (Unsupported) option.

    Choose full list from the repository menu

  5. Click the Update Available button again and this time the 31.3.12 version will be offered.

    Newer versions are available with the full list enabled

  6. Click Install Updates and the latest versions of the selected SDK tools will be installed.

Troubleshooting

Surface Duo emulator does not appear in the Visual Studio device list

The current version of the Surface Duo emulator cannot be started by Visual Studio. First manually start the emulator and once it's running it should appear in the list for deployment and debugging:

  • Windows - open Start > Surface Duo Emulator and choose Surface Duo Emulator for Visual Studio.
  • Mac - start from the command line using sh ./run_vs.sh (the "VS" in the script name indicates it is intended for Visual Studio for Mac).

Update the pointer to your Android SDK

If you changed the installation location of the Android SDK from the default, you will need to update the run_vs script to point to the correct installation location. Otherwise, the emulator will not run when you click the shortcut or run the script.

  1. Find the installation location of the Surface Duo Emulator. The default location is %USERPROFILE%\SurfaceDuoEmulator.
  2. Open the artifacts folder inside of the installation folder.
  3. Make a backup copy of run_vs.bat - You can copy it to something like run_vs_original.bat.
  4. Open the file run_vs.bat in a text editor.
  5. Verify the ANDROID_SDK_LOCATION path value near the top of the file is correct.
rem ##### ENSURE THE SDK LOCATION BELOW IS CORRECT: #######
set ANDROID_SDK_LOCATION=C:\Program Files (x86)\Android\android-sdk