在Android Studio中使用Firebase Performance时,gradle任务app:transformClassesWithFirebasePerformancePluginForDebug
所花费的时间比任何其他任务都要长,因此大大减慢了我的gradle构建时间。
我们项目中的Firebase导致40%的构建时间增加。为了加快调试构建,我们添加了使用app / build.gradle和root build.gradle文件中的构建参数来打开/关闭它的可能性:
应用:
if(!project.hasProperty(“disable-performance-plugin”)){apply plugin:'com.google.firebase.firebase-perf'}
根/ buildscript /依赖关系:
if(!project.hasProperty(“disable-performance-plugin”)){classpath('com.google.firebase:firebase-plugins:1.1.5'){exclude group:'com.google.guava',module:' guava-jdk5'}}
从命令行运行时使用
./gradlew your-task -Papable-performance-plugin
在Android Studio中工作时,将标志添加到编译器选项:
所有现有的答案都是有效的,但他们都错过了一些东西。
要解决此问题,您有两个主要选项可供选择。
firebasePerformanceInstrumentationEnabled
property这是the official way provided by the SDK本身在构建过程中禁用它。
这是做什么的:
transformClassesWithFirebasePerformancePluginFor*
任务执行时间减少到约5-10秒。<meta-data>
标记控制后者并调用FirebasePerformance.getInstance().setPerformanceCollectionEnabled()
。更多信息在the docs。这该怎么做:
我认为只在我们需要它的情况下启用插件时更容易(通常只有在我们发布应用程序时才会这样)而不是在所有其他情况下禁用插件。
注意:当然,使用手动构建可能会忘记启用它。因此,如果您没有CI,可能值得在Gradle中添加一些其他自动脚本,或者坚持使用其他答案中使用的相反方法。
但总的来说,我们只需要两个步骤:
gradle.properties
文件中:
firebasePerformanceInstrumentationEnabled=false
优点:
缺点:
firebase-perf
Gradle plugin这是做什么的:
<meta-data>
标记控制后者并调用FirebasePerformance.getInstance().setPerformanceCollectionEnabled()
。更多信息在the docs。这该怎么做:
这种方法有类似的要点和警告,还包括两个步骤:
build.gradle
文件:
if (project.hasProperty('useFirebasePerf')) {
apply plugin: 'com.google.firebase.firebase-perf'
}
注意:您不需要对项目级别的build.gradle
应用相同的检查:
classpath "com.google.firebase:firebase-plugins:$firebase_plugins_version"
当插件本身未启用时,Gradle将不会以任何方式使用此声明。
如果你使用firebase-plugins v1.1.1或更高版本作为guava-jdk5
,你不需要在那里排除stated in the docs依赖。优点:
缺点:
firebase-perf
SDK如果您不使用Firebase Performance SDK中的自定义跟踪或任何其他功能,并且仅依赖于自动监视(即,您的代码中没有任何依赖于SDK),那么您可以将非依赖性排除在非生产中建立。
这该怎么做:
您需要做的就是更新您的app模块qazxsw poi文件:
build.gradle
if (project.property('firebasePerformanceInstrumentationEnabled') == 'true') {
implementation "com.google.firebase:firebase-perf:${firebase_perf_version}"
}
优点:
缺点:
您还可以查看if (project.hasProperty('useFirebasePerf')) {
implementation "com.google.firebase:firebase-perf:${firebase_perf_version}"
}
,我在这个主题上进一步扩展。
此主题中的所有注释均有效。我想建议一种非常简单的方法来禁用调试版本:
my article
只是给另一个选项来禁用if (getGradle().getStartParameter().getTaskRequests().toString().contains("Release")) {
apply plugin: 'com.google.firebase.firebase-perf'
}
,这是我的食谱:
在main build.gradle文件夹中:
transformClassesWithFirebasePerformancePluginForDebug
在build.gradle应用程序文件中:
if (!project.gradle.startParameter.taskNames.any { taskName ->
taskName.toLowerCase().contains('assemble') && taskName.toLowerCase().contains('debug') }) {
classpath("com.google.firebase:firebase-plugins:$firebasePluginVersion") {
exclude group: 'com.google.guava', module: 'guava-jdk5'
}
}
我也遇到了这个问题。最初我们一直在使用R.Zagórski提供的答案的变体,但基于Gradle论坛的if (!project.gradle.startParameter.taskNames.any { taskName ->
taskName.toLowerCase().contains('assemble') && taskName.toLowerCase().contains('debug') }) {
apply plugin: 'com.google.firebase.firebase-perf'
}
,似乎有条件地将插件应用于项目并不是正确的方法:
插件不能仅应用于“项目的一部分”。它们是否适用。
有条件地应用插件似乎可以正常工作,但它不是官方支持的功能。在同一个线程中,另一个点是:
但是插件应该允许您以更细粒度的级别配置它。
果然,Firebase插件实际上有一个属性可以让你打开或关闭仪器(因此切换增加的构建时间)。但是,使用这个属性是很棘手的,因为你必须在构建过程中的恰当时间应用它,但是一旦你有了它,那么你基本上可以根据你的需要进行调整。
以下代码片段是我们如何基于Debug与非Debug构建变体来轮换检测。它是用Kotlin编写的,但我想它也会转换为Groovy:
similar thread
请注意,有了这个,plugins {
...
id ("com.google.firebase.firebase-perf")
}
...
android {
...
applicationVariants.all {
val variant = this
val isFirebaseEnabled = !variant.javaCompiler.name.contains("Debug", true)
gradle.taskGraph.whenReady {
if (this.hasTask(variant.javaCompiler))
{
project.FirebasePerformance.isInstrumentationEnabled = isFirebaseEnabled
}
}
}
...
}
任务仍将始终针对每个构建变体运行,但对于未启用检测的变体,它几乎会立即完成。
我简化了这个答案transformClassesWithFirebasePerformancePluginFor*
的选项2
Firebase Performance发布了新版本的gradle.properties
(v1.3.0)。这样可以为特定的构建变体(包括buildTypes或productFlavors)禁用Firebase性能监视Gradle插件。
以下示例:
perf-plugin
参考发行说明:
android {
// ...
debug {
FirebasePerformance {
// Set this flag to 'false' to disable @AddTrace annotation processing and
// automatic HTTP/S network request monitoring
// for a specific build variant at compile time.
instrumentationEnabled false
}
}
}