Skip to content

Commit 0399bce

Browse files
[FGP] Report the real error when applying kotlin-android fails (flutter#195080)
When `android.builtInKotlin=false` and `android.newDsl=true`, the Flutter Gradle Plugin said "KGP was not found on the classpath". The real error from the Kotlin Gradle Plugin was hidden. The build then failed with an unrelated script compilation error. This change catches only the missing-plugin case, so other failures show the Kotlin Gradle Plugin's own message. Related to flutter#181557 and flutter#189236. <details><summary>Agent authored details</summary> `FlutterPluginUtils.detectApplyingKotlinGradlePlugin` wrapped `pluginManager.apply("kotlin-android")` in `catch (_: Exception)`. It now catches `org.gradle.api.plugins.UnknownPluginException`, which Gradle's `DefaultPluginManager.apply(String)` throws for an unknown plugin id. Failures inside the plugin's own `apply` are wrapped differently and now propagate. Before (AGP 9.3.1, KGP 2.4.20, template app, `builtInKotlin=false`, `newDsl=true`): ``` Applying the Kotlin Android Plugin (KGP) was unsuccessful. KGP was not found on the classpath. ... * What went wrong: Script compilation errors: Line 41: kotlin { ``` After: ``` > Failed to apply plugin 'kotlin-android'. > ⛔ Failed to apply plugin 'org.jetbrains.kotlin.android' The 'org.jetbrains.kotlin.android' plugin is not compatible with AGP's 9.0 new DSL (`android.newDsl=true` is enabled by default). Solution: Set `android.builtInKotlin=true` in `gradle.properties` and migrate to built-in Kotlin (see https://kotl.in/gradle/agp-built-in-kotlin for guidance), or set `android.newDsl=false` in `gradle.properties` to temporarily bypass this issue. ``` Tests: the two existing "KGP fails to apply" tests now throw `UnknownPluginException`. Added `propagates a KGP failure that is not a missing plugin`. No comments, docs or tests removed. Verification: `./gradlew test` (JDK 17) passed. ktlint 1.5.0 with the CI flags passed. A manual build of the template app was run before and after. Note: with AGP 9, KGP resolves through AGP even when `settings.gradle.kts` does not declare it, so the missing-plugin branch is hard to reach in practice. This applies cleanly to `flutter-3.50-candidate.0`; `FlutterPluginUtils.kt` is identical there. </details> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 12e6b28 commit 0399bce

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎packages/flutter_tools/gradle/src/main/kotlin/FlutterPluginUtils.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.gradle.api.GradleException
2323
import org.gradle.api.Project
2424
import org.gradle.api.UnknownTaskException
2525
import org.gradle.api.logging.Logger
26+
import org.gradle.api.plugins.UnknownPluginException
2627
import org.gradle.kotlin.dsl.register
2728
import org.gradle.kotlin.dsl.support.serviceOf
2829
import org.gradle.process.ExecOperations
@@ -583,9 +584,12 @@ object FlutterPluginUtils {
583584
if (!pluginState.hasAppPlugin && !pluginState.hasLibPlugin) return@subprojects
584585

585586
if (!isBuiltInKotlinEnabled(project, agpVersion) && !pluginState.hasKgpPlugin) {
587+
// Only a missing plugin is handled here. Any other failure, such as the
588+
// Kotlin Gradle Plugin rejecting `android.newDsl=true`, propagates so that
589+
// the build reports the real cause.
586590
try {
587591
pluginManager.apply("kotlin-android")
588-
} catch (_: Exception) {
592+
} catch (_: UnknownPluginException) {
589593
logger.quiet(
590594
"""
591595
Applying the Kotlin Android Plugin (KGP) was unsuccessful. KGP was not found on the classpath.

‎packages/flutter_tools/gradle/src/test/kotlin/FlutterPluginUtilsTest.kt‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import org.gradle.api.internal.project.ProjectInternal
3535
import org.gradle.api.invocation.Gradle
3636
import org.gradle.api.logging.Logger
3737
import org.gradle.api.plugins.PluginManager
38+
import org.gradle.api.plugins.UnknownPluginException
3839
import org.gradle.api.provider.Provider
3940
import org.gradle.api.provider.ProviderFactory
4041
import org.gradle.kotlin.dsl.support.serviceOf
@@ -1818,8 +1819,8 @@ class FlutterPluginUtilsTest {
18181819
val appPluginManager = testProject.appPluginManager
18191820
val plugin1Manager = testProject.plugin1Manager
18201821

1821-
every { appPluginManager.apply("kotlin-android") } throws Exception("KGP not on classpath")
1822-
every { plugin1Manager.apply("kotlin-android") } throws Exception("KGP not on classpath")
1822+
every { appPluginManager.apply("kotlin-android") } throws UnknownPluginException("KGP not on classpath")
1823+
every { plugin1Manager.apply("kotlin-android") } throws UnknownPluginException("KGP not on classpath")
18231824

18241825
executeDetectApplyingKotlinGradlePlugin(testProject)
18251826

@@ -1837,6 +1838,36 @@ class FlutterPluginUtilsTest {
18371838
)
18381839
}
18391840
}
1841+
1842+
@Test
1843+
fun `propagates a KGP failure that is not a missing plugin`(
1844+
@TempDir tempDir: Path
1845+
) {
1846+
val testProject =
1847+
setupTest(
1848+
tempDir = tempDir,
1849+
agpVersion = templateAgpVersion,
1850+
builtInKotlin = "false",
1851+
appConfig =
1852+
SubprojectConfig(
1853+
"app",
1854+
declarativelyAppliedPlugins = listOf("com.android.application")
1855+
),
1856+
pluginConfigs = emptyList()
1857+
)
1858+
val kgpFailure = GradleException("KGP rejected android.newDsl=true")
1859+
every { testProject.appPluginManager.apply("kotlin-android") } throws kgpFailure
1860+
1861+
val thrown =
1862+
assertThrows<GradleException> {
1863+
detectApplyingKotlinGradlePlugin(testProject.appProject)
1864+
}
1865+
1866+
assertEquals(kgpFailure, thrown)
1867+
verify(exactly = 0) {
1868+
mockLogger.quiet(match { it.contains("KGP was not found on the classpath") })
1869+
}
1870+
}
18401871
}
18411872

18421873
@Nested
@@ -1930,8 +1961,8 @@ class FlutterPluginUtilsTest {
19301961
val appPluginManager = testProject.appPluginManager
19311962
val plugin1Manager = testProject.plugin1Manager
19321963

1933-
every { appPluginManager.apply("kotlin-android") } throws Exception("KGP not on classpath")
1934-
every { plugin1Manager.apply("kotlin-android") } throws Exception("KGP not on classpath")
1964+
every { appPluginManager.apply("kotlin-android") } throws UnknownPluginException("KGP not on classpath")
1965+
every { plugin1Manager.apply("kotlin-android") } throws UnknownPluginException("KGP not on classpath")
19351966

19361967
executeDetectApplyingKotlinGradlePlugin(testProject)
19371968

0 commit comments

Comments
 (0)