Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 221
papadi Avatar asked Jun 02 '20 05:06

papadi


People also ask

How to connect Android Studio to WSL2?

In WSL2 terminal, run adb connect PHONE_ID:5555. You will be prompted to confirm the connection on your phone. Do that. 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. If Android Studio was open in WSL2, close it and then open it again.

How to run GUI apps on WSL2?

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. There is a way for the connected devices, but this has to be done one by one:

What are the limitations of WSL2?

A limitation is that WSL2 does not support nested virtualization at this time, so you will not be able to emulate an virtual Android device. Not the answer you're looking for?

Why can't I See my WSL IP address from the emulator?

Looks like WSL ip isn’t visible from within the emulator. You need to connect to windows ip and add a port forwarding in the same way as for metro bundle netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3333 connectaddress=$WSL_CLIENT connectport=3333 Hope this helps.


Video Answer


2 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

like image 142
lau Avatar answered Oct 23 '22 20:10

lau


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!

like image 11
Patrick Kenny Avatar answered Oct 23 '22 19:10

Patrick Kenny