1

拥有 android 库模块,在调试版本中它想使用 stetho,bot no 在发布版本中。我想它可以增加对

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'

问题在于代码需要在哪里使用 StethoInterceptor

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

它如何在没有 stetho 依赖的发布版本中编译?

4

1 回答 1

0

请在您的班级中调用以下checkAndInitStetho()方法:application

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);
            }
        }
    }
}

此外,当您创建 时OkHttpClient.Builder,您应该检查其debug模式是否如下:-

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

请通过课程链接application

于 2020-06-29T15:45:31.043 回答