仅在调试版本中包含Stetho [英] Include Stetho only in the debug build variant
问题描述
我知道我可以使用debugCompile
仅将dependency
拉入debug build
.是否有一种很好的,精简的方法来执行所需的code initialization
?没有依赖关系,其他变体将无法编译.
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.
推荐答案
您有一些选择.
选项1:包括所有版本的Stetho(使用compile
而不是debugCompile
),并且仅在Application
类中将其初始化以进行调试版本.
Option 1: Include Stetho for all builds (using compile
instead of debugCompile
) and only initialize it in your Application
class for debug builds.
这很容易做到.在您的Application
类中,像这样检查BuildConfig.DEBUG
:
This is pretty easy to do. In your Application
class, check BuildConfig.DEBUG
like so:
if (BuildConfig.DEBUG) {
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build()
);
}
选项2::仅将Stetho用于调试版本,并为调试和发行版本创建不同的Application
类.
Option 2: Only include Stetho for debug builds, and create different Application
classes for debug and release builds.
感谢Gradle,应用程序可以具有用于不同构建变体的不同源集.默认情况下,您具有发布和调试构建类型,因此可以拥有三种不同的源集:
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:
- 调试以获得仅在调试版本中需要的代码
- release 以获得仅在发行版中需要的代码
- main 用于所有版本中所需的代码
- 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
您的应用程序代码可能当前在main
源集中.您只需在应用程序中的main
文件夹旁边创建一个名为debug
的新文件夹,并为要添加到调试版本的所有内容镜像main
文件夹的结构.
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.
在这种情况下,您需要在main
源集中的Application
类完全不引用Stetho.
In this case, you want an Application
class in your main
source set that doesn't reference Stetho at all.
然后,您需要在debug
源集中的Application
类像通常那样初始化Stetho.
Then you want an Application
class in your debug
source set that initializes Stetho like you normally would.
您可以在 Stetho示例中看到此设置的示例.具体来说,这是主要应用程序类和这是调试应用程序类.还要注意,它们在每个源集中设置了清单,用于选择要使用的Application类.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!