VirtualBox中的Android USB调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12477987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android USB debugging in VirtualBox
提问by Brian
I am trying to get mono for android (aka monodroid) working within a windows 7 virtual machine on virtualbox but I am having issues with the adb connection to my phone. When I run "adb devices" to list the devices present it show up no problem, but when I attempt to run an "adb install", logcat, or push it hangs. Currently there is a bug in virtualbox which applies to this but I was hoping someone more knowledgeable than myself would have a workaround.
我正在尝试在 virtualbox 上的 Windows 7 虚拟机中获得适用于 android(又名 monodroid)的单声道,但我在 adb 连接到我的手机时遇到了问题。当我运行“adb devices”来列出存在的设备时,它显示没有问题,但是当我尝试运行“adb install”时,logcat 或 push 挂起。目前,virtualbox 中存在一个适用于此的错误,但我希望比我自己知识渊博的人有一个解决方法。
https://www.virtualbox.org/ticket/6620
https://www.virtualbox.org/ticket/6620
Of coarse non of this would be an issue if Xamarin released a linux port of their product or if I got familiar with Java, but I suppose those are slightly different issues.
如果 Xamarin 发布了他们产品的 linux 端口或者如果我熟悉 Java,那么粗略的非这将是一个问题,但我认为这些问题略有不同。
回答by Stephen Niedzielski
Brian, one workaround you might consider is to make your VM host an ADB host as well. Then you can just connect your VM client to it over TCP / IP. Here's the general idea for that setup:
Brian,您可能考虑的一种解决方法是让您的 VM 主机也成为 ADB 主机。然后您可以通过 TCP/IP 将您的 VM 客户端连接到它。这是该设置的总体思路:
- Install the Android SDK on the host machine. You should only require the platform-tools package which includes
adb
. - Don't allow your VM client to take ownership of the Android device, so disable any VirtualBox USB filter rules you have and it also wouldn't hurt to then detach and reattach the device.
- From your VM client, run
adb kill-server
. Make it atskill adb
for certainty. If you have any running instances of Eclipse, you'll want to shut those down first because it'll actually launchadb
in the background. Don't skip this step. From the host, execute
adb devices
. If all goes well (and it should), you'll see your device listed. It should look something like this (note the port number and sorry for the mangling):* daemon not running. starting it now on port 5037 * * daemon started successfully * List of devices attached 015d2994ed200409 device
- 在主机上安装 Android SDK。您应该只需要包含
adb
. - 不要让您的 VM 客户端拥有 Android 设备的所有权,因此请禁用您拥有的任何 VirtualBox USB 过滤规则,然后分离并重新连接设备也不会受到伤害。
- 从您的 VM 客户端,运行
adb kill-server
. 让它成为tskill adb
肯定的。如果您有任何正在运行的 Eclipse 实例,您需要先关闭它们,因为它实际上会adb
在后台启动。不要跳过这一步。 从主机执行
adb devices
. 如果一切顺利(应该如此),您将看到您的设备已列出。它应该看起来像这样(请注意端口号并抱歉修改):* daemon not running. starting it now on port 5037 * * daemon started successfully * List of devices attached 015d2994ed200409 device
At this point, your host should have an ADB server running at port 5037. You can check this from the VM client by running telnet 10.0.2.2 5037
, where 10.0.2.2
is the default IP for VirtualBox hosts and 5037
is the default ADB port noted above.
此时,您的主机应该有一个在端口 5037 上运行的 ADB 服务器。您可以通过运行从 VM 客户端检查这一点telnet 10.0.2.2 5037
,其中10.0.2.2
是 VirtualBox 主机的默认 IP,5037
是上面提到的默认 ADB 端口。
Now you must either forward the ports from your host to your VM client, or otherwise connect ADB directly to your host IP:port. If you're like me, you'll find the ADBHOST and ANDROID_ADB_SERVER_PORT variables to be poorly documented and easy to screw up. For this reason, consider simple port fowarding over
ssh
(maybe via Cygwin) like so from the VM client:autossh -nNL5037:localhost:5037 -oExitOnForwardFailure=yes 10.0.2.2
Finally, run
adb devices
from your VM client. If you see "daemon not running," it means your port fowarding is screwed up. Otherwise you should see your device and be able to logcat all day. One noteworthy point is that you won't have anadb
daemon running on your VM client, except for when you're actually using the debug bridge, of course.
现在您必须将端口从您的主机转发到您的 VM 客户端,或者以其他方式将 ADB 直接连接到您的主机 IP:port。如果你像我一样,你会发现 ADBHOST 和 ANDROID_ADB_SERVER_PORT 变量的文档很差并且很容易搞砸。出于这个原因,请考虑
ssh
从 VM 客户端像这样进行简单的端口转发(可能通过 Cygwin):autossh -nNL5037:localhost:5037 -oExitOnForwardFailure=yes 10.0.2.2
最后,
adb devices
从您的 VM 客户端运行。如果您看到“守护进程未运行”,则表示您的端口转发被搞砸了。否则,您应该会看到您的设备并能够整天登录。值得注意的一点是,您adb
的 VM 客户端上不会运行守护程序,当然,除非您实际使用调试桥。
I've used a similar mechanism for debugging devices over the network that are connected to remote machines and it worked well.
我使用了一种类似的机制来调试通过网络连接到远程机器的设备,并且运行良好。
回答by FUD
Not sure what has changed in VirtualBox since the question was posted, but selecting the usb device from VirtualBox menu Devices -> USB -> DeviceNameworked seamlessly. I am working on VirtualBox 5.1.2 r108956.
不确定自发布问题以来 VirtualBox 中发生了什么变化,但是从 VirtualBox 菜单Devices -> USB -> DeviceName 中选择 USB 设备可以无缝工作。我正在使用 VirtualBox 5.1.2 r108956。
回答by Nebel22
I was also having trouble with ADB running over VM. I had a Windows 10 host and an Ubuntu 14.04 client. The autossh
method did not work for me. I found an easier solution from another StackOverflow question. It requires that both your PC and your device are on the same WiFi network.
我也遇到了在 VM 上运行 ADB 的问题。我有一个 Windows 10 主机和一个 Ubuntu 14.04 客户端。该autossh
方法对我不起作用。我从另一个StackOverflow 问题中找到了一个更简单的解决方案。它要求您的 PC 和您的设备在同一个 WiFi 网络上。
On your VM client run the following:
adb tcpip 5555
The device will disconnect from the VM because ADB is now in TCP mode instead of USB.
For the next part you need the IP address of your device. To find it, simply run
adb shell ip -f inet addr show wlan0
Now you can connect to your device. On your VM client, run:
adb connect <Device IP>:5555
在您的 VM 客户端上运行以下命令:
adb tcpip 5555
设备将与 VM 断开连接,因为 ADB 现在处于 TCP 模式而不是 USB。
对于下一部分,您需要设备的 IP 地址。要找到它,只需运行
adb shell ip -f inet addr show wlan0
现在您可以连接到您的设备。在您的 VM 客户端上,运行:
adb connect <Device IP>:5555
From here all my adb shell, logcat, and monkey sessions ran without interruption!
从这里开始,我所有的 adb shell、logcat 和monkey 会话都不间断地运行!
回答by zoli
I had the same problem and it made me search for about 2 hours.I tried several approaches including all above solutions but none of them worked
我遇到了同样的问题,这让我搜索了大约 2 个小时。我尝试了几种方法,包括上述所有解决方案,但都没有奏效
My situation
我的情况
My host was archlinux and the guest was windows 8.1 I was running flash builder in windows but the usb debugging wouldn't work and my connected device was nexus 5 so i followed these steps
我的主机是 archlinux,来宾是 windows 8.1 我在 windows 中运行 flash builder 但 USB 调试不起作用,我连接的设备是 nexus 5 所以我按照这些步骤
- Enable USB controller from virtualbox menu Machine-->settings-->usb enable usb controller and enable USB 2.0 (EHCI) controller then add your device(click on + button on right side) then start your virtual machine
- Now you should download the latest google usb drivers from hereand extract it
- Go to control panel device manager right click on your device go to driver tab and click on Update driver there should be two option one search automaticly ... and another browse my computer choose the second one and give the extracted file path(check the Included subfolders option) after this your device should be recognized by windows and there will be a message in your android device for allowing access to your device
- 从虚拟机菜单启用 USB 控制器 Machine-->settings-->usb 启用 USB 控制器并启用 USB 2.0 (EHCI) 控制器然后添加您的设备(单击右侧的 + 按钮)然后启动您的虚拟机
- 现在你应该从这里下载最新的谷歌 USB 驱动程序并解压
- 转到控制面板设备管理器右键单击您的设备转到驱动程序选项卡并单击更新驱动程序应该有两个选项一个自动搜索...另一个浏览我的计算机选择第二个并提供提取的文件路径(检查包括子文件夹选项)之后,Windows 应该可以识别您的设备,并且您的 android 设备中会显示一条消息,允许访问您的设备
In my case i should copy the drivers to flashbuilder android drivers path too but maybe for your situation this isn't necessary
在我的情况下,我也应该将驱动程序复制到 flashbuilder android 驱动程序路径,但也许对于您的情况,这不是必需的
回答by Enis
I had same problem with VirtualBox 5.0.14 and solution provided by Stephen Niedzielski didn't work for me. If someone is still interested can try this solution.
我在 VirtualBox 5.0.14 上遇到了同样的问题,Stephen Niedzielski 提供的解决方案对我不起作用。如果有人仍然感兴趣可以尝试这个解决方案。
My environment is:
我的环境是:
- Host OS: Mac OS 10.11
- Guest OS in VirtualBox: Ubuntu 14.04
- Device: Galaxy S5 running Cyanomodgen 12.1
- 主机操作系统:Mac OS 10.11
- VirtualBox 中的来宾操作系统:Ubuntu 14.04
- 设备:运行 Cyanomodgen 12.1 的 Galaxy S5
Start a wifi hotspot in host OS and connect there the android device, or connect both in a router. In virtualbox set network mode for guest OS to 'Bridged Adapter' and then select name of Wifi adapter. In that way host OS and android device are in same network.
在主机操作系统中启动 wifi 热点并在那里连接 android 设备,或在路由器中连接两者。在 virtualbox 中,将来宾操作系统的网络模式设置为“桥接适配器”,然后选择 Wifi 适配器的名称。这样主机操作系统和安卓设备就在同一个网络中。
Then, in android device activate 'Developer mode' and enable 'ADB over network', provided from Cyanomodgen. (For devices with original version of android, google how to use ADV over network if possible) In host OS run 'adb kill-server' and make sure no instance of ADB is running. Then run command 'adb connect 192.168.2.12', ip of android device.
然后,在 android 设备中激活“开发者模式”并启用 Cyanomodgen 提供的“ADB over network”。(对于具有原始版本 android 的设备,如果可能的话,谷歌如何通过网络使用 ADV)在主机操作系统中运行“adb kill-server”并确保没有 ADB 实例正在运行。然后运行命令'adb connect 192.168.2.12',android设备的ip。
Congrats :) Now you are are able to use ADB with android device to take logcat or pull and push files.
恭喜 :) 现在您可以在 Android 设备上使用 ADB 来获取 logcat 或拉取和推送文件。
回答by morphatic
I'm posting this here for people with a setup similar to mine:
我在此处为设置与我类似的人发布此信息:
- Host OS: OSX (10.15.4 Catalina)
- Client OS: Windows 10 Enterprise (v1903 build 18362.752)
- VirtualBox v6.1.4
- Android Studio v3.6.2
- 主机操作系统:OSX(10.15.4 Catalina)
- 客户端操作系统:Windows 10 Enterprise (v1903 build 18362.752)
- VirtualBox v6.1.4
- 安卓工作室 v3.6.2
To allow Android Studio in client OS (Windows) to connect to an Android phone (using adb) via USB:
允许客户端操作系统 (Windows) 中的 Android Studio 通过 USB 连接到 Android 手机(使用 adb):
- Update to latest version of VirtualBox (currently 6.1.4)
- Install VirtualBox Extension Pack from here
- With the VM powered down, go to Settings > Ports > USB and make sure the USB 3.0 (xHCI) Controller is enabled, then click "OK"
- From the host OS(i.e. OSX) open up a terminal and start
adb
and confirm that the phone is connected by runningadb devices
- Start up the VM, and from the VirtualBox menu select Devices > USB > [YOUR PHONE] to connect your host OS USB port to the VM
- Open up a terminal on the client OS (Windows) and run
adb devices
to confirm that the adb server is running and can find your phone. You might need to add the Android platform tools to your PATH (mine was at C:\Users\Username\AppData\Local\Android\Sdk\platform-tools). This step might be optional--I suspect that Android Studio might start upadb
on its own. - Start up Android Studio in the client OS (Windows) and go to File > Settings > Appearance and Behavior > System Settings > Android SDK > SDK Tools and make sure that the Google USB Driver is installed (see screenshot here)
- 更新到最新版本的 VirtualBox(当前为 6.1.4)
- 从这里安装 VirtualBox 扩展包
- 关闭VM后,转到设置 > 端口 > USB 并确保启用了 USB 3.0 (xHCI) 控制器,然后单击“确定”
- 从主机操作系统(即 OSX)打开终端并启动
adb
并通过运行确认手机已连接adb devices
- 启动 VM,然后从 VirtualBox 菜单中选择 Devices > USB > [YOUR PHONE] 将主机操作系统 USB 端口连接到 VM
- 在客户端操作系统 (Windows) 上打开终端并运行
adb devices
以确认 adb 服务器正在运行并且可以找到您的手机。您可能需要将 Android 平台工具添加到您的 PATH(我的位于 C:\Users\Username\AppData\Local\Android\Sdk\platform-tools)。这一步可能是可选的——我怀疑 Android Studio 可能会adb
自行启动。 - 在客户端操作系统 (Windows) 中启动 Android Studio 并转到文件 > 设置 > 外观和行为 > 系统设置 > Android SDK > SDK 工具并确保已安装 Google USB 驱动程序(请参阅此处的屏幕截图)
After I did these things, Android Studio was able to detect my phone and run/debug apps using that device.
完成这些操作后,Android Studio 能够检测到我的手机并使用该设备运行/调试应用程序。
Hope this helps!
希望这可以帮助!