Include Stetho Only in the Debug Build Variant

Include Stetho only in the debug build variant

You have a few options.

Option 1: Include Stetho for all builds (using compile instead of debugCompile) and only initialize it in your Application class for debug builds.

This is pretty easy to do. In your Application class, check BuildConfig.DEBUG like so:

if (BuildConfig.DEBUG) {
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build()
);
}

Option 2: Only include Stetho for debug builds, and create different Application classes for debug and release builds.

Thanks to Gradle, applications can have different source sets for different build variants. By default, you have release and debug build types, so you can have three different source sets:

  • debug for code you only want in debug builds
  • release for code you only want in release builds
  • main for code you want in all builds

Your application code is likely all currently in the main source set. You can simply create a new folder called debug next to the main folder in your application and mirror the structure of your main folder for everything you want to add for debug builds.

In this case, you want an Application class in your main source set that doesn't reference Stetho at all.

Then you want an Application class in your debug source set that initializes Stetho like you normally would.

You can see an example of this setup in the Stetho sample. Specifically, here's the main Application class, and here's the debug Application class. Also note that they set up manifests in each source set that selects which Application class to use.

Import library only for debugging

Just leave the unused imports as they are. Your approach of if (BuildConfig.DEBUG) is perfectly valid. And frankly the best way to implement it.

Unused imports have no impact on performance: reference. There might be a trivial increase in compile time, but no increase in runtime.

Import statements don't make it to byte code.

You will need to change
Gradle:

debugCompile 'com.facebook.stetho:stetho:1.4.1'
debugCompile 'com.uphyca:stetho_realm:2.0.0'

to

compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.uphyca:stetho_realm:2.0.0'

importing a library only for debugging, not for release

In your gradle replace

compile 'library'

for

compileDebug 'library'

In Gradle 3.0 not sure but think it is implementationDebug/apiDebug

But on release the code will not compile.

A solution that may work if it doesnt conflict is to create a interface in 'main/java' that will delegate what you want without being library specific, then create a folder 'debug/java' and create a File that implements the interface with the Library, call it from your code instead this debug will be specific for the flavor debug compile. Here to compile as release without errors you may specify a class with the same name in 'release/java' with a empty implementation (if this doesnt works you will have to lookup with Class#forName and it will only work on debug)

Android - dependencies and code separation (debug/release builds)

Put the code you only want to compile in debug mode under "src/debug/java", as described here. Gradle allows you to use multiple source sets per build type and flavor, and will only compile those that are relevant for the build configuration.



Related Topics



Leave a reply



Submit