Android Studio开发局域网其他设备如何连接官方模拟器

1. 背景

       1. 现在的Android 官方的模拟器越来越好用了,而且Android Studio也默认集成了模拟器工具集,使用起来是很方便的,但是苦于其他设备联网测试时,如果我们在安卓应用中开放服端口务,那么局域网的其他设备是访问不了的。 这是因为android官方的模拟器没有提供桥接模式的方式, 模拟器的IP对外是隐藏的。
        之前用Genymotion模拟器的时候,是可以设置成桥接模式,模拟器使用的局域网网段的IP地址, 所以其他机器就能访问到。

        2. 做Android开发,模拟器是必备品,而Android模拟器的IP和电脑总不是一个网段的,想要进行测试非常麻烦,甚至很多时候模拟器没有办法连接互联网,本文详细的描述了Android模拟器IP设置的完整步骤。

2. 解决方案adb forward

解决方案介绍

Android官方为我们提供一种adb重定向方式可以在本机访问Android模拟器。 只需要使用adb工具添加一个转发规则即可。

adb forward tcp:本机端口 tcp:模拟器端口

完成以上步骤后,就可以通过 localhost:本机端口 或 127.0.0.1:本机端口来访问模拟器提供的服务了。

其他可能需要的adb forward命令

# 列出所有转发规则
adb forward --list
# 删除某个转发规则
adb forward --remove tcp:11111
# 删除所有转发规则
adb forward --remove-all

adb forward原理图

3.存在问题

但是,有时,我们不止是要本机访问,可能网络上的其他机器也需要能访问才行,这时候我们如果想要通过,如:192.168.xx.xx:本机端口 这样的方式来访问,是无法访问到服务的,这是因为adb 提供的forward转发功能只针对了localhost和127.0.0.1做了转发,这是写死的。 我们无法改变。

网络上其实也有办法就是修改adb源码,然后重新打包,这样的方式无疑门槛是比较高的,而且后期adb升级版本又不行了,还得重新打包,所以是不推荐使用这种方式的,具体参考如下:

用VNC:webkey2.9,做adb forward映射后,在USB模式下可访问,而不用原来的wifi。但是forward命令只是默认绑定localhost,用本机ipv4地址则不行。这样就使得不能从其他
的机器来远程操作设备了。于是修改adb源码使其都绑定。

修改如下:
1.在android adb源码里找到文件夹:~/ANDROID_SOURCE_CODE/system/core/adb/adb.c
2.修改int local_name_to_fd(const char *name)方法里的代码:
 ret = socket_loopback_server(port, SOCK_STREAM);
  修改为:
 ret = socket_inaddr_any_server(port, SOCK_STREAM);
  即可。
3.重新编译adb(可参考【在Linux下编译Windows版本的adb和fastboot】)
————————————————
版权声明:本文为CSDN博主「老鼠尼奥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/NEO_Rat/article/details/8540632

4. 存在问题解决方案

这时候怎么办呢? 我就想啊, 因为  本机127.0.0.1:本机端口 是可以访问的, 如果我们能将来自于  192.168.xx.xx:本机端口 的访问请求转发到127.0.0.1:本机端口 是不是就解决问题了呢?

于是我开始寻找如何在Windows上添加这个端口转发功能, 果然让我给找到了。 我们只需要在Windows PowerShell(管理员)添加如下端口转发规则即可,这里需要注意要有管理员权限才可以的。命令如下:

netsh interface portproxy add v4tov4 listenport=8110 listenaddress=192.168.1.66 connectport=8110 connectaddress=127.0.0.1

其他可能需要的指令参考

增加端口映射,将10.10.10.10的11111映射到10.10.10.11的80端口 
netsh interface portproxy add v4tov4 listenport=11111 listenaddress=10.10.10.10 connectport=80 connectaddress=10.10.10.11

删除端口映射 
netsh interface portproxy del v4tov4 listenport=11111 listenaddress=10.10.10.10

查看已存在的端口映射 
netsh interface portproxy show v4tov4

这时候我再通过192.168.xx.xx:本机端口来访问,就可以访问了,哈哈~~~~

5. ADB命令参考

global options:
 -a         listen on all network interfaces, not just localhost
 -d         use USB device (error if multiple devices connected)
 -e         use TCP/IP device (error if multiple TCP/IP devices available)
 -s SERIAL  use device with given serial (overrides $ANDROID_SERIAL)
 -t ID      use device with given transport id
 -H         name of adb server host [default=localhost]
 -P         port of adb server [default=5037]
 -L SOCKET  listen on given socket for adb server [default=tcp:localhost:5037]

general commands:
 devices [-l]             list connected devices (-l for long output)
 help                     show this help message
 version                  show version num

