1

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?

lannyf
  • 9,865
  • 12
  • 70
  • 152
  • 1
    Does this answer your question? [Include Stetho only in the debug build variant](https://stackoverflow.com/questions/30172308/include-stetho-only-in-the-debug-build-variant) – EraftYps Jun 29 '20 at 15:43
  • If stetho doesn't provide special artifact for release build then you shouldn't be using that dependency per build type. Instead you can try conditionally adding like: `if(BuildType.Debug) { add stetho }` – Jeel Vankhede Jun 29 '20 at 15:43
  • it does not compile if no dependency `.addNetworkInterceptor(StethoInterceptor())` – lannyf Jun 29 '20 at 15:45

1 Answers1

0

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

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
  • the library does not have `application`, the only code it may need is somewhere to do `.addNetworkInterceptor(StethoInterceptor())`` – lannyf Jun 29 '20 at 15:48
  • Not that, your android app has Application class rt? otherwise, you can do while the concrete() of your first activity executes. Best do in Application class – Shalu T D Jun 29 '20 at 15:50
  • the library does not no know who is the hosting app. if no dependency to stetho, the `Stetho.` is not resolved. – lannyf Jun 29 '20 at 16:07