'Could not connect to TCP port: Connection refused when trying to open Expo app on Android Emulator in WSL2

I've been following this guide https://medium.com/@pellea/using-adb-with-wsl2-7aebbb070a47 and so far have gotten to the point where WSL2 can see the emulator(s) running on Windows through Android Studios -> AVD manager. For example, on both Windows and WSL2, adb devices correctly shows:

List of devices attached
emulator-5554   device

However, when I go to my expo app and try to launch the Android emulator, I get the following error message:

Couldn't start project on Android: could not connect to TCP port 5554: Connection refused

This is after I've tried the following:

  1. exporting ADB_SERVER_SOCKET=tcp:<MY IP>:5037 in my WSL2 profile(s)
  2. Unblocking WSL2 vEthernet connections from my Windows firewall via (from the above link)
Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)"
  1. I've portforwarded 5554 from Windows to WSL2, as well as 5037 (from 0.0.0.0 to 127.0.0.1):
$WSL_CLIENT = bash.exe -c "ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'";
$WSL_CLIENT -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$WSL_CLIENT = $matches[0];
iex "netsh interface portproxy add v4tov4 listenport=5554 listenaddress=127.0.0.1 connectport=5554 connectaddress=$WSL_CLIENT"

The ports are as follows:

netsh interface portproxy show all

Listen on ipv4:             Connect to ipv4:

Address         Port        Address         Port
--------------- ----------  --------------- ----------
127.0.0.1       8081        172.25.38.171   8081
0.0.0.0         5037        127.0.0.1       5037
127.0.0.1       5554        172.29.149.0    5554


Solution 1:[1]

I had the exact same problem, but with a different setup (using Expo with the Metro bundler on a Mac). I worked around it by trying different virtual device images. Maybe the same can help you.

The virtual device that works with me so far is the one selected in this screenshot: https://i.stack.imgur.com/CFTJ0.png. The other two in the screenshot have the same problem. I don't really know the actual reason, must be something with port access with those other emulated devices.

Image details: Name: 4 WVGA (Nexus S) API 29 API: 29 Target: Android 10.0 (Google APIs) CPU/ABI (x86)

I'm sure other images work as well (or configuring the others somehow), i just haven't tried many more because it takes so long and they're so big.

Solution 2:[2]

Just had the same issue with Expo.

Here is what fixed it for me:

  1. Check for a process that uses port 5555 (Terminal)
    netstat -ano | findstr 5555
    
  2. Kill the process using its ID (PowerShell)
    Stop-Process -ID [PROCESS_ID] -Force
    

Then it started working.

Solution 3:[3]

Did you try to map port 5554 to Windows IP instead? (or localhost). The Android emulator is running on Windows, not WSL2, so the port proxy should be from WSL2 to Windows, not backwards.

Have you already solved it in another manner?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 RobC
Solution 3 Aitor Alonso