networking:
 connect HOST[:PORT]      connect to a device via TCP/IP [default port=5555]
 disconnect [HOST[:PORT]]
     disconnect from given TCP/IP device [default port=5555], or all
 forward --list           list all forward socket connections
 forward [--no-rebind] LOCAL REMOTE
     forward socket connection using:
       tcp:<port> (<local> may be "tcp:0" to pick any open port)
       localabstract:<unix domain socket name>
       localreserved:<unix domain socket name>
       localfilesystem:<unix domain socket name>
       dev:<character device name>
       jdwp:<process pid> (remote only)
 forward --remove LOCAL   remove specific forward socket connection
 forward --remove-all     remove all forward socket connections
 ppp TTY [PARAMETER...]   run PPP over USB
 reverse --list           list all reverse socket connections from device
 reverse [--no-rebind] REMOTE LOCAL
     reverse socket connection using:
       tcp:<port> (<remote> may be "tcp:0" to pick any open port)
       localabstract:<unix domain socket name>
       localreserved:<unix domain socket name>
       localfilesystem:<unix domain socket name>
 reverse --remove REMOTE  remove specific reverse socket connection
 reverse --remove-all     remove all reverse socket connections from device

file transfer:
 push [--sync] LOCAL... REMOTE
     copy local files/directories to device
     --sync: only push files that are newer on the host than the device
 pull [-a] REMOTE... LOCAL
     copy files/dirs from device
     -a: preserve file timestamp and mode
 sync [all|data|odm|oem|product|system|vendor]
     sync a local build from $ANDROID_PRODUCT_OUT to the device (default all)
     -l: list but don't copy

shell:
 shell [-e ESCAPE] [-n] [-Tt] [-x] [COMMAND...]
     run remote shell command (interactive shell if no command given)
     -e: choose escape character, or "none"; default '~'
     -n: don't read from stdin
     -T: disable PTY allocation
     -t: force PTY allocation
     -x: disable remote exit codes and stdout/stderr separation
 emu COMMAND              run emulator console command

app installation:
 install [-lrtsdg] [--instant] PACKAGE
 install-multiple [-lrtsdpg] [--instant] PACKAGE...
     push package(s) to the device and install them
     -l: forward lock application
     -r: replace existing application
     -t: allow test packages
     -s: install application on sdcard
     -d: allow version code downgrade (debuggable packages only)
     -p: partial application install (install-multiple only)
     -g: grant all runtime permissions
     --instant: cause the app to be installed as an ephemeral install app
 uninstall [-k] PACKAGE
     remove this app package from the device
     '-k': keep the data and cache directories

backup/restore:
   to show usage run "adb shell bu help"

debugging:
 bugreport [PATH]
     write bugreport to given PATH [default=bugreport.zip];
     if PATH is a directory, the bug report is saved in that directory.
     devices that don't support zipped bug reports output to stdout.
 jdwp                     list pids of processes hosting a JDWP transport
 logcat                   show device log (logcat --help for more)

security:
 disable-verity           disable dm-verity checking on userdebug builds
 enable-verity            re-enable dm-verity checking on userdebug builds
 keygen FILE
     generate adb public/private key; private key stored in FILE,
     public key stored in FILE.pub (existing files overwritten)

scripting:
 wait-for[-TRANSPORT]-STATE
     wait for device to be in the given state
     State: device, recovery, sideload, or bootloader
     Transport: usb, local, or any [default=any]
 get-state                print offline | bootloader | device
 get-serialno             print <serial-number>
 get-devpath              print <device-path>
 remount                  remount partitions read-write
 reboot [bootloader|recovery|sideload|sideload-auto-reboot]
     reboot the device; defaults to booting system image but
     supports bootloader and recovery too. sideload reboots
     into recovery and automatically starts sideload mode,
     sideload-auto-reboot is the same but reboots after sideloading.
 sideload OTAPACKAGE      sideload the given full OTA package
 root                     restart adbd with root permissions
 unroot                   restart adbd without root permissions
 usb                      restart adb server listening on USB
 tcpip PORT               restart adb server listening on TCP on PORT

internal debugging:
 start-server             ensure that there is a server running
 kill-server              kill the server if it is running
 reconnect                kick connection from host side to force reconnect
 reconnect device         kick connection from device side to force reconnect
 reconnect offline        reset offline/unauthorized devices to force reconnect

environment variables:
 $ADB_TRACE
     comma-separated list of debug info to log:
     all,adb,sockets,packets,rwx,usb,sync,sysdeps,transport,jdwp
 $ADB_VENDOR_KEYS         colon-separated list of keys (files or directories)
 $ANDROID_SERIAL          serial number to connect to (see -s)
 $ANDROID_LOG_TAGS        tags to be used by logcat (see logcat --help)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值