|
| 1 | +# Stetho |
| 2 | +Stetho is a sophisticated debug bridge for Android applications. When enabled, |
| 3 | +developers have access to the Chrome Developer Tools feature natively part of |
| 4 | +the Chrome desktop browser. Developers can also choose to enable the optional |
| 5 | +`dumpapp` tool which offers a powerful command-line interface to application |
| 6 | +internals. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +### WebKit Inspector |
| 11 | +WebKit Inspector is the internal name of the Chrome Developer Tools feature. |
| 12 | +It is implemented using a client/server protocol which the Stetho software |
| 13 | +provides for your application. Once your application is integrated, simply |
| 14 | +navigate to `chrome://inspect` and click "Inspect" to get started! |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +#### Network inspection |
| 19 | +Network inspection is possible with the full spectrum of Chrome Developer Tools features, including image preview, JSON response helpers, and even exporting traces to the HAR format. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +#### Database inspection |
| 24 | +SQLite databases can be visualized and interactively explored with full read/write capabilities. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +### dumpapp |
| 29 | +Dumpapp extends beyond the Inspector UI features shown above to provide a much |
| 30 | +more extensible, command-line interface to application components. A default |
| 31 | +set of plugins is provided, but the real power of dumpapp is the ability to |
| 32 | +easily create your own! |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +## Integration |
| 37 | +Integrating with Stetho is intended to be seamless and straightforward for |
| 38 | +most existing Android applications. There is a simple initialization step |
| 39 | +which occurs in your `Application` class: |
| 40 | + |
| 41 | +```java |
| 42 | +public class MyApplication extends Application { |
| 43 | + public void onCreate() { |
| 44 | + super.onCreate(); |
| 45 | + Stetho.initialize( |
| 46 | + Stetho.newInitializerBuilder(this) |
| 47 | + .enableDumpapp(Stetho.defaultDumperPluginsProvider(this)) |
| 48 | + .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this)) |
| 49 | + .build()); |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +This brings up most of the default configuration but does not enable some |
| 55 | +additional hooks (most notably, network inspection). See below for specific |
| 56 | +details on individual subsystems. |
| 57 | + |
| 58 | +### Enable network inspection |
| 59 | +If you are using the popular [OkHttp](http://https://github.com/square/okio) |
| 60 | +library at the 2.2.x+ release, you can use the |
| 61 | +[Interceptors](https://github.com/square/okhttp/wiki/Interceptors) system to |
| 62 | +automatically hook into your existing stack. This is currently the simplest |
| 63 | +and most straightforward way to enable network inspection: |
| 64 | + |
| 65 | +```java |
| 66 | +OkHttpClient client = new OkHttpClient(); |
| 67 | +client.networkInterceptors().add(new StethoInterceptor()); |
| 68 | +``` |
| 69 | + |
| 70 | +If you are using any of other network stack options, you will need to manually |
| 71 | +provide data to the `NetworkEventReporter` interface. The general pattern for implementing this is: |
| 72 | + |
| 73 | +```java |
| 74 | +NetworkEventReporter reporter = NetworkEventReporterImpl.get(); |
| 75 | +// Important to check if it is enabled first so as not to add overhead to |
| 76 | +// the common case that is not under scrutiny. |
| 77 | +if (reporter.isEnabled()) { |
| 78 | + reporter.requestWillBeSent(new MyInspectorRequest(request)); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +See the `stetho-sample` project for more details. |
| 83 | + |
| 84 | +### Custom dumpapp plugins |
| 85 | +Custom plugins are the preferred means of extending the `dumpapp` system and |
| 86 | +can be added easily during configuration. Simply replace your configuration |
| 87 | +step as such: |
| 88 | + |
| 89 | +```java |
| 90 | +Stetho.initialize(Stetho.newInitializerBuilder(context) |
| 91 | + .enableDumpapp(new MyDumperPluginsProvider(context)) |
| 92 | + .build()) |
| 93 | + |
| 94 | +private static class MyDumperPluginsProvider implements DumperPluginsProvider { |
| 95 | + ... |
| 96 | + |
| 97 | + public Iterable<DumperPlugin> get() { |
| 98 | + ArrayList<DumperPlugin> plugins = new ArrayList<DumperPlugin>(); |
| 99 | + for (DumperPlugin defaultPlugin : Stetho.defaultDumperPluginsProvider(mContext).get()) { |
| 100 | + plugins.add(defaultPlugin); |
| 101 | + } |
| 102 | + plugins.add(new MyDumperPlugin()); |
| 103 | + return plugins; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +See the `stetho-sample` project for more details. |
| 109 | + |
| 110 | +## Improve Stetho! |
| 111 | +See the CONTRIBUTING.md file for how to help out. |
| 112 | + |
| 113 | +## License |
| 114 | +Stetho is BSD-licensed. We also provide an additional patent grant. |
0 commit comments