如何使 adbd 传输在 android 模拟器中侦听 tcp 端口?

How do I make adbd transport to listen on a tcp port in android emulator?

我想从我的应用连接到 adb shell。为了实现它,我需要 adbd 传输来监听 tcp 端口。

使用真实设备不是解决方案,我想用 android 模拟器专门测试我的应用程序。

我创建并 运行 模拟器:

avdmanager create avd -n phone -d pixel_xl  -k "system-images;android-30;google_apis_playstore;x86_64"
emulator @phone

我运行adb tcpip 5555。虽然命令 运行s successfully adbd transport 没有公开。

adb shell logcat | grep adbd
01-15 15:40:23.786  2013  2013 I adbd    : Setup mdns on port= 5555
01-15 15:40:23.787  2013  2032 I adbd    : Waiting for persist.adb.tls_server.enable=1
01-15 15:40:23.787  2013  2013 I adbd    : adbd listening on tcp:5555
01-15 15:40:23.788  2013  2013 I adbd    : adbd listening on vsock:5555
01-15 15:40:23.798  2013  2040 I adbd    : host-14: read thread spawning
01-15 15:40:23.799  2013  2013 I adbd    : host-14: already offline
01-15 15:40:23.799  2013  2013 I adbd    : Calling send_auth_request...
01-15 15:40:23.806  2013  2041 I adbd    : host-14: write thread spawning
01-15 15:40:23.808  2013  2013 I adbd    : adbd_auth: loading keys from /data/misc/adb/adb_keys
01-15 15:40:23.809  2013  2013 I adbd    : adb client authorized
01-15 15:40:23.818  2013  2033 I adbd    : adbd_auth: received a new framework connection
01-15 15:40:23.819  2013  2033 I adbd    : adbd_auth: received new framework fd 18 (current = -1)
01-15 15:40:23.819  2013  2033 I adbd    : adbd_auth: marking framework writable
01-15 15:40:24.793  2013  2034 I adbd    : adbd mDNS service _adb._tcp registered: 1
01-15 15:43:09.598  2013  6477 I adbd    : adbd restarting in TCP mode (port = 5555)
01-15 15:43:09.635  6478  6479 I adbd    : Waiting for persist.adb.tls_server.enable=1
01-15 15:43:09.635  6478  6478 I adbd    : Setup mdns on port= 5555
01-15 15:43:09.635  6478  6478 I adbd    : adbd listening on tcp:5555
01-15 15:43:09.635  6478  6478 I adbd    : adbd listening on vsock:5555
01-15 15:43:09.635  6478  6481 I adbd    : adbd mDNS service _adb._tcp registered: 1
01-15 15:43:10.856  6478  6485 I adbd    : host-12: read thread spawning
01-15 15:43:10.856  6478  6486 I adbd    : host-12: write thread spawning
01-15 15:43:10.857  6478  6478 I adbd    : host-12: already offline
01-15 15:43:10.857  6478  6478 I adbd    : Calling send_auth_request...
01-15 15:43:10.859  6478  6478 I adbd    : adbd_auth: loading keys from /data/misc/adb/adb_keys
01-15 15:43:10.860  6478  6478 I adbd    : adb client authorized
01-15 15:43:11.607  6478  6480 I adbd    : adbd_auth: received a new framework connection
01-15 15:43:11.607  6478  6480 I adbd    : adbd_auth: received new framework fd 18 (current = -1)
01-15 15:43:11.607  6478  6480 I adbd    : adbd_auth: marking framework writable
adb shell netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program Name
tcp        0      0 10.0.2.16:54912         172.217.16.42:443       ESTABLISHED -
tcp        0      0 10.0.2.16:60088         172.217.20.202:443      ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:43504  ::ffff:142.250.75.3:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:43502  ::ffff:142.250.75.3:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:44476  ::ffff:64.233.164.:5228 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:59130  ::ffff:142.250.203.:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:50832  ::ffff:172.217.16.1:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:50836  ::ffff:172.217.16.1:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:35828  ::ffff:216.58.215.1:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:48984  ::ffff:216.58.215.7:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:49628  ::ffff:172.217.16.4:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:60070  ::ffff:172.217.20.2:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:43942  ::ffff:216.58.215.6:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:57422  ::ffff:142.250.203.:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:54880  ::ffff:172.217.16.4:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:54876  ::ffff:172.217.16.4:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:43506  ::ffff:142.250.75.3:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:60054  ::ffff:172.217.20.2:443 ESTABLISHED -
tcp6       0      0 ::ffff:10.0.2.16:60072  ::ffff:172.217.20.2:443 ESTABLISHED -
udp     4608      0 10.0.2.16:68            10.0.2.2:67             ESTABLISHED -
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           -
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           -
udp        0      0 0.0.0.0:50490           0.0.0.0:*                           -
udp6       0      0 :::35263                :::*                                -
udp6       0      0 :::5228                 :::*                                -
udp6       0      0 :::5353                 :::*                                -
udp6       0      0 :::5353                 :::*                                -
udp6       0      0 :::5353                 :::*                                -
adb shell nc localhost 5555
nc: connect: Connection refused

我也试过使用不同的系统映像但没有成功:

avdmanager create avd -n tv -d tv_720p -k "system-images;android-28;android-tv;x86"

如何使用 tcp 传输连接到模拟器 adb shell?

没有支持的方法来执行此操作。在模拟器中时(ro.kernel.qemu=1)adb 使用 QEMUD 管道而不是常规套接字。

https://android.googlesource.com/platform/system/core/+/refs/tags/android-9.0.0_r61/adb/transport_local.cpp#388

编辑

我成功了。 我替换了

func = use_qemu_goldfish() ? qemu_socket_thread : server_socket_thread;

std::thread(qemu_socket_thread, port).detach();
std::thread(server_socket_thread, port).detach();

由于某些原因,如果您 运行 仅 server_socket_thread,adb 将不起作用。