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示例中看到此设置的一个示例。具体地说,这是主要的应用程序类,这是调试应用程序类。还要注意,它们在每个源集中设置清单,选择使用哪个应用程序类。