Classloader mismatch when restoring a build service reference
Current Behavior
Using a build service that is also an OperationCompletionListener as a parameter to the FlowAction sometimes fails with an exception in a CC miss build:
GradleException: Could not load the value of field `__buildAnalyticsService__` of `BuildEndAction$Parameters_Decorated` bean found in class `BuildEndAction`.
Caused by: java.lang.IllegalArgumentException: Cannot set the value of a property of type GradleBuildAnalyticsListener loaded with VisitableURLClassLoader(ClassLoaderScopeIdentifier.Id{coreAndPlugins:settings[:]:groovy-dsl:/Users/b/Development/apps/android/settings.gradle:SettingsScript(local)}) using a provider of type GradleBuildAnalyticsListener loaded with ScriptClassLoader(groovy-script-/Users/b/Development/apps/android/settings.gradle-loader).
Expected Behavior
BuildService should be properly injected into the action.
Context (optional)
No response
Self-contained Reproducer Project
N/A
Gradle version
8.12.1
Build scan URL (optional)
No response
Your Environment (optional)
No response
We're having this a bunch recently. One example here: https://github.com/apollographql/apollo-kotlin/actions/runs/15589064153/job/43903051251
Setting kotlin.internal.collectFUSMetrics=false workarounds the issue but it's not 100% clear if it's a KGP thing or a Gradle thing.
We also facing this in our internal source code. Whats even worse, it seems the whole build gets invalidated afterwards. No build cache is picked up. Super frustrating developer experience...