Stetho 简化 Android 调试 (一)
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1' // 使用 okhttp3 作为网络底层库
compile 'com.facebook.stetho:stetho-okhttp:1.3.1' // 使用 okhttp 作为网络底层库
compile 'com.facebook.stetho:stetho-urlconnection:1.3.1' // 使用 urlconnection 作为网络底层库
}
在 Application 中初始化
public class MyApplication extends Application {
public static OkHttpClient okHttpClient;
public void onCreate() {
super.onCreate();
// 一般使用默认初始化配置足够使用
Stetho.initializeWithDefaults(this);
// 如果需要查看网络请求相关信息(以使用 okhttp3 为例)
initOkHttpClient();
}
private void initOkHttp() {
okHttpClient = new OkHttpClient()
.newBuilder()
.addNetworkInterceptor(new StethoInterceptor()) // 这里添加一个拦截器即可
.build();
}
}
至此,我们的应用配置完毕。是不是超级简单?
接下来看怎么使用 Developer Tools 查看应用的各种信息。
3.打开 Chrome 输入 chrome://inspect
========================================================================================

inspect
如果第 1、2 步配置正确的话,就可以看到以上页面。点击 inspec:

Developer Tools
注意:如果第一次启动,这个页面有可能会一片空白。因为 Developer Tools 在连接远程服务器,这个连接需要科学上网。如果页面加载成功后再次打开,就不需要科学上网了。
Developer Tools 功能说明
1.Elements

就像 uiautomatorviewer.bat 工具一样,可以查看页面视图的层级结构。
2.Network

网络请求的会话信息都包含在这里,可以点击单个会话到详情,查看具体的报文信息。如下:

3.Resources
在这个选项卡里面,就可以查看应用保存的信息,包括 SharePreference、数据库,并且可以执行 SQL 语句,直接修改 SharePreference 的内容。如下:



该控制台允许使用 JavaScript 代码与应用程序或 Android SDK 交互。如下:

Console Result
context.getResources().getString(R.string.app_name);
运行以上代码可以得到应用下对应的字符串资源。
importPackage(android.util);
Log.d("WaitingAnd", "chuang");
运行以上代码可以在 Logcat 打印一条日志。
评论