Debugging with Stetho in Android - GeeksforGeeks

06 Jan, 2021
Stetho is an open-source debug library developed by Facebook. It allows you to use chrome debugging tools to troubleshoot network traffic., thus it provides a rich, interactive debugging experience for android developers. Stetho easily and smoothly debug network calls. It is a sophisticated debug bridge for Android applications. When enabled, developers have a path to the Chrome Developer Tools feature natively part of the Chrome desktop browser. Developers can also prefer to allow the optional dumpapp tool which allows a powerful command-line interface to application internals. Without limiting its functionality to just network inspection, JavaScript console, database inspection, etc. 
Stetho is an open-source debugging platform.It provides a rich and highly interactive experience.With the help of Stetho native applications debugging very simple.It offers you to use Google Chrome debugging tool for various activities.It provides hierarchy inspection during debugging.Also, Stetho allows network, database management, and more interacting features.Stetho uses HTTP web-socket to send data.
Stetho is an open-source debugging platform.
It provides a rich and highly interactive experience.
With the help of Stetho native applications debugging very simple.
It offers you to use Google Chrome debugging tool for various activities.
It provides hierarchy inspection during debugging.
Also, Stetho allows network, database management, and more interacting features.
Stetho uses HTTP web-socket to send data.
The problem with debugging network traffic while developing android applications, debugger facing problems with traditional debugging tools get messed up and inspection got very complex while switching the devices.
The debugging is more reliable and easy with the Stetho library because it uses the chrome debugging tool which is supports web-socket and using it for network debugging. Stetho automated the call inspection, so it becomes more important for android developers. 
Stetho uses an HTTP web Socket server that sends all debugging information to the browser. It is accessible through:
chrome://inspect
Step 1: Add the following dependency in the build.gradle file.
implementation ‘com.facebook.stetho:stetho-okhttp3:1.5.1’
Step 2: Register your class in AndroidManifest.xml, and initialize it in the application.
Java
import android.app.Application;import android.content.Context;import com.facebook.stetho.InspectorModulesProvider;import com.facebook.stetho.Stetho;import com.facebook.stetho.inspector.protocol.ChromeDevtoolsDomain;import com.facebook.stetho.okhttp3.StethoInterceptor;import com.facebook.stetho.rhino.JsRuntimeReplFactoryBuilder;import com.jakewharton.caso.OkHttp3Downloader;import com.squareup.caso.Caso;import okhttp3.OkHttpClient;  public class Stetho extends Application {    public OkHttpClient httpClient;    @Override    public void onCreate() {        super.onCreate();        final Context context = this;        if (BuildConfig.DEBUG) {                          // Create an InitializerBuilder             Stetho.InitializerBuilder initializerBuilder = Stetho.newInitializerBuilder(this);                          // Enable Chrome DevTools              initializerBuilder.enableWebKitInspector(new InspectorModulesProvider() {                @Override                public Iterable<ChromeDevtoolsDomain> get() {                                        // Enable command line interface                    return new Stetho.DefaultInspectorModulesBuilder(context).runtimeRepl(                            new JsRuntimeReplFactoryBuilder(context)                                    .addVariable("foo", "bar")                                    .build()                    ).finish();                }            });                              // Use the  InitializerBuilder to generate  an Initializer            Stetho.Initializer initializer = initializerBuilder.build();                              // Initialize Stetho with the Initializer            Stetho.initialize(initializer);                              // Add Stetho interceptor            httpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();        } else {            httpClient = new OkHttpClient();        }        Caso caso = new Caso.Builder(this).downloader(new OkHttp3Downloader(httpClient)).build();        Caso.setSingletonInstance(caso);    }}
Or
Initialize your library with a single line of code (for example ):
Java
public class Applicationstetho extends Application {  public void onCreate() {    super.onCreate();    if(BuildConfig.DEBUG ){      Stetho.initializeWithDefault(this)      }  }}
Updating your manifest file in your android project:
XML
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://geeksforgeeks.com/apk/res/android"    package="com.geeksforgeeks.sthetosample">      <uses-permission android:name="android.permission.INTERNET"/>      <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme"        android:name=".StethoSample">        ...    </application>  </manifest>
Step 3: Enable network inspection.
Following method is the easiest and simpler w2ay to enable network inspection when you constructing the okHttpClient instance:
okHttpClient.Builder()
           .addNetworkInterceptor(StethoInterceptor())
           .build()
How to check?
Start emulator on the device. Then open chrome://inspect on Chrome and your emulator device should appear, after that click inspects to launch a new window and click the network tab to watch network traffics.
android
Picked
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Retrofit with Kotlin Coroutine in Android
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples