Sitelet https://github.com/flutter/flutter/pull/190776
Skip to content

[tool] Migrate BuildApkCommand and Android build toolchain to modular dependency injection - #190776

Merged
auto-submit[bot] merged 31 commits into
flutter:masterfrom
bkonyi:di/14-android-build-and-toolchain
Sep 15, 2026
Merged

auto-submit[bot] merged 31 commits into
flutter:masterfrom
bkonyi:di/14-android-build-and-toolchain

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 14 of the modular dependency injection migration.

  • Migrates BuildApkCommand (flutter build apk) and shared Android toolchain components to modular constructor dependency injection:
    BuildApkCommand({
      required AndroidBuilder androidBuilder,
      required AndroidContext androidContext,
      required BuildSystem buildSystem,
      required ToolContext toolContext,
      bool verboseHelp = false,
    })
  • Requires non-nullable AndroidBuilder in BuildCommand and AndroidGradleBuilder, eliminating ambient globals.dart and context.get<AndroidBuilder>() fallbacks.
  • Requires explicit Analytics and Logger in exitWithNoSdkMessage.
  • Defers BuildAppBundleCommand and BuildAarCommand migrations to stacked follow-up PRs (#190778 / di/14b-build-appbundle and di/14c-build-aar).
  • Converts Android build command tests to hermetic testWithoutContext where applicable.

Part of #188471

@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. team-android Owned by Android platform team team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Aug 8, 2026
@bkonyi
bkonyi force-pushed the di/14-android-build-and-toolchain branch 6 times, most recently from a9748ac to 9e45bc1 Compare August 12, 2026 18:39
@bkonyi
bkonyi force-pushed the di/14-android-build-and-toolchain branch from 9e45bc1 to fdb15cd Compare September 2, 2026 13:26
@bkonyi
bkonyi marked this pull request as ready for review September 2, 2026 13:26
@bkonyi
bkonyi requested a review from a team as a code owner September 2, 2026 13:26
@bkonyi
bkonyi requested review from camsim99 and removed request for a team September 2, 2026 13:26
@bkonyi bkonyi added the CICD Run CI/CD label Sep 2, 2026
@github-actions github-actions Bot removed team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Sep 2, 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 refactors BuildCommand and its subcommands (BuildAarCommand, BuildApkCommand, and BuildAppBundleCommand) to explicitly inject dependencies like AndroidContext and ToolContext instead of relying on global variables, and updates associated tests to run without context. The review feedback highlights a few areas for improvement: avoiding the use of OutputPreferences.test() as a fallback in production code, caching the project getter in BuildAarCommand and BuildAppBundleCommand to prevent redundant file system I/O and object creation, and ensuring that casting terminal to AnsiTerminal does not discard custom mock implementations in tests.

Comment thread packages/flutter_tools/lib/src/commands/build.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_aar.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_appbundle.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build.dart Outdated
@bkonyi

bkonyi commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 refactors BuildCommand and its subcommands (BuildAarCommand, BuildApkCommand, and BuildAppBundleCommand) to support dependency injection of ToolContext, AndroidContext, and AndroidBuilder instead of relying on global variables, enabling hermetic testing without context overrides. A critical bug was identified in BuildCommand where omitting the androidBuilder argument during instantiation in production causes it to default to null, resulting in Android builds silently doing nothing. The feedback suggests falling back to context.get() when the injected builder is null.

Comment thread packages/flutter_tools/lib/src/commands/build.dart Outdated
@bkonyi

bkonyi commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 refactors the Android build commands (BuildAarCommand, BuildApkCommand, and BuildAppBundleCommand) to accept their dependencies directly through their constructors instead of relying on global context, enabling them to be tested using testWithoutContext. Feedback suggests caching the _featureFlags getter in FlutterCommand to prevent redundant object allocations and improve performance.

Comment thread packages/flutter_tools/lib/src/runner/flutter_command.dart Outdated
@bkonyi

bkonyi commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 refactors the build command infrastructure in the Flutter tools by introducing ToolContext and AndroidContext to centralize dependencies. It updates BuildCommand and its subcommands (BuildAarCommand, BuildApkCommand, BuildAppBundleCommand) to use these contexts, reducing redundant parameter passing. Additionally, it cleans up test code by introducing fake contexts and updates the exitWithNoSdkMessage utility to accept optional dependencies. The review feedback suggests further simplifying the command constructors and getters by leveraging the androidContext property to access androidSdk and avoiding unnecessary terminal downcasts.

Comment thread packages/flutter_tools/lib/src/commands/build.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build.dart
Comment thread packages/flutter_tools/lib/src/commands/build_aar.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_aar.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_aar.dart
Comment thread packages/flutter_tools/lib/src/commands/build_apk.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_apk.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_appbundle.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_appbundle.dart Outdated
Comment thread packages/flutter_tools/lib/src/commands/build_appbundle.dart Outdated
@bkonyi
bkonyi marked this pull request as ready for review September 9, 2026 03:59

@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 refactors the Android build tooling, specifically AndroidGradleBuilder and BuildApkCommand, to accept context and builder dependencies via their constructors instead of relying on global variables. This change improves testability, and corresponding unit tests have been added or updated. Feedback on the changes notes a regression in gradle_utils.dart where the error parameter was omitted from the Event.flutterBuildInfo telemetry call, which should be restored.

Comment thread packages/flutter_tools/lib/src/android/gradle_utils.dart Outdated
@bkonyi
bkonyi requested a review from chingjun September 11, 2026 14:55
chingjun
chingjun previously approved these changes Sep 11, 2026

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

reapproving after rebase/merge.

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 15, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Sep 15, 2026
Merged via the queue into flutter:master with commit 416cfc7 Sep 15, 2026
23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 15, 2026
pull Bot pushed a commit to Little-Star888/flutter that referenced this pull request Sep 17, 2026
…lutter#192246)

## Summary

Part 14b of the modular dependency injection migration.

Migrates `BuildAppBundleCommand` (`flutter build appbundle`) to modular
dependency injection with `ToolContext` and `AndroidContext`, removing
direct dependencies on `globals.dart`.

Stacked on flutter#190776
Compare:
bkonyi/flutter@di/14-android-build-and-toolchain...di/14b-build-appbundle

Part of flutter#47161

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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