fix(battery_plus): apply KGP unless built-in Kotlin is enabled - #3946
fix(battery_plus): apply KGP unless built-in Kotlin is enabled#39464akloon wants to merge 1 commit into
Conversation
The guard assumed AGP 9 always means built-in Kotlin is active. AGP enables built-in Kotlin only when android.builtInKotlin is not explicitly false, and `flutter create` writes android.builtInKotlin=false into every new app. On AGP 9 with android.builtInKotlin=false, the plugin skipped KGP and AGP provided no Kotlin either, so nothing compiled the plugin's Kotlin sources and configuration failed at the KotlinAndroidProjectExtension lookup. This was masked only because Flutter's kgpRegexKotlin does not match the imperative apply(plugin = "...") form, so Flutter applied KGP as a fallback on the plugin's behalf. Guard on the same condition AGP itself uses: built-in Kotlin is on when AGP >= 9 and android.builtInKotlin is not explicitly false. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Confirming this downstream. We hit exactly this on a Flutter app with AGP 9: flutter create writes android.builtInKotlin=false, so the agpMajor < 9 guard skips applying KGP while AGP doesn't supply built-in Kotlin either - leaving neither, and the build dies with UnknownDomainObjectException: Extension of type 'KotlinAndroidProjectExtension' does not exist. Checking the property rather than inferring from the AGP version is the right fix. We're currently pinning a fork carrying just this commit and #3950 so we can move to AGP 9.3.2. With both applied, verified green: assembleDebug, assembleRelease (R8 full mode), and unit tests across the app and a Wear module. Reverting either reproduces the error. FWIW the failing checks (as of the time I wrote this comment) here look unrelated to the change: they're iOS/macOS/Windows jobs failing on Unable to load contents of file list: '/Target Support Files/Pods-Runner/...xcfilelist' (a CocoaPods PODS_ROOT path issue). While every Android check passes (including integration tests on API 24/29/32/36) other open PRs show the same 5 failures, so it looks repo-wide rather than PR-specific. |
Description
battery_plus's Android build script decided whether to apply the Kotlin Gradle Plugin (KGP) from the AGP major version alone:That assumes AGP 9 always means built-in Kotlin is active. AGP 9 enables built-in Kotlin only when
android.builtInKotlinis not explicitlyfalse— andandroid.builtInKotlin=falseis exactly whatflutter createwrites into every new app'sandroid/gradle.properties, and what this repository's example apps set.So on AGP 9 +
android.builtInKotlin=falsethe plugin skipped KGP and AGP provided no Kotlin either. Nothing compiled the plugin's Kotlin sources, and configuration failed a few lines below at the unconditional extension lookup:This is not visible today only because Flutter's
FlutterPluginUtils.kgpRegexKotlinmatchesplugins { }blocks but not the imperativeapply(plugin = "…")form used here, so Flutter does not see the declaration and applies KGP itself — accidentally papering over the broken guard. Flutter already warns that this fallback is going away.This PR guards on the same condition AGP itself uses:
The
KotlinAndroidProjectExtensionblock below is unchanged — AGP 9's built-in Kotlin registers that extension, so it resolves under both branches. On AGP 8 behaviour is provably identical to today:agpMajor >= 9is false, so KGP is applied exactly as before.Full mechanism, AGP bytecode evidence and reproduction steps are in #3944.
Verification
builtInKotlin=false, with Flutter's fallback regex patched out, before fixKotlinAndroidProjectExtension does not existflutter build apk --debugsucceedsbuiltInKotlin=truemelos run analyzeandmelos run test:unit_allboth pass.Related Issues
Checklist
CHANGELOG.mdnor the plugin version inpubspec.yamlfiles.flutter analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?
!in the title as explained in Conventional Commits).