Skip to content

[add-to-app] FGP throws NPE in getLegacyAndroidExtension when the host app builds with AGP 9 defaults (newDsl) #191703

Open

Description

@Daracul

Filed as a separate issue at @jesswrd's request from #181557 (comment).

Steps to reproduce

  1. Android host app on AGP 9.2.1 / Gradle 9.6.1 (Kotlin DSL build scripts).
  2. Flutter module included from source via include_flutter.groovy (add-to-app).
  3. AGP 9 defaults left as is (android.newDsl / android.builtInKotlin not overridden in the host's gradle.properties).
  4. Run Gradle sync (or any configuration).

Flutter 3.47.1 stable / Dart 3.13.1. All plugin dependencies already migrated off unconditional KGP.

Expected results

Configuration succeeds, or fails with a clear actionable message.

Actual results

Applying dev.flutter.flutter-gradle-plugin to the module project fails:

Caused by: java.lang.NullPointerException
    at com.flutter.gradle.FlutterPluginUtils.getLegacyAndroidExtension$gradle(FlutterPluginUtils.kt:514)
    at com.flutter.gradle.plugins.PluginHandler$Companion.configurePluginDependencies(PluginHandler.kt:218)
    at com.flutter.gradle.plugins.PluginHandler.configurePlugins$gradle(PluginHandler.kt:69)
    at com.flutter.gradle.FlutterPlugin.addFlutterTasks(FlutterPlugin.kt:505)
    at com.flutter.gradle.FlutterPlugin.apply(FlutterPlugin.kt:136)

Analysis

With newDsl=true (the AGP 9 default) the legacy BaseExtension is not registered, so getLegacyAndroidExtension's project.extensions.findByType(BaseExtension::class.java)!! throws. getLegacyAndroidExtension has ~14 call sites in 3.47.1 (PluginHandler.kt, FlutterPlugin.kt:249, FlutterPluginUtils.kt:533/797/923), so subsequent failures are likely once the first path is fixed — #191218 appears to cover this first one (buildModeFor / plugin dependency wiring).

Workaround

Per the migration docs: set android.newDsl=false and android.builtInKotlin=false in the host app's gradle.properties. Works, confirmed.

Related

#180137 (newDsl migration umbrella), #181557 (AGP 9 umbrella).

Happy to test snapshots against our host app once the add-to-app path is wired through the new DSL.

Activity

  1. added
    c: crashStack traces logged to the console
    platform-androidAndroid applications specifically
    toolAffects the "flutter" command-line tool. See also t: labels.
    workaround availableThere is a workaround available to overcome the issue
    team-androidOwned by Android platform team
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Aug 25, 2026
  2. mbcorona commented on Aug 25, 2026

    @mbcorona
    Member

    Thanks for the report and stack trace. Confirmed cause: getLegacyAndroidExtension does findByType(BaseExtension)!!, which NPEs under AGP 9's newDsl default since the legacy BaseExtension isn't registered. Routing to the Android team for further investigation.

  3. mboetger commented on Aug 25, 2026

    @mboetger
    Contributor

    To @jesswrd to investigate a better error messaging

  4. added
    P1High-priority issues at the top of the work list
    triaged-androidTriaged by Android platform team
    on Aug 25, 2026
  5. added a commit that references this issue on Oct 12, 2026
  6. Daracul commented on Oct 19, 2026

    @Daracul
    Author

    Confirming the fix from #192116 on a production add-to-app setup.

    Setup

    • Flutter 3.50.0-0.1.pre (beta), module included from source via include_flutter.groovy
    • Host: AGP 9.2.1, Gradle 9.6.1, Kotlin 2.4.10, AGP 9 defaults (android.newDsl not overridden, android.builtInKotlin=true), no org.jetbrains.kotlin.android applied anywhere in the host
    • 18 plugin projects, incl. shared_preferences_android 2.4.28 and pdfx 2.11.0

    Result
    :app:assembleDebug builds, the app runs and Flutter screens work on a device. The getLegacyAndroidExtension NPE from this report is gone, so from our side this issue can be closed once the fix reaches stable.

    Two add-to-app gaps we hit on the way (not this bug, reporting for completeness; happy to file them separately if preferred):

    1. Kotlin-DSL build scripts of plugins fail with Unresolved reference 'compileSdkVersion' on compileSdk = flutter.compileSdkVersion (e.g. shared_preferences_android 2.4.28, build.gradle.kts:42). In add-to-app the flutter extension type is not on the host root buildscript classpath, so the Kotlin DSL cannot generate the accessor. The workaround from [add2app] [flutter_plugin_android_lifecycle] Gradle Kotlin Script Error: Unresolved Reference compileSdkVersion #188339 (declare dev.flutter.flutter-gradle-plugin with apply false in the host root plugins {}) works, but only when the included build is present, i.e. only in the from-source configuration. [add2app] [flutter_plugin_android_lifecycle] Gradle Kotlin Script Error: Unresolved Reference compileSdkVersion #188339 was closed as a duplicate of [flutter_plugin_android_lifecycle] Gradle Kotlin Script Error: Unresolved Reference compileSdkVersion #184133, which was then closed for lack of info, so the gap currently has no open tracking issue.

    2. :flutter:compileFlutterBuildDebug (FlutterTask) calls Task.project at execution time, which Gradle 9.6 reports as a configuration cache problem (8 problems, 1 unique). Not fatal in warning mode.

  7. added a commit that references this issue on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listc: crashStack traces logged to the consolehas reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-androidTriaged by Android platform teamworkaround availableThere is a workaround available to overcome the issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions