lannyf
lannyf

Reputation: 11035

android library module how to bring in stetho only in debug build

Having android library module, in debug build it would like to use the stetho, bot no in release build. I guess it could add dependency of

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

or using 
debugImplementation 'com.facebook.stetho:stetho:1.4.1'. // with latest version
debugImplementation 'com.uphyca:stetho_realm:2.0.0'

The question is in where the code need to take the StethoInterceptor

OkHttpClient.Builder()
    .addNetworkInterceptor(StethoInterceptor())
    .build()

how does it compile in release build that there is no stetho dependency?

Upvotes: 1

Views: 219

Answers (1)

Shalu T D
Shalu T D

Reputation: 4039

Please call the below checkAndInitStetho() method in your application class:

public YourApplication extends Application {

 @Override
    public void onCreate() {
        super.onCreate();
        checkAndInitStetho();
    }


private void checkAndInitStetho() {
        //Stetho integration
        if (BuildConfig.DEBUG) { 
            try {
                SyncTask.executeAndWait(() -> Stetho.initialize(
                        Stetho.newInitializerBuilder(YourApplication.this)
                                .enableDumpapp(Stetho.defaultDumperPluginsProvider(YourApplication.this))
                                .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(YourApplication.this))
                                .build()));
            } catch (Exception e) {
                LOGE(LOG_TAG, "Error on checkAndInitStetho()", e);
            }
        }
    }
}

Also, when you create the OkHttpClient.Builder, you should check whether its debug mode as below:-

if (BuildConfig.DEBUG) {
                builder.addNetworkInterceptor(new 
                 StethoInterceptor());
 }

Please go through the link for the application class

Upvotes: 0

Related Questions