Skip to content

Commit 9a813a8

Browse files
[CP-beta][FGP] Report the real error when applying kotlin-android fails (#195691)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: #181557 ### Impact Description: Cherry-pick of #195080 (also tracks #189236 and #180137). On Flutter 3.50 beta (`flutter-3.50-candidate.0`), when an Android project sets `android.newDsl=true` while `android.builtInKotlin=false`, `FlutterPluginUtils.detectApplyingKotlinGradlePlugin` catches all exceptions from `pluginManager.apply("kotlin-android")`. Instead of surfacing the Kotlin Gradle Plugin's actual incompatibility message (`The 'org.jetbrains.kotlin.android' plugin is not compatible with AGP's 9.0 new DSL` along with instructions to set `android.builtInKotlin=true` or `android.newDsl=false`), Flutter Gradle Plugin prints `"Applying the Kotlin Android Plugin (KGP) was unsuccessful. KGP was not found on the classpath."` and the build fails later with an unrelated script compilation error (`Unresolved reference: kotlin` / `compilerOptions`). Narrowing the catch to `UnknownPluginException` surfaces KGP's real error and remediation guidance to developers adopting AGP 9's new DSL in 3.50. ### Changelog Description: [flutter/181557](#181557): Android builds with `android.newDsl=true` and `android.builtInKotlin=false` misreport a missing Kotlin Gradle Plugin instead of the actual incompatibility error. ### Workaround: Set `android.builtInKotlin=true` (after migrating off `org.jetbrains.kotlin.android`) or `android.newDsl=false` in `android/gradle.properties`; however, without this fix the build output misleads developers into debugging a missing KGP classpath dependency instead of the `builtInKotlin`/`newDsl` mismatch. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. Create a new Flutter Android app (`flutter create my_app`) using AGP 9.x. 2. Set `android.builtInKotlin=false` and `android.newDsl=true` in `android/gradle.properties`. 3. Run `flutter build apk` and verify that Gradle reports KGP's real error (`The 'org.jetbrains.kotlin.android' plugin is not compatible with AGP's 9.0 new DSL` with remediation steps) instead of `"KGP was not found on the classpath."` (also covered by unit tests in `FlutterPluginUtilsTest.kt`).
1 parent 4474c2f commit 9a813a8

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)