How to connect Android Studio running inside WSL2 with connected devices or android emulator running on host

Viewed 22460

I'm running Android Studio (currently in version 3.6.1) under Ubuntu 18.04 in WSL 2 (Windows 10 2004), which now supports GUI. Since virtualization inside WSL2 doesn't work, I need to keep running my emulator on the host and of course, connected devices which I use for debugging my android app appear on the host.

How can Android Studio see the emulator and devices appearing on the host?

3 Answers

There is a way for the connected devices, but this has to be done one by one:

  1. on windows: adb tcpip 5555
  2. on wsl2: adb connect [ip device]:5555

and in android studio should you see the device

Here's what I had to do based on this Medium post.

  1. Install Android Studio on Windows (to get the SDK, SDK can also be installed another way).
  2. Add adb to the environment variables on Windows. From File Explorer, right-click on This PC and then choose Advanced system settings, then Environment Variables. Under System variables, look for Path, click Edit, and then New. Add this path: C:\Users\MYUSERNAME\AppData\Local\Android\Sdk\platform-tools. Make sure to change MYUSERNAME.
  3. Install Android SDK in WSL2. In the SDK Manager in Android Studio, make sure you have an SDK installed that supports your phone (I had an older phone that needed an older SDK).
  4. In WSL2, add adb to your PATH. You need to make sure that you are using the same version of adb on Windows and Linux, which is why I installed the latest Android Studio on both. In my case, I added alias adb='/home/MYUSERNAME/Android/Sdk/platform-tools/adb' to my .bashrc.
  5. Make sure your phone is connected to your PC via USB and then you have set up USB debugging.
  6. In Powershell on Windows, run adb devices. Make sure the device is there.
  7. In Powershell on Windows, run adb tcpip 5555.
  8. In WSL2 terminal, run adb devices. You will not see any device yet, but this will start up adb.
  9. Get the IP address of your phone. You can do this from Settings -> About phone -> Status -> IP address. It will probably be something like 192.168.0.10n.
  10. In WSL2 terminal, run adb connect PHONE_ID:5555.
  11. You will be prompted to confirm the connection on your phone. Do that.
  12. adb will probably fail because of the connection confirmation. Run adb kill-server in the WSL2 terminal and then run adb connect PHONE_ID:5555.
  13. If Android Studio was open in WSL2, close it and then open it again.

Finally your device is available to debug!

ADB over TCP did not work for me. Found this work-around in a WSL GitHub issue that worked, adding the steps here with a few extra details:

(1) In WSL, add the following to ~/.bashrc:

export WSL_HOST_IP="$(tail -1 /etc/resolv.conf | cut -d' ' -f2)"
export ADB_SERVER_SOCKET=tcp:$WSL_HOST_IP:5037

(2) Add a Windows Firewall Rule

  • Open Windows Defender Firewall and go to Advanced Settings.
  • Right click on "Inbound Rules" and click "New Rule"
  • Select "Port" then Specific TCP port "5037", then "Allow the connection"
  • Check Domain, Private, and Public as needed for your Internet connection (I only added Domain and Private)
  • Name the rule whatever suits you
  • After the firewall entry is added, right click on it and go to Properties
  • Go to Scope -> Remote IP Addresses -> Add "172.16.0.0/12" (this is the WSL VM subnet)

(3) Start ADB Server (with specific arguments to make it listen on all addresses)

  • Create a a VBS script (e.g. "C:\Users\user\Documents\Development\wsl_adb_start.vbs") and put the following into it:
    CreateObject("WScript.Shell").Run "adb.exe kill-server", 0, True
    CreateObject("WScript.Shell").Run "adb.exe -a -P 5037 nodaemon server", 0, True
    
    Note: this assumes that adb is on your path. If not, add the full path to it (usually something like %USERPROFILE%\AppData\Local\Android\Sdk\platform-tools\adb.exe)
  • Run the script by opening up powershell and executing: WScript "C:\Users\shuba\Documents\Development\wsl_adb_start.vbs".
  • (Re)-start WSL

The VBS script invocation is the only step you will have to do per-boot: your WSL VM will connect to your host ADB instance. After this, start up WSL and enjoy!

Related