热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

只在调试版本中包含Stetho。-IncludeStethoonlyinthedebugbuildvariant

IknowthatIcanusedebugCompiletoonlypullinadependencyforthedebugbuild.Isthereagood

I know that I can use debugCompile to only pull in a dependency for the debug build. Is there a good, streamlined way to do the code initialization that is required as well? Without the dependencies the other variants will fail to compile.

我知道我可以使用debugCompile来为调试构建引入一个依赖项。是否有一种良好的、流线化的方法来进行所需的代码初始化?没有依赖项,其他变体将无法编译。

3 个解决方案

#1


49  

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.

选项1:包含所有构建的Stetho(使用compile而不是debugCompile),并且只在应用程序类中初始化它以进行调试构建。

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

这很容易做到。在应用程序类中,检查BuildConfig。调试如下:

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.

选项2:只包含用于调试构建的Stetho,并为调试和发布构建创建不同的应用程序类。

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:

多亏了Gradle,应用程序可以为不同的构建版本拥有不同的源代码集。默认情况下,您有发布和调试构建类型,因此您可以有三个不同的源集:

  • 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.

您的应用程序代码很可能都在主源集中,您可以在应用程序的主文件夹旁边创建一个名为debug的新文件夹,并镜像主文件夹的结构,以便为调试构建添加所有内容。

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

在本例中,您希望主源集中有一个应用程序类,它根本不引用Stetho。

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

然后,您需要在调试源集中应用程序类,它将像通常那样初始化Stetho。

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.

您可以在Stetho示例中看到此设置的一个示例。具体地说,这是主要的应用程序类,这是调试应用程序类。还要注意,它们在每个源集中设置清单,选择使用哪个应用程序类。

#2


72  

Check the @Tanis answer.

检查@Tanis回答。

Also you can use something like this:

你也可以这样使用:

Add the library only on debug version.

只在调试版本中添加库。

dependencies {
   debugCompile 'com.facebook.stetho:stetho:1.1.1      
 }

In your Application you can do :

在你的申请中,你可以:

public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    StethoUtils.install(this);
  }
}

Then you can create different StethoUtils class in the debug/release version.

然后您可以在调试/发布版本中创建不同的StethoUtils类。

In src/debug/java/

在src /调试/ java /

public class StethoUtils{

   public static void install(Application application){
       Stetho.initialize(
          Stetho.newInitializerBuilder(application)
            .enableDumpapp(Stetho.defaultDumperPluginsProvider(application))
            .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(application))
            .build());

   }
}

In src/release/java/

在src /释放/ java /

public class StethoUtils{

   public static void install(Application application){
      // do nothing
   }
}

#3


1  

Using java reflection may be a perferct idea:

使用java反射可能是一个非常好的想法:

private void initStetho() {
            if (BuildConfig.DEBUG) {
                try {
                   Class stethoClazz = Class.forName("com.facebook.stetho.Stetho");
                    Method method = stethoClazz.getMethod("initializeWithDefaults",Context.class);
                    method.invoke(null, this);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }

then we can debug compile stetho:

然后我们可以调试编译stetho:

debugCompile 'com.facebook.stetho:stetho:1.5.0'

推荐阅读
author-avatar
爱你一辈子2502860605
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有