非死book的Android调试工具Stetho介绍

jopen 9年前

Stetho是非死book出品的一个强大的Android调试工具,使用该工具你可以在Chrome Developer Tools查看App的布局,网络请求,sqlite,preference,一切都是可视化的操作,无须自己在去使用adb,也不需要root你的设备。使用的方式很简单,配置好之后,在Chrome地址栏输入chrome://inspect (哈哈,和webview 远程调试的方式一样)。废话少说,先来看看效果图。

inspector-discovery.png

inspector-sqlite.png

inspector-elements.png

inspector-network.png

怎么样,是不是看起来很碉堡,尤其是如果你做过web开发,肯定感觉超级熟悉哈。下面我们就来看看怎么使用这么碉堡的工具。

配置

添加gradle引用

compile 'com.非死book.stetho:stetho:1.1.0'

只有stetho库是必须的,想查看网络请求的话,需要使用下面的两个库之一(看你的网络库用的是okhttp还是urlconnection)

compile 'com.非死book.stetho:stetho-okhttp:1.1.0'

或者

compile 'com.非死book.stetho:stetho-urlconnection:1.1.0'

修改代码

public class MyApplication extends Application {    public void onCreate() {      super.onCreate();      Stetho.initialize(          Stetho.newInitializerBuilder(this)              .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))              .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))              .build());    }  }

开启网络请求查看

若果你使用了okhttp库,参考下面的代码:

OkHttpClient client = new OkHttpClient();  client.networkInterceptors().add(new StethoInterceptor());

如果你使用了HttpURLConnection,你需要使用StethoURLConnectionManager来帮忙。

更多细节请参考stetho源码中的stetho-sample。

来自:http://www.androidcn.org/topic/552fabaa8ca8a1e07687e999