简体   繁体   English

使用 WSL2 为空的 ADB 设备列表

[英]ADB device list empty using WSL2

I'm trying to debug / connect up a device for development using WSL2 (Ubuntu).我正在尝试使用 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.我已经按照这篇文章https://stackoverflow.com/a/58229368/21061上的步骤操作,它使用相同的 ADB 版本在 Windows 和 Linux 上设置 ADB。

Once I've done that however, I get an empty list of devices in the Ubuntu terminal.但是,一旦我这样做了,我会在 Ubuntu 终端中得到一个空的设备列表。 I've tried killing and restarting the ADB server from the Windows command line and that doesn't seem to make any difference.我已经尝试从 Windows 命令行杀死并重新启动 ADB 服务器,但这似乎没有任何区别。 Is this not possible in WSL2 or is there something I'm missing?这在 WSL2 中是不可能的,还是我遗漏了什么?

This answer worked for me using WSL 2:这个答案对我使用 WSL 2 有用:

On Windows:在 Windows 上:

adb tcpip 5555

Then on WSL 2:然后在 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 并再次连接:

adb kill-server
adb connect [ip device]:5555

You can get your [ip device] from: Settings > About device > Status.您可以从以下位置获取您的 [ip 设备]:设置 > 关于设备 > 状态。 It has a form like: 170.100.100.100它的形式如下: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. [ip 设备] 是[YOUR_PHONE_IP]地址。 To get the IP address of your phone, go to "Settings -> About phone -> Status -> IP address".要获取手机的 IP 地址,请转到“设置 -> 关于手机 -> 状态 -> IP 地址”。 It will probably be something like 192.168.xy它可能类似于 192.168.xy

I did not add adb to my PATH variable - neither in Windows nor in Linux/WSL2.我没有将 adb 添加到我的 PATH 变量中——在 Windows 和 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 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.解压缩平台工具后,我必须将目录更改为 PowerShell 和 WSL2 中的解压缩文件夹 ( cd platform-tools )。

Then in PowerShell on Windows, I run .\adb tcpip 5555 in the platform-tools folder.然后在 Windows 上的 PowerShell 中,我在platform-tools文件夹中运行.\adb tcpip 5555

In WSL2 terminal, I run ./adb connect 192.168.2.199:5555 (where 192.168.2.199 was my PHONE_IP address).在 WSL2 终端中,我运行./adb connect 192.168.2.199:5555 (其中 192.168.2.199 是我的 PHONE_IP 地址)。

The first time you connect using [YOUR_PHONE_IP] address, you will be prompted to confirm the connection.首次使用[YOUR_PHONE_IP]地址连接时,系统会提示您确认连接。 The adb might say it failed to connect while it was waiting for the confirmation. adb 在等待确认时可能会说连接失败。 If so, run ./adb kill-server in the WSL2 terminal and then run ./adb connect [YOUR_PHONE_IP]:5555 again.如果是这样,请在 WSL2 终端中运行./adb kill-server ,然后再次运行./adb connect [YOUR_PHONE_IP]:5555

You can display the list of attached devices via .\adb devices in PowerShell and ./adb devices in WSL2.您可以通过 PowerShell 中的.\adb devices./adb devices显示连接的设备列表。

That is all.就这些。 Now you should be able to debug your phone using WSL2.现在您应该可以使用 WSL2 调试您的手机了。

we can do it more easily on WSL2我们可以在WSL2上更轻松地做到这一点

if we installed adb in windows and the path is /mnt/c/platform-tools/adb.exe如果我们在 windows 中安装 adb 并且路径是/mnt/c/platform-tools/adb.exe

the WSL2 adb install at /usr/bin/adb WSL2 adb 安装在/usr/bin/adb

let change adb to adb_bk and set ln -s to link adb.exeadb更改为adb_bk并设置ln -s链接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现在我们可以使用 adb 并使用 bash 脚本

try it试试看

I was trying with this myself to get WSl2 working and i didn´t found any fast way to do this, this is easy and it is safe for everyone that wanna solve the problem:我自己尝试用这个来让 WSl2 工作,但我没有找到任何快速的方法来做到这一点,这很容易,而且对于想要解决问题的每个人来说都是安全的:
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
And now we can work as we wants in WSL1/WSL2 shell:现在我们可以在 WSL1/WSL2 shell 中随心所欲地工作了:
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.如果 windows 中的 adb.exe 会找到您的 android 设备,那么 wsl 中的 adb 也会。 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.我写这篇文章是因为我不建议任何人使用adb tcpip (在我见过的少数地方推荐),但不知道它会带来什么风险。 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 tcpip命令的设备复制和粘贴来链接我的脚本以在 1-3 秒内入侵任何 android 设备而不理解后果,我想指出我写这篇文章是为了宣布提供建议是多么危险不等于给出建议,当你完成后,尽快关闭它!

For people using over tcpip and is done with their tasks, execute:对于使用 over tcpip 并完成任务的人,执行:

adb disconnect
Of course you can create an symlink into /usr/bin also for using it system-wide if that is prefered.当然,如果愿意,您也可以在 /usr/bin 中创建一个符号链接,以便在系统范围内使用它。
 ln -s "/mnt/c/Program\ Files\ \(x86\)/adb/adb.exe" /usr/bin/adb

https://pastebin.com/raw/DtFSMBsF https://pastebin.com/raw/DtFSMBsF

in WSL2在 WSL2 中

sudo mv /usr/bin/adb /usr/bin/adb_bk sudo mv /usr/bin/adb /usr/bin/adb_bk

sudo ln -s /mnt/c/platform-tools/adb.exe /usr/bin/adb sudo ln -s /mnt/c/platform-tools/adb.exe /usr/bin/adb

adb devices亚行设备

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM