Browse Source

Handle ClassCastException for ComposeMultiplatformBuildService in Compose Gradle plugin (#3467)

* Handle ClassCastException for ComposeMultiplatformBuildService in Compose Gradle plugin

ClassCastException might occur when our gradle plugin was loaded more than once.  See https://github.com/JetBrains/compose-multiplatform/issues/3459

We throw another exception with a bit more clear message.

* Update gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt
pull/3506/head
Oleksandr Karpovich 1 year ago committed by GitHub
parent
commit
e888495386
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt

17
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt

@ -13,6 +13,7 @@ import org.jetbrains.compose.internal.mppExtOrNull
import org.jetbrains.compose.internal.webExt import org.jetbrains.compose.internal.webExt
import org.jetbrains.kotlin.gradle.plugin.* import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import java.lang.ClassCastException
import javax.inject.Inject import javax.inject.Inject
@Suppress("UnstableApiUsage") @Suppress("UnstableApiUsage")
@ -50,7 +51,21 @@ class ComposeCompilerKotlinSupportPlugin @Inject constructor(
val service = ComposeMultiplatformBuildService.provider(target) val service = ComposeMultiplatformBuildService.provider(target)
buildEventsListenerRegistry.onTaskCompletion(service) buildEventsListenerRegistry.onTaskCompletion(service)
service.get().parameters.unsupportedCompilerPlugins.add( val providedService = try {
service.get()
} catch (e: ClassCastException) {
// Compose Gradle plugin was probably loaded more than once
// See https://github.com/JetBrains/compose-multiplatform/issues/3459
throw IllegalStateException(
"Failed to get ComposeMultiplatformBuildService instance." +
" Compose Gradle plugin was probably loaded more than once." +
" Consider declaring it in the root build.gradle.kts",
e
)
}
providedService.parameters.unsupportedCompilerPlugins.add(
target.provider { target.provider {
composeCompilerArtifactProvider.compilerArtifact.takeIf { composeCompilerArtifactProvider.compilerArtifact.takeIf {
target.hasNonJvmTargets() && it.isNonJBComposeCompiler() target.hasNonJvmTargets() && it.isNonJBComposeCompiler()

Loading…
Cancel
Save