How to configure Firebase Performance Monitoring plugin extension in Gradle Kotlin DSL
Asked Answered
A

2

15

I have an Android app using Gradle with Kotlin DSL. I'm adding Firebase Performance Monitoring, but I would like for it to be enabled only for a specific build type.

I've been following the instructions provided at Firebase - Disable Firebase Performance Monitoring. Unfortunately the provided snippets are in Groovy.

I've tried to get a reference to the Firebase Performance Monitoring extension in my app level Gradle script by doing the following:

    plugins {
        ...
        id("com.google.firebase.firebase-perf")
        kotlin("android")
        kotlin("android.extensions")
        kotlin("kapt")
    }

    buildTypes {
        getByName(BuildTypes.DEBUG) {
            configure<com.google.firebase.perf.plugin.FirebasePerfExtension> {
                setInstrumentationEnabled(false)
            }
        }
        ...
    }

    ...

    dependencies {
        val firebaseVersion = "17.2.1"
        implementation("com.google.firebase:firebase-core:$firebaseVersion")
        implementation("com.google.firebase:firebase-analytics:$firebaseVersion")
        implementation("com.google.firebase:firebase-perf:19.0.5")
    }

Android Studio doesn't see any problem in this and auto-completes FirebasePerfExtension. Unfortunately upon running a Gradle sync I get the following:

Extension of type 'FirebasePerfExtension' does not exist. 
Currently registered extension types: [ExtraPropertiesExtension, DefaultArtifactPublicationSet, ReportingExtension, SourceSetContainer, JavaPluginExtension, NamedDomainObjectContainer<BaseVariantOutput>, BaseAppModuleExtension, CrashlyticsExtension, KotlinAndroidProjectExtension, KotlinTestsRegistry, AndroidExtensionsExtension, KaptExtension]

There's no plugin extension related to Firebase Performance Monitoring.

This is in my project level build.gradle file dependencies block:

classpath("com.google.firebase:perf-plugin:1.3.1")

Any help is appreciated!

Update 1

As recommended on the Gradle - Migrating build logic from Groovy to Kotlin guide at "Knowing what plugin-provided extensions are available" I've ran the kotlinDslAccessorsReport task. None of the resulting extensions seems to be related to Firebase.

Ardeth answered 23/3, 2020 at 4:36 Comment(0)
B
19

Had the same issue and was going to apply from groovy file, but seems i found the solution in here: https://docs.gradle.org/5.0/userguide/kotlin_dsl.html#sec:interoperability

withGroovyBuilder {
   "FirebasePerformance" {
       invokeMethod("setInstrumentationEnabled", false)
    }
}
Bionomics answered 15/4, 2020 at 11:0 Comment(1)
Fantastic! You are a legend! This seems to work nicely. :)Ardeth
S
9

We used this answer, util we discovered a better working way in the team

check(this is ExtensionAware)
configure<com.google.firebase.perf.plugin.FirebasePerfExtension> { setInstrumentationEnabled(false) }
Stronski answered 29/4, 2021 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.