Using Stetho to debug Android database

Facebook’s Stetho project enables you to use Chrome debugging tools to troubleshoot network traffic, database files, and view layouts. With this library, you need to have an active emulator or device running, and you use will Chrome to connect to the device by typing chrome://inspect.

For network traffic, you can use the Network Inspector:

Any SQLite database can also be inspected using the Resources -> Web SQL tab:

Note!

The third-party Android Async Http Client library uses the Apache HTTP Client, which is not currently supported by Stetho as noted in this issue. Troubleshooting networking issues works best with OkHttp or Retrofit. Regardless, you can still use this library for SQLite database inspection.

Setup

Setup your app/build.gradle file:

// Gradle dependency on Stetho
  dependencies {
    compile 'com.facebook.stetho:stetho:1.3.1'
  }

Next, initialize Stetho inside your Application object:

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

If you are also using Stetho with the OkHttp or Retrofit, make sure to include the OkHttp library as well:

dependencies {
    // add below Stetho main dependency
    compile 'com.facebook.stetho:stetho-okhttp3:1.3.1' // for OkHttp library
}

You will also need to add the StethoInterceptor when constructing the OkHttpClient instance:

OkHttpClient client = new OkHttpClient.Builder()
    .addNetworkInterceptor(new StethoInterceptor())
    .build();

Start your emulator or device. Then visit chrome://inspect on your Chrome desktop and your emulator device should appear. Click on Inspect to launch a new window.

inspector-discovery

Click on the Network tab. Now you can start watching network traffic between your emulator or device in real-time!

Inspecting SharedPrefs

To view your app’s SharedPreferences, open the Resources tab of the Developer Tools window and select LocalStorage. You will see the names of the files your app uses to store the preferences. Clicking a file displays the key-value pairs stored in that file.

You can even edit the values stored in a file.

Credits:

https://github.com/codepath/android_guides/wiki/Debugging-with-Stetho/8debb39f4fbeefe3c99daa512caa16331a0e8a34

That’s it for now!
Be sure to leave your comment below regarding any queries or suggestions.

 

 

the-dagger

Android Developer, Open Source Enthusiast

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.