Skip to content

Instantly share code, notes, and snippets.

@j-nava
Created January 25, 2023 18:34
Show Gist options
  • Select an option

  • Save j-nava/d87f475a4231201213b1eba141d99239 to your computer and use it in GitHub Desktop.

Select an option

Save j-nava/d87f475a4231201213b1eba141d99239 to your computer and use it in GitHub Desktop.
Android virtual device (emulator) + WSL2

Android virtual device (emulator) + WSL2

Connect to an Android virtual device running in Windows from your WSL2 instance

2023-25-01

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.

Prerequisites

Assuming you have a WSL2 instance up and running, you need:

Windows

  • Android SDK installed
  • adb in the environment path (optional but it's an assumption for this guide)
  • Virtual device set up and open
  • adb devices correctly reporting that the virtual device is online. Eg.:
List of devices attached
emulator-5554   device

WSL2

  • Android SDK installed
  • adb in the environment path (optional but it's an assumption for this guide)
  • socat (recommended to be installed from your distro's package manager)

Step 1: Run adb server in Windows

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.

Step 2: Forward connections in WSL2

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

  1. citation needed 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment