Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying CrashlyticsExtension in a convention plugin #6131

Closed
nuhkoca opened this issue Jul 28, 2024 · 7 comments
Closed

Applying CrashlyticsExtension in a convention plugin #6131

nuhkoca opened this issue Jul 28, 2024 · 7 comments

Comments

@nuhkoca
Copy link

nuhkoca commented Jul 28, 2024

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: Android Studio Ladybug | 2024.1.1
  • Firebase Component: Crashlytics Gradle Plugin
  • Component version: 3.0.2

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

We use build-logic composite build in our project to manage and share common build logic across the project. We almost have a convention plugin for everything. Recently, we decided to create another one for Firebase setup such as App Distribution, Crashlytics and Performance. Whereas, App Distribution and Performance were moved without any hassle, however, Crashlytics is throwing an exception when trying to configure it in a convention plugin as following:

An exception occurred applying plugin request [id: 'tr.application.firebase']

Failed to apply plugin 'tr.application.firebase'.
Extension of type 'CrashlyticsExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, SonarExtension, SonarExtension, ReportingExtension, DetektExtension, KtlintExtension, LibrariesForLibs, VersionCatalogsExtension, RootProjectAccessor, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, JavaToolchainService, JavaPluginExtension, BaseAppModuleExtension, ApplicationAndroidComponentsExtension, NamedDomainObjectContainer, KotlinAndroidProjectExtension, KotlinTestsRegistry, AnvilExtension, ComposeCompilerGradlePluginExtension, JacocoPluginExtension, AndroidJUnitPlatformExtension, GoogleServicesPlugin.GoogleServicesPluginConfig, AppDistributionExtension]

Relevant Code:

Version

plugin-firebase-crashlytics = { module = "com.google.firebase:firebase-crashlytics-gradle", version.ref = "firebaseCrashlytics" }

build-logic:convention/build.gradle.kts

plugins {
    `kotlin-dsl`
}

group = "de.traderepublic.app.buildlogic"


implementation(libs.plugin.firebase.crashlytics)

Convention Plugin

class AndroidApplicationFirebaseConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            with(pluginManager) {
                apply("com.google.firebase.appdistribution")
                apply("com.google.firebase.crashlytics")
                apply("com.google.firebase.firebase-perf")
            }

            val extension = extensions.getByType<ApplicationExtension>()
            configureFirebase(extension)
        }
    }
}

configureFirebase function

internal fun Project.configureFirebase(
    applicationExtension: ApplicationExtension,
) {
    applicationExtension.apply {
        defaultConfig {
            booleanField(CRASHLYTICS_ENABLED, true)
        }

        buildTypes {
            getByName(TRBuildType.debug.name) {
                configureCrashlytics()

                booleanField(CRASHLYTICS_ENABLED, false)
            }

            getByName(TRBuildType.release.name) {
                val betaProp = findProperty("isBeta") ?: false
                val isBeta = betaProp.toString().toBoolean()
                if (isBeta) {
                    configureAppDistribution()
                }
            }

            getByName(TRBuildType.internal.name) {
                configureAppDistribution()
            }
        }
    }
}

private fun Project.configureAppDistribution() {
    configure<AppDistributionExtension> {
        serviceCredentialsFile = "$rootDir/firebase_credentials.json"
        groups = System.getenv(FIREBASE_TESTER_GROUPS)
        releaseNotes = System.getenv(FIREBASE_RELEASE_NOTES)
    }
}

private fun Project.configureCrashlytics() {
    configure<CrashlyticsExtension> {
        mappingFileUploadEnabled = false
    }
}
@lehcar09
Copy link
Contributor

Hi @nuhkoca, thank you for reaching out. I tried reproducing the issue, however, I'm not familiar with composite build so I was having difficulty. By any chance, could you share an MCVE to help us investigate the issue?

Could you check, if it's possible, if the import for CrashlyticsExtension module is added in the app/build.gradle.kts file of the built app?

import com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsExtension

@nuhkoca
Copy link
Author

nuhkoca commented Jul 30, 2024

Hi @lehcar09 thanks for getting back to me. Will prepare a reproducible for you and let you know here

@lehcar09
Copy link
Contributor

lehcar09 commented Aug 1, 2024

Hey @nuhkoca, we'll keep the needs-info tag for now. Don't worry if the issue closes due to stale, we can always reopen this once we have new information. Thanks!

@nuhkoca
Copy link
Author

nuhkoca commented Aug 1, 2024

@nuhkoca
Copy link
Author

nuhkoca commented Aug 6, 2024

@lehcar09 you looked into this by any chance?

@lehcar09
Copy link
Contributor

lehcar09 commented Aug 7, 2024

Thank you for the repro you shared @nuhkoca. I was able to reproduce the issue you raised.

After some debugging, it looks like the issue occurs because of incorrect context. The Crashlytics configuration applies to the specific build type. After changing to ApplicationBuildType, I was able to run the app (I've added an activity and called setCrashlyticsCollectionEnabled(true) )

import com.android.build.api.dsl.ApplicationBuildType
import com.android.build.api.dsl.ApplicationExtension
import com.google.firebase.appdistribution.gradle.AppDistributionExtension
import com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

internal fun Project.configureFirebase(extension: ApplicationExtension) {
    extension.buildTypes {
        getByName("debug") {
            configureAppDistribution()
            configureCrashlytics()
        }
    }
}

private fun ApplicationBuildType.configureCrashlytics() {
    configure<CrashlyticsExtension> {
        mappingFileUploadEnabled = false
    }
}

private fun ApplicationBuildType.configureAppDistribution() {
    configure<AppDistributionExtension> {
        releaseNotes = "My build-tools release notes"
    }
}

Could you check if this works for you?

@nuhkoca
Copy link
Author

nuhkoca commented Aug 7, 2024

@lehcar09 ohh cool that worked, thanks for helping out! Still would like it to work under Project context as like AppDistribution but anyway it works for me tho.

@nuhkoca nuhkoca closed this as completed Aug 7, 2024
@firebase firebase locked and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants