Stetho利器

 Android开发中的又一件利器——Stetho。

 Stetho is a sophisticated debug bridge for Android applications. When enabled, developers have access to the Chrome Developer Tools feature natively part of the Chrome desktop browser. Developers can also choose to enable the optional dump app tool which offers a powerful command-line interface to application internals.
 Once you complete the set-up instructions below, just start your app and point your laptop browser to chrome://inspect. Click the "Inspect" button to begin.

Download

Download the latest JARs or grab via Gradle:

compile 'com.facebook.stetho:stetho:1.3.1'

 Only the main stetho dependency is strictly required; however, you may also wish to use one of the network helpers:

compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'

Putting it together
 Integrating with Stetho is intended to be seamless and straightforward for most existing Android applications. There is a simple initialization step which occurs in your Application class:

public class MyApplication extends Application { 
  public void onCreate() { 
    super.onCreate(); 
    Stetho.initializeWithDefaults(this);
  }
}

 This brings up most of the default configuration but does not enable some additional hooks (most notably, network inspection). See below for specific details on individual subsystems.

Enable network inspection
 If you are using the popular OkHttp library at the 3.x release, you can use the Interceptors system to automatically hook into your existing stack. This is currently the simplest and most straightforward way to enable network inspection:

new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .build()

 Note that okhttp 2.x will work as well, but with slightly different syntax and you must use the stetho-okhttp
artifact (notstetho-okhttp3).

 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.
 See the stetho-sample project for more details.
Going further

Custom dumpapp plugins
 Custom plugins are the preferred means of extending the dumpapp
system and can be added easily during configuration. Simply replace your configuration step as such:

Stetho.initialize(Stetho.newInitializerBuilder(context) .enableDumpapp(new DumperPluginsProvider() {
  @Override public Iterable get() {
    return new Stetho.DefaultDumperPluginsBuilder(context) .provide(new MyDumperPlugin()) .finish(); 
  } 
}).enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context)) .build())

 See the stetho-sample project for more details.

 这是我使用时候的简单配置,在app/build.gradle下:

compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp:1.1.0'

 这里我老早就用了Retrofit,所以不知道在声明okhttp有什么影响。

 另外在Application下面,onCreate()中:

Stetho.initialize(Stetho.newInitializerBuilder(this).enableDumpapp(Stetho.defaultDumperPluginsProvider(this)).enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this)).build());

 这时打开Chrome,地址栏输入:chrome://inspect/,就可以开始剖析你的应用了!

你可能感兴趣的:(Stetho利器)