Skip to content

[flutter_tools] Add Gradle error handlers for android.newDsl failures - #195742

Draft
reidbaker-agent wants to merge 6 commits into
flutter:masterfrom
reidbaker-agent:agp-newdsl-error-handlers
Draft

reidbaker-agent wants to merge 6 commits into
flutter:masterfrom
reidbaker-agent:agp-newdsl-error-handlers

Conversation

@reidbaker-agent

@reidbaker-agent reidbaker-agent commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Adds kotlinAndroidPluginWithNewDslErrorHandler and legacyVariantApiErrorHandler to gradle_errors.dart so Android builds failing under Android Gradle Plugin 9's new DSL (android.newDsl=true) display actionable Flutter Fix guidance:

  • kotlinAndroidPluginWithNewDslErrorHandler: Matches Kotlin Gradle plugin failures when org.jetbrains.kotlin.android (kotlin-android) is applied while android.newDsl is enabled and android.builtInKotlin is disabled (whether applied automatically by Flutter when android.builtInKotlin=false is set, or directly in an app or Flutter plugin Gradle build script). Directs developers to the immediate android.newDsl=false opt-out in android/gradle.properties (or .android/gradle.properties for add-to-app modules) so the app can build and log warnings naming projects still applying kotlin-android, and links to the built-in Kotlin migration guide before enabling android.builtInKotlin=true.
  • legacyVariantApiErrorHandler: Matches Groovy and Kotlin Gradle build script or Gradle plugin failures caused by legacy Android Gradle Plugin extension types (BaseExtension, LibraryExtension, AppExtension, BaseAppModuleExtension, TestedExtension, TestExtension) and legacy variant APIs (applicationVariants, libraryVariants, testVariants, unitTestVariants, variantFilter) removed under android.newDsl=true, while avoiding false positives on .kt source files, kotlinOptions, or projects where android.newDsl=false is already active. Directs developers to android.newDsl=false and migration to com.android.build.api.dsl and androidComponents.onVariants / androidComponents.beforeVariants.

Part of #180137
Part of #166550

Pre-launch Checklist

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. 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.

@reidbaker-agent
reidbaker-agent requested a review from a team as a code owner October 31, 2026 15:44
@reidbaker-agent
reidbaker-agent requested review from camsim99 and removed request for a team October 31, 2026 15:44
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. team-android Owned by Android platform team labels Oct 31, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces two new Gradle error handlers, kotlinAndroidPluginWithNewDslErrorHandler and legacyVariantApiErrorHandler, to handle build failures related to the Android Gradle Plugin's new DSL (android.newDsl=true) and legacy variant APIs. It also adds helper functions to parse gradle.properties and includes comprehensive unit tests. The review feedback suggests catching Exception instead of FileSystemException to safely handle potential FormatException from invalid UTF-8 when reading properties, and refining the opt-out instructions to avoid redundant setup suggestions when android.newDsl=false is already configured.

