首先,这篇文章要说的不是大名鼎鼎的Inspector。
作为一名偶尔串客写写前端代码,但对前端又非常不熟练的Android终端开发,经常需要改两行代码就要看看页面效果对不对。如果是兼容桌面浏览器的页面还好办,在本地启动服务,写两句代码refresh一下就可以看到效果了,但是更多的时候,我写的页面是必须在APP中加载,用到APP提供的JsApi的,桌面浏览器根本没法使用页面的功能。
怎样方便的调试呢?
- 手机和电脑连到同一网段?公司网络环境不允许!
- 电脑插一个随身Wifi开一个热点?公司安全策略不允许!
- 发布到远程Server上去?要疯掉!
想起来之前写ReactNative代码时,手机用USB连电脑,手机就可以直接访问电脑启动的NodeJS服务,恍然大悟,原来可以这样!
首先,电脑要有ADB,作为一名Android终端开发,这个是必备的。
然后,终端中输入执行一下adb,列出的帮助文档中,有这样一段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
adb
forward
--
list
-
list
all
forward
socket
connections
.
the
format
is
a
list
of
lines
with
the
following
format
:
<
serial
>
" "
<
local
>
" "
<
remote
>
"\n"
adb
forward
<
local
>
<
remote
>
-
forward
socket
connections
forward
specs
are
one
of
:
tcp
:
<
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
)
adb
forward
--
no
-
rebind
<
local
>
<
remote
>
-
same
as
'adb forward
if
<
local
>
is
already
forwarded
adb
forward
--
remove
<
local
>
-
remove
a
specific
forward
socket
connection
adb
forward
--
remove
-
all
-
remove
all
forward
socket
connections
adb
reverse
--
list
-
list
all
reverse
socket
connections
from
device
adb
reverse
<
remote
>
<
local
>
-
reverse
socket
connections
reverse
specs
are
one
of
:
tcp
:
<
port
>
localabstract
:
<
unix
domain
socket
name
>
localreserved
:
<
unix
domain
socket
name
>
localfilesystem
:
<
unix
domain
socket
name
>
adb
reverse
--
no
-
rebind
<
remote
>
<
local
>
-
same
as
'adb reverse
if
<
remote
>
is
already
reversed
.
adb
reverse
--
remove
<
remote
>
-
remove
a
specific
reversed
socket
connection
adb
reverse
--
remove
-
all
-
remove
all
reversed
socket
connections
from
device
|
原来,Android允许我们通过ADB,把Android上的某个端口映射到电脑(adb forward),或者把电脑的某个端口映射到Android系统(adb reverse)。
假设电脑上开启的服务,监听的端口为8000。Android手机通过USB连接电脑后,执行 adb reversetcp:8000 tcp:8000,然后在手机中访问127.0.0.1:8000,就可以访问到电脑上启动的服务了,完美解决写两句代码就要看效果的问题!
http://blog.xiaoyu.im/post_678.html