If you need Hyper-V turned on while using an Android Emulator, the only way to go is to install the Visual Studio Android Emulator. You can skip the preamble and go directly to the install instructions.
Android emulation has always been a critical part of the development cycle of android apps.
Because of the sluggines of the first google emulator, others have developed alternative solutions like Genymotion , the now deprecated Xamarin Android Player or the Microsoft Visual Studio Android Emulator. Today the Google emulator is way better than before, thanks to HAXM (Intel Hardware Accelerated Execution Manager).
To choose the emulator that suits your needs, let’s recap some facts:
All Android emulators but Visual Studio Android Emulator require Hyper-V to be turned off, because Genymotion run inside a VirtualBox virtual machine while the new (and fast) Google emulator use HAXM and both are incompatible with Hyper-V.
All Android emulators but Google one don’t have the Google Play Services, because Google apparently does not license the needed parts to other emulator providers.
Knowing that, it’s clear that if you don’t need Hyper-V the right choice is to use the Google emulator with HAXM. That’s why this is the preselected choice when you’re installing the Visual Studio 2017 “Mobile Development with .NET” workload:
But if you need Hyper-V for other stuffs and you don’t want to turn it on and off (rebooting your PC) or moreover you need it during an Android emulation session to connect to locally Docker hosted services, then the right thing to do is to install Visual Studio Android Emulator.
How to install Visual Studio Android Emulator
Open the Visual Studio Installer and uncheck “Google Android Emulator” and “Intel Hardware Acceleration”. This is not a required step but will save you lot of installation space (around 14 GB).
Then move to the Single Components tab, scroll to the Emulators section and select “Visual Studio Android Emulator”:
After completing the installation, you’ll find the Visual Studio Android Emulator in the Tools menu:
That in turn open the app, where you can create one or more virtual devices.
Once created, the virtual device is nothing more than a Virtual Machine inside Hyper-V (more on this later, when we will enable internet connection) but this is transparent to the user. Opening or creating a Xamarin Android project shows the created virtual device into the device list:
And everything (should) work as expected.
Enable internet connection
Alas, the connection between the emulated Android device and Visual Studio use an internal switch of Hyper-V. Thus, the emulator is not able to connect to internet. To solve this issue, we need to open the Hyper-V Manager Console and click on the Virtual Switch Management voice in the Action panel:
Here we can see a virtual switch named “Windows Phone Emulator Internal Switch”:
Please, do yourself a favor and do not change it to external net: any different configuration will break the emulator. I myself spent half a day trying all the possible configurations, with no result, until Rob Prouse saved my life with the solution.
Instead of tweaking the “Windows Phone Emulator Internal Switch”, we need to create a new switch altogether. I’m Italian, so I’ve named it “Commutatore virtuale esterno” but you can name it “External Switch” or whatever you like. The only important thing is to create it as an external type:
Once done, back to the management console, we need to select the Android virtual machine and click on “Settings…”:
As you can see, we already have a Network Interface Card connected to the Windows Phone Emulator Internal Switch that is needed by the emulator and we aren’t allowed to change:
Instead, the trick is to create another Network Interface Card:
And then connect it to the external switch we just created few steps above:
And we are (almost) done.
This solution has some drawbacks:
- You still need to install Google Play Services, but this is out of the scope of this post.
- When creating the external switch, using the Wi-Fi adapter could not work. I have the docking station for my Surface Pro 4 and I’ve used the Ethernet one, as suggested by Rob Prouse, and it works.
Maybe I’ll write another post on those points. For the first one you can check this (outdated but still useful) post: https://university.xamarin.com/resources/how-to-install-google-play-on-android-emulator.
Other useful info
Using the emulator, you could run in a couple of issues that prevent you to debug the app from Visual Studio. The symptom is that the app start in the emulator and immediately crash, going back to the device home screen. To solve, please have a look at this post:
That’s all folks, happy coding!