【问题标题】:android library module how to bring in stetho only in debug buildandroid库模块如何仅在调试版本中引入stetho
【发布时间】:2020-06-29 15:22:26
【问题描述】:

拥有 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 依赖的发布版本中编译?

【问题讨论】:

  • 这能回答你的问题吗? Include Stetho only in the debug build variant
  • 如果 stetho 没有为发布构建提供特殊的工件,那么您不应该为每个构建类型使用该依赖项。相反,您可以尝试有条件地添加:if(BuildType.Debug) { add stetho }
  • 如果没有依赖则不编译 .addNetworkInterceptor(StethoInterceptor())

标签: android android-library stetho


【解决方案1】:

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

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

请通过link 获取application

【讨论】:

  • 库没有application,它可能需要的唯一代码是在某个地方做`.addNetworkInterceptor(StethoInterceptor())`
  • 不是,你的安卓应用有Application类rt?否则,您可以在第一个活动的具体()执行时执行。应用类中的最佳表现
  • 图书馆不知道谁是托管应用程序。如果不依赖于 stetho,则 Stetho. 不会被解析。
猜你喜欢
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 2021-05-11
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多