Repository navigation
[flutter_tools] Add Gradle error handlers for android.newDsl failures - #195742
reidbaker-agent wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
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.
| } on UnimplementedError { | ||
| return false; | ||
| } on FileSystemException { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
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.
| } on UnimplementedError { | |
| return false; | |
| } on FileSystemException { | |
| return false; | |
| } | |
| } on UnimplementedError { | |
| return false; | |
| } on Exception { | |
| return false; | |
| } |
There was a problem hiding this comment.
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.
| 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'; | ||
| } |
There was a problem hiding this comment.
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.
| 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'; | |
| } |
There was a problem hiding this comment.
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.
|
The Flutter error messages look good (is actionable for Flutter Android developers). Just had a nit. |
jesswrd
left a comment
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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:" ' |
There was a problem hiding this comment.
| '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' |
| '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' |
There was a problem hiding this comment.
"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:
| '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.
|
@jesswrd I am considering closing this. Do you think this is a clear win? |
@reidbaker I think at the very least you should merge |
Adds
kotlinAndroidPluginWithNewDslErrorHandlerandlegacyVariantApiErrorHandlertogradle_errors.dartso Android builds failing under Android Gradle Plugin 9's new DSL (android.newDsl=true) display actionableFlutter Fixguidance:kotlinAndroidPluginWithNewDslErrorHandler: Matches Kotlin Gradle plugin failures whenorg.jetbrains.kotlin.android(kotlin-android) is applied whileandroid.newDslis enabled andandroid.builtInKotlinis disabled (whether applied automatically by Flutter whenandroid.builtInKotlin=falseis set, or directly in an app or Flutter plugin Gradle build script). Directs developers to the immediateandroid.newDsl=falseopt-out inandroid/gradle.properties(or.android/gradle.propertiesfor add-to-app modules) so the app can build and log warnings naming projects still applyingkotlin-android, and links to the built-in Kotlin migration guide before enablingandroid.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 underandroid.newDsl=true, while avoiding false positives on.ktsource files,kotlinOptions, or projects whereandroid.newDsl=falseis already active. Directs developers toandroid.newDsl=falseand migration tocom.android.build.api.dslandandroidComponents.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-assistbot 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.