This is for those of you who want or need to use Windows but prefer to have everything dev-related (including GUI applications like IDEs) running inside WSL2. Unfortunately, if you're developing for Android, it's impossible1 to run virtual devices in WSL2.
Assuming you have a WSL2 instance up and running, you need:
- Android SDK installed
adbin the environment path (optional but it's an assumption for this guide)- Virtual device set up and open
adb devicescorrectly reporting that the virtual device is online. Eg.:
List of devices attached
emulator-5554 device
- Android SDK installed
adbin the environment path (optional but it's an assumption for this guide)socat(recommended to be installed from your distro's package manager)
From an elevated PowerShell:
# Disables firewall for the WSL network adapter
Set-NetFirewallProfile -Profile Private -DisabledInterfaceAliases "vEthernet (WSL)"
Set-NetFirewallProfile -Profile Public -DisabledInterfaceAliases "vEthernet (WSL)"
# Kills adb server if running
adb kill-server
# Runs adb server
adb -a -P 5037 nodaemon server
If everything goes right, adb should block and wait for network connections.
From a WSL2 terminal:
# Kills adb server if running
adb kill-server
# Forwards connections
socat -d -d TCP-LISTEN:5037,reuseaddr,fork TCP:$(cat /etc/resolv.conf | tail -n1 | cut -d " " -f 2):5037
If everything goes right, socat should block and redirect local tcp connections from port 5037 to Windows.
Open a new WSL2 terminal and test the connection:
adb devices
You should see something like this, as if it was running in WSL2:
List of devices attached
emulator-5554 device
Footnotes
-
citation needed 😅 ↩