Fecebook Stetho工具介绍
stetho是Facebook推出的安卓APP网络诊断和数据监控的工具,接入方便,功能强大,是开发者必备的好工具。
主要功能包括:
- 查看App的布局
- 网络请求抓包
- 数据库、sp文件查看
- 自定义dumpapp插件
- 对于JavaScript的支持
无需root,只要能通过adb连接设备,操作方便。
接入方法
gradle配置
因为目前我们的项目中已经集成了okhttp,只需要在build.gradle添加如下两行配置
dependencies {//...compile 'com.facebook.stetho:stetho-js-rhino:1.3.1'compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
初始化
在Application类中完成初始化
private void MyApplicationCreate() {// ...Stetho.initializeWithDefaults(mContext);}
- 1
- 2
- 3
- 4
使用功能
- adb方式连接到设备
- 运行debug模式的app
- 在Chrome浏览器地址栏中输入chrome://inspect
- 选择需要inspect的应用进程
查看App的布局
网络诊断
给OkHttpClient添加拦截器。
new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();
- 1
- 2
- 3
主要功能有包括下载图片的预览,JSON数据查看,网络请求内容和返回内容
数据库、sp文件查看
自定义dumpapp插件
Stetho.initialize(Stetho.newInitializerBuilder(context).enableDumpapp(new DumperPluginsProvider() {@Overridepublic Iterable<DumperPlugin> get() {return new Stetho.DefaultDumperPluginsBuilder(context).provide(new HelloWorldDumperPlugin()).provide(new APODDumperPlugin(context.getContentResolver())).finish();}}).enableWebKitInspector(new ExtInspectorModulesProvider(context)).build());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
其中HelloWorldDumperPlugin和APODDumperPlugin是自定义的插件,具体内容可以参考Stetho提供的sample程序
运行dumpapp脚本后以达到与app交互通信的效果。
$ ./dumpapp -l
apod
crash
files
hello
hprof
prefs
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
原理简介
其中dumpapp是一个python脚本,通信的方式使用的是android提供的smartsocket接口
--- smartsockets -------------------------------------------------------
Port 5037 is used for smart sockets which allow a client on the host
side to request access to a service in the host adb daemon or in the
remote (device) daemon. The service is requested by ascii name,
preceeded by a 4 digit hex length. Upon successful connection an
"OKAY" response is sent, otherwise a "FAIL" message is returned. Once
connected the client is talking to that (remote or local) service.
client: <hex4> <service-name>
server: "OKAY"
client: <hex4> <service-name>
server: "FAIL" <hex4> <reason>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
使用PyCharm对Python进行断点调试:
这段脚本的功能就是通过读取/proc/net/unix
文件去找app设置的socket
1. 扫描android所有提供socket功能的设备,找到steho的devtools_remote
2. 建立与第一步找到的进程socket,然后通过smartsocket进行通信。
3. 设备上的app相当于一个服务器,脚本是客户端对它进行访问
后缀为_devtools_remote的socket是android留给chrome的后门。
// Note that _devtools_remote is a magic suffix understood by Chrome which //causes the discovery process to begin.
- 1
详细内容可以看这篇官方文档
这篇文档提供的例子是在命令行中输入下面的命令,就能在电脑上看到手机chrome中的内容了:
adb forward tcp:9222 localabstract:chrome_devtools_remote
打开的chrome-devtool其实是一个websocket连接。
private void handlePageList(LightHttpResponse response)throws JSONException {if (mPageListResponse == null) {JSONArray reply = new JSONArray();JSONObject page = new JSONObject();page.put("type", "app");page.put("title", makeTitle());page.put("id", PAGE_ID);page.put("description", "");page.put("webSocketDebuggerUrl", "ws://" + mInspectorPath);Uri chromeFrontendUrl = new Uri.Builder().scheme("http").authority("chrome-devtools-frontend.appspot.com").appendEncodedPath("serve_rev").appendEncodedPath(WEBKIT_REV).appendEncodedPath("devtools.html").appendQueryParameter("ws", mInspectorPath).build();page.put("devtoolsFrontendUrl", chromeFrontendUrl.toString());reply.put(page);mPageListResponse = LightHttpBody.create(reply.toString(), "application/json");}setSuccessfulResponse(response, mPageListResponse);
}
- 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
在android上的服务端socket写法,
详见LocalSocketServer类的listenOnAddress方法
private void listenOnAddress(String address) throws IOException {mServerSocket = bindToSocket(address);LogUtil.i("Listening on @" + address);while (!Thread.interrupted()) {try {// Use previously accepted socket the first time around, otherwise wait to accept another.LocalSocket socket = mServerSocket.accept();// Start worker threadThread t = new WorkerThread(socket, mSocketHandler);t.setName(WORKER_THREAD_NAME_PREFIX +"-" + mFriendlyName +"-" + mThreadId.incrementAndGet());t.setDaemon(true);t.start();} catch (SocketException se) {// ignore exception if interrupting the threadif (Thread.interrupted()) {break;}LogUtil.w(se, "I/O error");} catch (InterruptedIOException ex) {break;} catch (IOException e) {LogUtil.w(e, "I/O error initialising connection thread");break;}}LogUtil.i("Server shutdown on @" + address);}
- 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
- 30
- 31
- 32
- 33
对于JavaScript的支持
Chrome开发者工具原生支持JavaScript,所以Stetho也提供了JavaScript的支持。
通过在console中输入如下代码可以让设备app弹出一个toast
importPackage(android.widget);
importPackage(android.os);
var handler = new Handler(Looper.getMainLooper());
handler.post(function() { Toast.makeText(context, "Hello from JavaScript", Toast.LENGTH_LONG).show() });
- 1
- 2
- 3
- 4
更多玩法见Rhino on Stetho
相关链接
http://facebook.github.io/stetho/
https://github.com/facebook/stetho/tree/master/stetho-js-rhino
https://www.figotan.org/2016/04/18/using-stetho-to-diagnose-data-on-android/
https://developer.chrome.com/devtools/docs/remote-debugging-legacy
https://android.googlesource.com/platform/system/core/+/master/adb/protocol.txt
Fecebook Stetho工具介绍相关推荐
- Stetho工具介绍
stetho是Facebook推出的安卓APP网络诊断和数据监控的工具,接入方便,功能强大,是开发者必备的好工具. 主要功能包括: 查看App的布局 网络请求抓包 数据库.sp文件查看 自定义dump ...
- Stetho的介绍和使用
1.简介 stetho是facebook开发的一个开源库,Android应用通过引入stetho,可以在Chrome/Chromium浏览器监控查看网络请求.数据库.SharedPreferences ...
- Android 数据库查看工具Stetho使用介绍
Android 数据库查看工具Stetho使用介绍 前言 使用工具 使用说明 添加依赖 注册声明 查看数据库 注意 完事 前言 毫无疑问,在做数据库开发的时候,开发人员总想能实时看到数据库的操作是否成 ...
- 软件包管理 之 软件在线升级更新yum 图形工具介绍
作者:北南南北 来自:LinuxSir.Org 提要:yum 是Fedora/Redhat 软件包管理工具,包括文本命令行模式和图形模式:图形模式的yum也是基于文本模式的:目前yum图形前端程序主要 ...
- IDEA IntelliJ 开发工具介绍
IntelliJ IDEA 开发Java的IDE 官网:https://www.jetbrains.com/ 下面是该工具介绍,有兴趣的可以看下,基本一堆废话: 集成开发环境(IDE,Integrat ...
- postman 接口测试工具介绍
postman 接口测试工具介绍 https://www.cnblogs.com/fly_dragon/p/9186745.html
- Java代码缺陷自动分析工具介绍
Java代码缺陷自动分析工具介绍 ...
- 77.Linux系统日志,screen工具介绍
Linux系统日志 日志重要吗?必须的,没有日志我们怎么知道系统状况?没有日志如何排查一个trouble?日志记录了系统每天发生的各种各样的事情,你可以通过他来检查错误发生的原因,或者受到***时** ...
- Exchange Server 2003多服务器安装以及管理工具介绍
Exchange服务器系列课程之二--Exchange Server 2003多服务器安装以及管理工具介绍 http://www.z8soft.com/article/server| 2011年4月1 ...
最新文章
- HDFS副本放置策略和机架感知
- LG P4074 [WC2013] 糖果公园(带修莫队,树上莫队)
- Redis:03---Redis的启动与配置参数大全
- Python3网络爬虫开发实战分析Ajax爬取今日头条街拍美图
- Mac如何设置intellij idea中文
- win10最常用dos命令以及win+R即可运行的命令
- matlab代码 无标度网络 生成图,标准无标度网络matlab
- 中兴威武3android驱动,中兴威武3
- 小鸡模拟器显示无法连接服务器,小鸡模拟器安装不了怎么办
- VSS无法访问 (0x80072EFD) 转载
- 9.5 隐函数求导法则
- 世人笑我太疯癫,我笑他人看不穿
- Vue官网提供表单验证cnpm i vee-validate@2 --save
- pos机属于计算机系统吗,pos机与计算机有什么区别
- h200和gr1108_华三H3C GR1108-P 路由器性能极限
- Vue项目在ie浏览器打不开的解决办法
- Vue百度地图电子围栏
- PowerPC的字节序问题
- python rolling regression. 使用 Python 实现滚动回归
- Linux | 分布式版本控制工具Git【版本管理 + 远程仓库克隆】
热门文章
- Android CPU, Compilers, D8 R8
- 10个优秀的Python库,实用且有趣
- 由View的onAttachedToWindow引发的图片轮播问题探究
- 挂载Nginx配置文件
- 组合数学-中国剩余定理(孙子定理)
- LSB Python 脚本分享
- 玩推推需三思而后行,晒晒昆明结婚需要多少钱
- 如何在微信公众号编辑器发布免费好看的排版内容
- Linux重启网卡报Job for network.service failed because the control process exited with error code. 错误
- Hadoop学习第二天