You can set up Android Studio inside a Hyper-V virtual machine. The caveat ist that you need to use the Visual Studio Android Emulator instead of Google’s Android Emulator.
I did a 75 minute lab at TechReady 24, an internal conference at Microsoft this week. For this, I needed to provide a virtual machine with the development environment that would be installed to over 40 machines inside the room where the lab took place. The requirement for this was – we’re talking Microsoft after all – that the virtual machine uses Hyper-V, Microsoft’s own virtualization technology. As the lab was about our Android SDK, we need an Android Development Environment inside the VM which turns out to be rather tricky and poorly documented. All I found about this was a mere: “it’s possible”. The tricky part is that you can’t use Google’s own emulator inside a Hyper-V VM as HAXM requires Hyper-V to be disabled. So here’s how to setup an Android Dev Environment using the Visual Studio Android Emulator inside a Hyper-V VM.
I have used my 2015 Lenovo X1 Carbon laptop for this. It worked out well but more than 8 GB RAM would have been really nice as the VM only gets 4GB of RAM on a host with 8GB of RAM.
- Windows 10 Enterprise/Professional/Education
- 8GB RAM or more – According to the docs Hyper-V requires a minimum of 4GB on the host machine. The Visual Studio Android Emulator requires at least 2GB of RAM in the virtual machine, so a host machine with 4GB of RAM just won’t cut it. My laptop has 8GB of RAM and I definitely felt the need for more.
- A 64-bit Processor with Second Level Address Translation (SLAT).
- CPU support for VM Monitor Mode Extension (VT-C on Intel CPUs).
Task 1: Setup your VM
1. Make sure Hyper-V is enabled on the host.
Just apply some Google-fu here. It’s not that complicated.
2. [Optional] Create virtual switches
The Hyper-V Management Console application allows to define “virtual switches” that map your physical network to a virtual one. On my machine, this was already configured correctly.
3. Create your new VM
Open the Hyper-V Manager and create a new VM running Windows 10 using the wizard in the Hyper-V Management application. I won’t go into the details on how to do it as the wizard is pretty self-explanatory. The following 4 settings are crucial for our Android dev setup and no-one really tells you about this. I needed to apply some serious Google-fu:
- Set a static amount of RAM for the VM. The VM will need at least 4GB of it. 6 or 8 GB is better. My laptop could barely handle 4GB of RAM for the VM but it did the trick.
- Set the VM to have 2 CPUs even if your machine has just 1 CPU.
- Connect the virtual machine to your virtual switches to make sure the VM has internet access.
- Allow Mac Network Spoofing.
Before we go ahead, run your new VM for the first time and complete the initial Windows installation. Verify that the VM can access the internet. Once you’ve done that, shut down the VM.
4. Enable nested virtualization
You will be running the Visual Studio Android Emulator – which itself is a virtual machine –from inside a virtual machine and both are using Hyper-V. This means you need to enable nested virtualization which isn’t configured per defaul. Thanks to this blogpost it doesn’t get very complicated, because you did Steps 3.1 through 3.4, right?!
- Now, on your host machine, open PowerShell and run the following command. Replace
<VMName>with the name of your VM.
Set-VMProcessor -VMName <VMName> -ExposeVirtualizationExtensions $true
The official Hyper-V manual explains other configs to enable nested virtualization. After a lot of trial and error, I ended up with the VM configuration and the PowerShell command that I described above.
Task 2: Install Android Studio and Visual Studio Android Emulator
1. Android Studio
Get and install Android Studio. Easy. When you try to run your app in the emulator, Android Studio will complain that you need to disable Hyper-V to run its emulator. Ignore the message.
2. Visual Studio Android Emulator
- Make sure Hyper-V is enabled INSIDE the VM.
- Download the VS Android Emulator. After installation is complete, download one profile and start the device. It takes a few minutes, especially for the first launch. It’s really straight forward but there is one thing of note here: Launching the device will take forever (as in “will never finish”) if the VM doesn’t have enough RAM. It will never tell you about this. If your VM has 4GB of RAM, you should be able to run a profile for a device with up to 1GB of RAM. A 2GB profile won’t work. I wanted a device with API level 23 and had to choose a different profile than the default one because of this limitation. Also: Choosing a device profile with 512GB of RAM improved the launch time of the emulator considerably.
- Verify that the emulator itself is connected to the internet. If you run into issues here, it’s basically the same steps as with as for the VM on the host pc open the Hyper-V Management app. There should be a VM for each profile that you created in the VS Android Emulator. Check if the VM has its network switches configured correctly. If the “host”-VM was configured correctly, it should work out of the box.
3. Run your android app in Android Studio.
Run your new Visual Studio Android Emulator. Android Studio should recognize the running emulator as a device and allow you to run your app on it. You won’t be able to launch the VS Emulator from within Android Studio.