(English) Debugging your Android SQLite database with Stetho

Není k dispozici v češtině.

Have you ever wondered why your Android app doesn‘t behave as expected? Why doesn’t it show data that you expect or have you ever even wondered if there are really any data at all? The situation is even more complicated if you use Content Providers for your database. Discussion about benefits and disadvantages of Content Providers is outside scope of this post but you will probably agree that it is more complicated to write queries using Content Providers than by raw SQL.
There are more ways around this.

Test raw queries

You can write test raw queries. But to really be able to go deep through data, you will need to add the query to code, build the project, test… and repeat if something is still broken. Of course, you can try to edit the query in the debugger but to avoid nightmares later, it might be better not to do it.

View content of SQLite database file

There are various programs that let you view the content of SQLite file. E.g. SQLite Studio or DB Browser for SQLite. In all of these programs, you can see your tables and in some of them even modify records or run SQL queries. However, the problem is how to get to the database file. If you save data to application private storage, you will not be able to get to production databases if your phone is not rooted. And even for development version, you will need to play with emulator and DDMS or add code to copy these files to public storage.

Stetho

Fortunately, there is the third option. One that lets you easily all of that. Stetho is a debugging bridge for Android applications. You can view all available debugging information using Chrome browser and opening chrome://inspect. There is much Chrome Developer Tools can do but I will speak only about debugging Android applications in this post. When inspect page is open, it will show a list of devices and applications that have Stetho dependency added (more about it later).

There are many tabs on the top. This post is about Resources tab that enables you to inspect SQLite databases and also inspect Shared Preferences. Another tab that might interest you is Network where you can watch network calls made by the app.

So let’s inspect resources tab.

Option Local Storage contains key values pairs of all application’s shared preferences files. Option Web SQL let’s you inspect application SQLite database(s). After opening database detail tables list is shown. You can inspect various tables content. What is more interesting is ability to write queries. Just click on database name and prompt wil be shown.

You can write all kinds of queries. It is nice but there are also some limitations. Stetho will show you max 250 records. And also if you have a lot of columns and select all of them, it is really shown as a mess. But in general, it is very nice and useful tool.

How to get it into app?

Add dependency through Gradle

compile 'com.facebook.stetho:stetho:1.5.0'

or Maven.

<dependency>
  <groupId>com.facebook.stetho</groupId>
  <artifactId>stetho</artifactId>
  <version>1.5.0</version>
</dependency>

Then initialize in your Application class. Simple application class might look like this.

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

Don’t forget to register Application class in AndroidManifest.xml. That’s it. If you want to debug network connections, add this dependency

compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'

or this one

compile 'com.facebook.stetho:stetho-urlconnection:1.5.0'

How can I only add Stetho to debug builds?

First, instead of compile use debugCompile

debugCompile 'com.facebook.stetho:stetho:1.5.0'

Now you Stetho library is available only in debug builds. But you try to initialize it in MyApplication class. How to solve it? Create two files with same name and static function with different content in folders of both debug and release build variants.

If you want it to be inside com.company.project package, inside your project’s src folder create folders debug and release both containing this folder structure: java\com\company\project. Here create StethoCongig.java file. This is debug variant.

public class StethoConfig {
    public static void initStetho(Context context) {
        Stetho.initializeWithDefaults(context);
    }
}

And this is release one.

public class StethoConfig {
    public static void initStetho(Context context) {
        // doing nothing on production
    }
}

Finally update MyApplication class:

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    StethoConfig.initStetho(this)
  }
}