Stetho

An Android debug bridge

Bedanta Bikash Borah
3 min readNov 15, 2016

What is stetho?

Stetho is an open source android debug bridge written by facebook. If you are looking for an easy to integrate tool to perform debug operations on your android app, you should definitely try this library. It comes on the top of chrome developer tool. You can enable db and SharedPreferences inspection by adding just two lines of code.You can also issue sql command from chrome. Cool isn’t it?

Features of Stetho:

  1. Look inside of a database.
  2. Look inside of SharedPreferences file.
  3. Do network inspection.
  4. Write your custom dumpapp plugin.
  5. Javascript Console.
  6. View Hierarchy (API 15 and above).

Integration Process:

To enable stetho you just need to do two things.

  1. Download the library: You can download the library using from Maven Central via Gradle or Maven.

//via gradle dependency
dependencies {
compile ‘com.facebook.stetho:stetho:1.4.1’
}
//via Maven<dependency>
<groupid>com.facebook.stetho</groupid>
<artifactid>stetho</artifactid>
<version>1.4.1</version>
</dependency>

2. Initialization on your Application class.

public class CustomApplication extends Application {
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this);
}
}
}

And thats it. You have enabled Stetho on your application. Now you can inspect your database and SharedPreferences with the help of chrome developer tool. Fire up your chrome browser and hit the URL

chrome://inspect

It will show you the device connected. In my case its showing my Nexus 5 device, but you can debug your application with emulator too.

Let’s look inside our database and SharedPreferences

Now you can inspect your database and edit SharedPreferences values from chrome developer tool. With the help of some 3rd party plugins like this one, you will be able to see your Realm files too. Another cool thing you can do inside chrome developer tool is issue sql query. Just select the db file from the left and type your query. It’s that simple.

SQL Query
Shared Preference

Do Network inspection

If you are using the OkHttp library from Squareup, you can use Interceptors to inspect your network connections.

//For OkHttp 2.x
//add compile dependency to your app level build.gradle file
dependencies {
compile 'com.facebook.stetho:stetho-okhttp:1.4.1'
}
//hook StethoInterceptor to the OkHttpClientOkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(new StethoInterceptor());//For OkHttp 3.x
//add compile dependency to your app level build.gradle file
dependencies {
compile 'com.facebook.stetho:stetho-okhttp3:1.4.1'
}
//hook StethoInterceptor to the OkHttpClientnew OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();

If you are using HttpUrlConnection, you can use StethoUrlConnectionManager. Here is a full implementation.

dependencies { 
compile ‘com.facebook.stetho:stetho-urlconnection:1.4.1’
}
Network Inspection

Custom dumpapp plugin:

Dumpapp is a powerful feature. Using dumpapp you can manipulate your Android app from the command line . To use dumpapp you should have the latest version of Python installed on your system. A default set of plugins is provided, but the real power of dumpapp is the ability to easily create your own.

Javascript Console:

Using This plugin you can execute javascript code that can interact with the application or even the Android SDK.

Showing a Toast Using Browser Console.

View Hierarchy:

For API level 15 and above you can see the view hierarchy on your chrome developer tool inside inspect tab.

View Hierarchy

Summary:

By now, you must have realized that Stetho can save you a lot of time during the debug process of your application. To learn more about Stetho, you can refer the sample project available on this GitHub repository.

--

--