Stetho 是facebook的一个项目(A debug bridge for Android applications)主要用于项目的调试
项目主页:http://facebook.github.io/stetho/#download
使用步骤: (首先先升级谷歌浏览器到最新)
- 在gradle里添加
// Gradle dependency on Stetho
dependencies {
compile 'com.facebook.stetho:stetho:1.5.0'
}
复制代码
用maven的使用下面的
<dependency>
<groupid>com.facebook.stetho</groupid>
<artifactid>stetho</artifactid>
<version>1.5.0</version>
</dependency>
复制代码
- 初始化
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
复制代码
添加完之后 先来看看怎么进入调试的界面
在chrome浏览器中输入地址:chrome://inspect 会看到如下
查看数据库
查看ui结构
查看network
如果想要拦截network的话 需要另外安装一下东西
dependencies {
compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'
}
复制代码
或者
dependencies {
compile 'com.facebook.stetho:stetho-okhttp:1.5.0'
}
复制代码
或者
dependencies {
compile 'com.facebook.stetho:stetho-urlconnection:1.5.0'
}
复制代码
根据官网的描述,如果想要启动拦截的话,需要启用拦截
使用 OkHttp 2.x
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
复制代码
使用 OkHttp 3.x
new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.build();
复制代码
在官网中,如果你用的是HttpURLConnection,你可以使用StethoURLConnectionManager去弄,常规的来说,你必须要明确指定添加Accept-Encoding: gzip头,然后手动处理被压缩的response,以便于Stetho减少处理负担 (原文:If you are using HttpURLConnection, you can use StethoURLConnectionManager to assist with integration though you should be aware that there are some caveats with this approach. In particular, you must explicitly add Accept-Encoding: gzip to the request headers and manually handle compressed responses in order for Stetho to report compressed payload sizes.)
想要更多细节,要查看github的sample
dumpapp
除了以上之外,还有dumpapp
Stetho.initialize(Stetho.newInitializerBuilder(context)
.enableDumpapp(new DumperPluginsProvider() {
@Override
public Iterable<DumperPlugin> get() {
return new Stetho.DefaultDumperPluginsBuilder(context)
.provide(new MyDumperPlugin())
.finish();
}
})
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
.build());
复制代码
Javascript Console
还可以使用js和应用设置sdk交互
我在尝试的时候报错 说缺少插件 然后去官方demo里面发现还要安装东西
debugCompile project(':stetho-js-rhino')
复制代码
根据注释 他们说这是一个很大的jar包 所以默认是关闭的 安装这个后应该就可以正常用了