Stetho is one of Facebook's open source Android platform Debugging tools.
Stetho can now view the layout of the app via Chrome, sqlite,sharedpreference, without root phone. Network and so on. It also supports the creation of dump files.
A very important point to use Stetho is to make it clear that Stetho is a debugging tool. In theory, it is only possible to use the debug package, assuming that your app's data is exposed to the release package.
Our code is centered on this.
Core Code implementation:first to add a reference to Stetho
The Gradle file directly joins the Stetho dependency.
Compile ' com.facebook.stetho:stetho-okhttp:1.1.1 ' debugcompile ' com.facebook.stetho:stetho:1.1.1 '
Stetho's dependence. Only need to pack dependencies in debug mode, Release mode packaging is not required.
Stetho-okhttp is the reliance required to obtain network data. Because there are dependencies in project. Only be able to join the dependency of all modes. Be able to see the code behind it.
Next, create a androidmanifest and application for the debug package in a specific location
Location for example, under the main Projectsrc folder, create the Debug folder as seen. If you do not need the dump function, just need to create a new Androidmanifest file and application file.
Androidmanifest indicates that debug starts the app from Debugapplication.
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " package=" Com.debug.xsldebug "> <application tools:replace=" Android : Name " android:name=". Activity.message.XSLDebugApplication "/></manifest>
Debugapplication inherits the original application of the application and joins the Stetho to initialize the relevant code.
<pre name= "code" class= "Java" >public class Xsldebugapplication extends Xslapplication {@Override public void OnCreate () {super.oncreate (); Stetho.initialize (Stetho.newinitializerbuilder (this). Enabledumpapp (Stetho.defaultd Umperpluginsprovider (this)). Enablewebkitinspector (Stetho.defaultinspectormodulesprovider (This)) . build ()); } private static Class Xsldumperpluginsprovider implements Dumperpluginsprovider {private final Context Mcontex T Public Xsldumperpluginsprovider (Context context) {Mcontext = context; } @Override public iterable<dumperplugin> get () {arraylist<dumperplugin> plugins = n EW arraylist<dumperplugin> (); For (Dumperplugin DefaultPlugin:Stetho.defaultDumperPluginsProvider (mcontext). Get ()) {Plugins.add (defaul Tplugin); } plugins.Add (New Xsldumperplugin ()); return plugins; } }}
After the above settings, the app already supports Stetho's basic functionality. Ability to check with Chrome's device or visit chrome://inspect/#devices to find the device you're currently connected to
Assuming that you want to support viewing network information, you need to do the following additional configuration
With the Okhttp network framework, it is very convenient to integrate Stetho, assuming that httpurlconnection is used, it can be a lot of trouble. Our network framework is already okhttp2.2+. So it is very convenient to integrate the Stetho.
public string okhttppost (string url, list<basicnamevaluepair> namevaluepairs) { string result = stringutils.empty_string; Okhttpclient client = Okhttpclientfactory.createdefault (); if (buildconfig.debug) { //debug package inside, add stetho-okhttp support client.networkinterceptors (). Add (New Com.facebook.stetho.okhttp.StethoInterceptor ()); } Request Request = new Request.builder () . URL (URL) . Post (Getformencodingbuilder (namevaluepairs). Build ()) . Build (); try { Response Response = client.newcall (Request). Execute (); if (response.issuccessful ()) { return Response.body (). String (); } } catch (Exception e) { E.printstacktrace (); } return result; }
Only debug packages are required to support Stetho. The release package is not required.
But even the release package. This line of code will also build, so the most starting dependency is inside. Stetho-okhttp's dependence is compile, not debugcompile.
preview:
1. View Layouts
2. View SQLite and support running SQL statements. Note: The command line is not hit on the console form.
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /southeast "width=" 655 "height=" 593 ">
3. View the network request.
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /southeast ">
4. Create a dump. In fact this function I did not try, because there is no dump demand.
Almond Forest Research and development Liang Jianbin
Android Debugging Tools _ Stetho