20

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?

papadi
  • 985
  • 2
  • 9
  • 22
  • 2
    https://stackoverflow.com/q/27245597/1778421 – Alex P. Jun 02 '20 at 12:31
  • Thanks. This partially answers my question. I managed to have adb running inside wsl2 to connect to the host's adb server, however Android Studio keeps doing its own thing and still starts a local adb server which obviously has no devices. There is an answer related to that in the post you recommended but has no votes and I couldn't make it work either. – papadi Jun 03 '20 at 05:49
  • @papadi how did you managed to run Android Studio inside WSL 2. Can you provide some information.?? – Ramesh-X Jun 24 '20 at 02:16
  • 1
    @AlexP. provided a solution above. I haven't found a better one so far. Unfortunately, it means that you cannot debug the app, but only build it using Studio and then copy it using an adb command. – papadi Jun 24 '20 at 06:50
  • @papadi my question was, how to install android studio in WSL 2 as you mentioned in the question. I have problems installing and running android studio in WSL 2. I can worry about adb later :D – Ramesh-X Jun 26 '20 at 03:14
  • 1
    I see. Just look around for how to run GUI apps on WSL2. You will need to install a desktop shell, enable remote desktop services on wsl and then connect to it using remote desktop. Then you have Linux desktop, you can install whatever you want. – papadi Jun 26 '20 at 07:14
  • I found that the following answer addresses my question. Posting it here for others with the same problem. https://askubuntu.com/a/925885/568872 – Ramesh-X Jun 27 '20 at 02:47

3 Answers3

19

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

Ryall
  • 12,010
  • 11
  • 53
  • 77
lau
  • 351
  • 2
  • 7
12

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!

Mj Carnaje
  • 78
  • 1
  • 7
Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76
6

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!

Nebel22
  • 478
  • 8
  • 17