Comment on lines +1003 to +1007
} on UnimplementedError {
return false;
} on FileSystemException {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching Exception instead of FileSystemException is safer and more robust here, as readAsLinesSync() can also throw a FormatException if the gradle.properties file contains invalid UTF-8 characters. This prevents the tool from crashing during error handling.

Suggested change
} on UnimplementedError {
return false;
} on FileSystemException {
return false;
}
} on UnimplementedError {
return false;
} on Exception {
return false;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated _isNewDslDisabledInGradleProperties in 3d409a1 to catch on FormatException explicitly alongside on FileSystemException and on UnimplementedError (rather than catching broad on Exception, which would mask unrelated runtime exceptions), and added unit tests covering invalid UTF-8 bytes and FormatException during gradle.properties reads.

Comment on lines +1010 to +1020
String _newDslOptOutInstructions(FlutterProject project, {required String gradlePropertiesPath}) {
final overrideNote = _isNewDslDisabledInGradleProperties(project)
? '$gradlePropertiesPath already sets android.newDsl=false; check '
'~/.gradle/gradle.properties and -Pandroid.newDsl command-line flags '
'for an override.\n'
: '';
return 'To build now, set android.newDsl=false in $gradlePropertiesPath.\n'
'$overrideNote'
'Android Gradle Plugin 10.0 removes the android.newDsl=false option.\n'
'For details, see: $kOptOutOfNewDslDocsUrl';
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When android.newDsl=false is already set in gradle.properties, instructing the user to set it again is redundant and confusing. We should omit the "To build now, set android.newDsl=false..." instruction if it is already configured.

Suggested change
String _newDslOptOutInstructions(FlutterProject project, {required String gradlePropertiesPath}) {
final overrideNote = _isNewDslDisabledInGradleProperties(project)
? '$gradlePropertiesPath already sets android.newDsl=false; check '
'~/.gradle/gradle.properties and -Pandroid.newDsl command-line flags '
'for an override.\n'
: '';
return 'To build now, set android.newDsl=false in $gradlePropertiesPath.\n'
'$overrideNote'
'Android Gradle Plugin 10.0 removes the android.newDsl=false option.\n'
'For details, see: $kOptOutOfNewDslDocsUrl';
}
String _newDslOptOutInstructions(FlutterProject project, {required String gradlePropertiesPath}) {
if (_isNewDslDisabledInGradleProperties(project)) {
return '$gradlePropertiesPath already sets android.newDsl=false; check '
'~/.gradle/gradle.properties and -Pandroid.newDsl command-line flags '
'for an override.\n'
'Android Gradle Plugin 10.0 removes the android.newDsl=false option.\n'
'For details, see: $kOptOutOfNewDslDocsUrl';
}
return 'To build now, set android.newDsl=false in $gradlePropertiesPath.\n'
'Android Gradle Plugin 10.0 removes the android.newDsl=false option.\n'
'For details, see: $kOptOutOfNewDslDocsUrl';
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated _newDslOptOutInstructions in 3d409a1 to omit "To build now, set android.newDsl=false..." when gradle.properties already sets android.newDsl=false, and instead instruct developers to remove any android.newDsl=true override in ~/.gradle/gradle.properties or -Pandroid.newDsl command-line flags.

…dance

Catches FormatException alongside FileSystemException when reading gradle.properties in _isNewDslDisabledInGradleProperties, and omits the redundant set android.newDsl=false instruction when gradle.properties already sets android.newDsl=false.
@camsim99
camsim99 requested review from jesswrd and removed request for camsim99 November 2, 2026 18:57
@jesswrd jesswrd added the CICD Run CI/CD label Nov 2, 2026
@jesswrd
jesswrd self-requested a review November 2, 2026 20:03
@jesswrd

jesswrd commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

The Flutter error messages look good (is actionable for Flutter Android developers). Just had a nit.

@jesswrd jesswrd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and repro-ed because the error message stated Gradle identifies the failing projects at the Flutter tooling level (which shocked me). Turns out, Gradle does not (because the build fails before Gradle can accurately report). Please update the error message.

'in $gradlePropertiesPath, or a Gradle build script in the app or in a Flutter plugin '
'may apply kotlin-android directly.\n'
'Gradle identifies the build script or project under "* Where:" or "* What went wrong:" '
'above.\n\n'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is misleading. With android.builtInKotlin=false, the Flutter Gradle Plugin applies kotlin-android to the app and fails before any plugin is configured, so * Where: always points at app/build.gradle.kts line: 1 (see the test below here). I repro'd with a migrated app depending on two KGP-applying plugins, and neither plugin is mentioned anywhere in the Android error.

I suggest dropping this sentence. At this point the developer can't tell whether their app or one of their plugins is at fault. If the project is not fully migrated to built-in Kotlin, the only way to find out is to set android.newDsl=false so the app builds; the Flutter Gradle Plugin then logs a warning listing every plugin that still applies KGP. If the developer is confident the project is fully migrated, they can set android.builtInKotlin=true instead.

'Flutter applies kotlin-android automatically when android.builtInKotlin=false is set '
'in $gradlePropertiesPath, or a Gradle build script in the app or in a Flutter plugin '
'may apply kotlin-android directly.\n'
'Gradle identifies the build script or project under "* Where:" or "* What went wrong:" '

@jesswrd jesswrd Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Gradle identifies the build script or project under "* Where:" or "* What went wrong:" '
'To see whether your app or any of its plugins still apply kotlin-android, the app '
'must build. Flutter then logs a warning naming each project that still applies the kotlin-android plugin.\n\n'

Comment on lines +1061 to +1063
'Otherwise, set android.builtInKotlin=true in $gradlePropertiesPath and remove any '
"'org.jetbrains.kotlin.android' (kotlin-android) declaration from the app's Gradle "
'build script.\n'

@jesswrd jesswrd Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Otherwise" reads as "if no plugin is at fault, set android.builtInKotlin=true and fix the app," but the flag is only safe once the app and all of its plugins are migrated. Suggest separating the app fix from the flag. I also want to avoid suggesting that migrating to built-in Kotlin only involves removing KGP, when it also involves replacing android.kotlinOptions{} DSL block with kotlin.compilerOptions{} DSL block. So I'm suggesting this:

Suggested change
'Otherwise, set android.builtInKotlin=true in $gradlePropertiesPath and remove any '
"'org.jetbrains.kotlin.android' (kotlin-android) declaration from the app's Gradle "
'build script.\n'
"If your app applies kotlin-android, migrate it to built-in Kotlin.\n"
'Once your app and all of its Flutter plugins are migrated to built-in Kotlin, set '
'android.builtInKotlin=true in $gradlePropertiesPath.\n'

…tion guidance

Updates kotlinAndroidPluginWithNewDslErrorHandler to explain that the app must build before Flutter logs warnings naming each project applying kotlin-android, and separates app migration to built-in Kotlin from setting android.builtInKotlin=true once the app and all Flutter plugins are migrated.
…box assertions

Explains why _ambiguousScriptNewDslPatterns is separated from _runtimeConfirmedNewDslPatterns and asserts module .android/gradle.properties checks against unwrapped box text.
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 3, 2026
@reidbaker reidbaker added the CICD Run CI/CD label Nov 3, 2026
@reidbaker

Copy link
Copy Markdown
Member

@jesswrd I am considering closing this. Do you think this is a clear win?

@reidbaker
reidbaker marked this pull request as draft November 3, 2026 20:42
@jesswrd

jesswrd commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@jesswrd I am considering closing this. Do you think this is a clear win?

@reidbaker I think at the very least you should merge kotlinAndroidPluginWithNewDslErrorHandler. The raw Gradle error already suggests android.newDsl=false, but it doesn't explain why a Flutter app hits this error (FGP applies KGP automatically when android.newDsl=false is set) and the Gradle error does not point to Flutter migration docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD team-android Owned by Android platform team tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants