我在 中设置了一个开发环境Hyper-V 虚拟机(Windows 10)。使用 Visual Studio 2019 作为我的 Xamarin IDE(一个 monodroid
项目)。
根据我的阅读,由于 Hyper-V 设计人员做出的决定,我的物理设备无法“通过”。
我也尝试过使用变通方法,不幸的是,设备没有出现在 guest 机器中(即使在 Device Manager
中)。
最初我无法看到该设备(在 IDE 中),但是,经过一些研究和工作,我设法让该设备显示出来。我用了this “欺骗”adb
在 guest 上运行以查看设备(连接到我的物理机)。
主机以 IP 192.168.1.13
运行脚本在端口 6037
( adb daemon
在 5037
上运行,这是默认设置)。 guest 机器运行脚本,remotehost
设置为主机IP,具有相同的端口。根据文档,我相信这种配置很好。
不幸的是,即使我能够部署到我的设备,调试器也无法连接,我收到 Cannot start debugging: Cannot connect to 127.0.0.1:29300: No connection could be made because the target machine actively refused it 127.0.0.1:29301
Couldn't connect debugger. You can see more details in Xamarin Diagnostic output and the full exception on logs.
根据我收集到的信息,调试器正在尝试连接到 localhost
,并且由于 IDE 在 guest 中运行,因此物理设备不会位于 localhost:29301
.我假设我是否可以让 IDE 连接到 192.168.0.13:29301
(主机 IP)相反,它会找到设备,因为它是连接的位置。
当我运行 adb devices
,主机和 guest 都显示物理设备,并在尝试运行调试器后,运行 adb forward --list
,前向规则如下:
V4QBB18807150883 tcp:29300 tcp:29300
V4QBB18807150883 tcp:29301 tcp:29301
我也可以在
Xamarin Diagnostics
中看到那setprop "debug.mono.extra" "debug=127.0.0.1:29302:29303,timeout=1585047190,loglevel=0,server=y"
被执行。我认为操纵它会给我一些快乐,但是,我不知道如何。我对
adb
不太熟悉和网络,所以任何有更多经验的人都可以对我的情况有所了解。如何让调试器(在 guest 内)连接到我的物理设备?
PS:在这里问之前我已经搜索了很多。我知道这存在:How can I connect to Android with ADB over TCP?即使在该页面上尝试了多个答案,我也无法解决我的问题。
最佳答案
经过大量研究,我遇到了this .而不是寻找设置adb
的方法端口转发,我试图看看是否有办法通过 Hyper-V 设置 USB 直通。
如果使用 RDP 是一个选项(通过“Hyper-V 管理器”直接连接也可以):
On host machine
Press Win+R type gpedit.msc and hit enter
Go to Computer configuration > Administrative templates > Windows Components > Remote Desktop Services > Remote Desktop Connection Client > RemoteFX USB Device Redirection
Select the only GPO there and Enable it and change the access rights to "Administrators and Users"
On VM:
Press Win+R type gpedit.msc and hit enter
Go to Computer configuration > Administrative templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Device and Resource Redirection
Select GPO "Do not allow supported Plug and Play device redirection" and disable it
Reboot both machines...
根据此评论,您可能需要强制更新组策略:
I had to force update group policy (gpupdate) for it to take effect. Just shutting down and restarting both host and vm wasn't enough. Thanks
最终,在执行了这些步骤之后(我运行了
gpupdate
并重新启动了两台机器),我在列出的设备中多了一个选项,“其他支持的 RemoteFX USB 设备”,这就是我的物理设备。勾选复选框后,VM 检测到设备,无需端口转发。这可能会导致安全风险,允许这样的直通,但是,我的 VM 不是公共(public)的。只能通过我的主机访问。
如果安全人员可以评论可能的安全风险,我将不胜感激,只是为了通知我自己和遇到这篇文章的潜在其他人。
关于android - 虚拟机开发环境中物理设备上的 USB 调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60829713/