Include Stetho only in the debug build variant

theblang picture theblang · May 11, 2015 · Viewed 13.6k times · Source

I know that I can use debugCompile to only pull in a dependency for the debug build. Is there a good, streamlined way to do the code initialization that is required as well? Without the dependencies the other variants will fail to compile.

Answer

Gabriele Mariotti picture Gabriele Mariotti · Jul 17, 2015

Check the @Tanis answer.

Also you can use something like this:

Add the library only on debug version.

dependencies {
   debugCompile 'com.facebook.stetho:stetho:1.1.1      
 }

In your Application you can do :

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    StethoUtils.install(this);
  }
}

Then you can create different StethoUtils class in the debug/release version.

In src/debug/java/

public class StethoUtils{

   public static void install(Application application){
       Stetho.initialize(
          Stetho.newInitializerBuilder(application)
            .enableDumpapp(Stetho.defaultDumperPluginsProvider(application))
            .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(application))
            .build());

   }
}

In src/release/java/

public class StethoUtils{

   public static void install(Application application){
      // do nothing
   }
}