Disabling Stetho in Release Builds

July 4, 2017 Android

Stetho is an Android library that enables extended debugging options.

Initialization

1
2
3
4
5
6
public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    Stetho.initializeWithDefaults(this);
  }
}

What’s wrong with this piece of code?

It executes even in release builds

It might not, but chances are it does. You are going to place this code in app/src/main/java/your_package/MyApplication.java, right? If so, Stetho will run in every build type, because this path corresponds to the main source set. This could lead to unwanted device resource consumption in production.

A. Change the source set

Create a debug folder under app/src and move your MyApplication class there. Thus you make it be used in debug builds only. Stetho initialization code is not going to run in release builds any more.

B. Do runtime checks

Another approach is to check an automatically generated BuildConfig class property at run time:

4
5
6
if (BuildConfig.DEBUG) {
    Stetho.initializeWithDefaults(this);
}
comments powered by Disqus