I'm trying to debug / connect up a device for development using WSL2 (Ubuntu). I've followed steps on this post https://stackoverflow.com/a/58229368/21061 which sets up ADB on both Windows and Linux using the same ADB version.
Once I've done that however, I get an empty list of devices in the Ubuntu terminal. I've tried killing and restarting the ADB server from the Windows command line and that doesn't seem to make any difference. Is this not possible in WSL2 or is there something I'm missing?
This answer worked for me using WSL 2:
On Windows:
adb tcpip 5555
Then on WSL 2:
adb connect [ip device]:5555
If it's the first time, it's going to ask you for permission in your phone, make sure to check the box to always grant permission. Then restart adb and connect again:
adb kill-server
adb connect [ip device]:5555
You can get your [ip device] from: Settings > About device > Status. It has a form like: 170.100.100.100
Jorge's answer is fine, but beginners like me may need some more details.
[ip device] is [YOUR_PHONE_IP] address. To get the IP address of your phone, go to "Settings -> About phone -> Status -> IP address". It will probably be something like 192.168.x.y.
I did not add adb to my PATH variable - neither in Windows nor in Linux/WSL2. Instead I just downloaded the latest version for both OS's using the links below:
https://dl.google.com/android/repository/platform-tools-latest-linux.zip https://dl.google.com/android/repository/platform-tools-latest-windows.zip
Once I unzipped platform-tools I had to change directory to the unzipped folder (cd platform-tools) in both PowerShell and WSL2.
Then in PowerShell on Windows, I run .\adb tcpip 5555 in the platform-tools folder.
In WSL2 terminal, I run ./adb connect 192.168.2.199:5555 (where 192.168.2.199 was my PHONE_IP address).
The first time you connect using [YOUR_PHONE_IP] address, you will be prompted to confirm the connection. The adb might say it failed to connect while it was waiting for the confirmation. If so, run ./adb kill-server in the WSL2 terminal and then run ./adb connect [YOUR_PHONE_IP]:5555 again.
You can display the list of attached devices via .\adb devices in PowerShell and ./adb devices in WSL2.
That is all. Now you should be able to debug your phone using WSL2.
we can do it more easily on WSL2
if we installed adb in windows and the path is /mnt/c/platform-tools/adb.exe
the WSL2 adb install at /usr/bin/adb
let change adb to adb_bk and set ln -s to link adb.exe
sudo mv /usr/bin/adb /usr/bin/adb_bk
sudo ln -s /mnt/c/platform-tools/adb.exe /usr/bin/adb
now we can use adb and work at bash script
try it
printf '\n%s\n%s\n' \
'### Alias for Android Debugging in WSL2' \
'alias adb="/mnt/c/Program\ Files\ \(x86\)/adb/adb.exe"' \
>> ~/.bashrc
source ~/.bashrc
C:\> adb devices
List of devices attached
P1CN21AKXAZ device
If adb.exe in windows will find your android device then also adb in wsl will. I writing this since I dont recommending anyone to use adb tcpip (that is recommended on few places I seen) without know exactly what risks it entails. I linking my script for hacking any android device within 1-3 seconds by copy and paste from devices that used adb tcpip command without understanding consequences, I want to point out that I am writing this to announce how dangerous it can be to give advice on something without being equal to giving the advice that when you are done, turn it off ASAP!
adb disconnect
ln -s "/mnt/c/Program\ Files\ \(x86\)/adb/adb.exe" /usr/bin/adb