9

I would like to run WSL2 with my api in it, and an Android emulator to run my app so I can test said api. WSL2 is working perfectly when I'm having Hyper-V activated. Then I'm trying to use Android Studio's emulator and it just hangs, never launching the emulator.

Now, if I deactivate Hyper-V entirely (in the windows features screen and through bcdedit /set hypervisorlaunchtype off) the emulator does launch perfectly, but of course WSL2 won't work now...

I took a look at lots of the topics around here without success. For example How to run Android emulator on windows while using WSL2 with zsh?, Running React Native in WSL with the emulator running directly in Windows or this tutorial. No success so far.

As said I'm using a Ryzen processor.

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88
  • 1
    Just as a heads-up, support for nested virtualization on AMD processors (which we need for this to work) is a feature currently in Windows Insider/Preview. It is expected to be available in the Windows 10 update later this year (and in Windows 11). – NotTheDr01ds Jul 07 '21 at 16:08
  • Interesting! Thanks – Jeremy Belolo Jul 08 '21 at 17:57
  • 1
    @NotTheDr01ds how would nested virtualization help with it? Do you mean running AVD inside WSL? Do you have link that confirms that? – otocon Jul 13 '21 at 14:23
  • The problem isn't that AVD needs to run under WSL2, but that WSL2 requires Hyper-V to be running on the host. And the Android emulation can't run when another hypervisor has control (see the second paragraph in [this section](https://developer.android.com/studio/run/emulator-acceleration#disable-hyper-v) of the Google doc). Nested virtualization allows other virtualization technologies to run while still under the control of Hyper-V. That said, it probably also still require changes on Google's part to utilize this feature once available, and it doesn't sound like they've done this yet. – NotTheDr01ds Jul 13 '21 at 14:33
  • @otocon Also note the answer below, which requires Windows 11 (where AMD nested virtualization is available) in order to run an Android VM inside Hyper-V. – NotTheDr01ds Jul 13 '21 at 14:35
  • 1
    @NotTheDr01ds answer below is mine and it does not use nested virtualization :) (or I am not aware of it). Nested virtualization is about running VM inside another VM. MS article here: https://techcommunity.microsoft.com/t5/virtualization/amd-nested-virtualization-support/ba-p/1434841 and here https://techcommunity.microsoft.com/t5/virtualization/windows-insider-preview-nested-virtualization/ba-p/382256. It is more like story with VirtualBox, where they had to integrate with Hyper-V https://www.how2shout.com/how-to/use-virtualbox-and-hyper-v-together-on-windows-10.html – otocon Jul 13 '21 at 21:37
  • 1
    @otocon Sorry, you are correct -- In re-reading your answer and looking at the [Android-x86](https://www.android-x86.org) project, it's clear that you are just running a Hyper-V VM that does *not* use nested virtualization, since there's no VM running inside that image -- I was thinking there was a VM providing the "emulation" inside that image, but it's not. Also, Google's method with WHPX doesn't appear to use nested virtualization either. – NotTheDr01ds Jul 13 '21 at 23:12

3 Answers3

2

I was able to find way around it. I'm running Windows 11 Pro, Update 21H2, OS build 22000.51, AMD.

Here is Google documentation how to setup emulator for AMD processors. It requires Hyper-V and WHPX to be disabled. And that does not go in pair with running WSL2.

My setup is:

  • Android Studio and IntelliJ running under Windows
  • Docker, DBs and other services running under Ubuntu (WSL2)

What I did:

  1. Enable Hyper-V and WHPX.
  2. Added new Android VM in Hyper-V Manager like that
  3. Find Android VM IP address (I did so by going to running Android wi-fi options) and use adb connect <ip goes here>:5555
  4. If connected properly, it should appear in Android Studio.

I tried to adb connect to the Android from Ubuntu, but it does not work out of the box. Should be doable with some WSL2 > Host redirects, but I currently do not need it and did not research it any further.

Last thing that bugged me was default Android VM resolution (looks like tablet in horizontal orientation). It is possible to adjust it like that.

We probably need to wait for Google to come up with emulator that uses the same emulation type as WSL2.

Things that do not work/are missing (or I do not know how to do it):

  • cannot change screen orientation on the fly
  • software keyboard is not showing up when typing (problem when you need to test your UI with it)
  • no GPU acceleration in Hyper-V (Microsoft disabled it due to secuirty reasons (?) but there seems to be a way to enable it, have not tried it, for my needs default performance is fine)
otocon
  • 1,029
  • 1
  • 8
  • 15
  • Very interesting! I will try this. I would prefer to be able to use Android Studio's emulators, but since it's impossible this will probably be the best there is right now. Thanks, will update on how it worked out. – Jeremy Belolo Jul 07 '21 at 12:54
  • One thing I failed to find a solution for is opening the remote developer tools. Tried to open the app menu with the command tool (d), with keyboard shortcuts from the emulator itself, with an adb command `adb shell input keyevent 82` - doesn't care, won't open the menu, so it's impossible to toggle remote debugging. – Jeremy Belolo Aug 06 '21 at 13:42
0

So, someone gave a great answer on the issue tracker!

https://issuetracker.google.com/issues/202188690#comment18

Basically, as he says, It's being caused by the XSAVE disable code in the WHPX API integration -- target/i386/whpx-all.c -- commenting out this line appears to make it work: https://android.googlesource.com/platform/external/qemu/+/refs/heads/emu-master-dev/target/i386/whpx-all.c#1768

In the issuetracker, another user has posted already compiled exe files, if anyone needs it. For me it fixed everything, the emulator is now working perfectly properly.

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88
  • Dose this work for Windows 10..? – Arst Jan 25 '22 at 12:31
  • Yes it was for Windows 10. Since I upgraded to Windows 11 I don't even need it - everything works as intended for me – Jeremy Belolo Jan 26 '22 at 14:01
  • In the original post, the #65 reply says: Reply to #64, the fix in official emulator releases only applies to Windows 11.. So I assume it is not for Windows 11... – Arst Feb 09 '22 at 09:09
0

Just add these extra features in windows and you can then run your emulator within wsl2, remember to chown for dev/kvm to your user.

enter image description here

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33553255) – Bö macht Blau Jan 06 '23 at 18:40