Applying a plugin in afterEvaluate {...} or androidComponents {...} results in compilation error as it is too late to apply plugins in those blocks.
To check the active variant, we can query the tasks specific to our build and see if it contains its flavor name:
/**
* Applies the Google Services plugin only if the build flavor is "fdroid",
* determined by inspecting the Gradle task names.
*/
val tasks = gradle.startParameter.taskNames
if (tasks.any { it.contains("fdroid", ignoreCase = true).not() }) {
pluginManager.apply(libs.plugins.google.services.get().pluginId)
}