Skip to content

[flutter_tools] Lazily initialize AndroidSdk platform and build-tools discovery - #191972

Merged
auto-submit[bot] merged 10 commits into
flutter:masterfrom
bkonyi:optimize_androidsdk_constructor_initialization
Sep 29, 2026
Merged

auto-submit[bot] merged 10 commits into
flutter:masterfrom
bkonyi:optimize_androidsdk_constructor_initialization

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Description

This PR optimizes AndroidSdk initialization by deferring expensive synchronous filesystem operations out of the constructor.

Why this is needed now

As part of the modular dependency injection migration (#190724), context objects like AndroidContext are created upfront during ToolDependencies.bootstrap / runner initialization rather than via the old global ambient context (AppContext).

Previously, AndroidSdk was initialized lazily only when a command or service queried globals.androidSdk or context.get<AndroidSdk>(). With upfront AndroidContext creation, AndroidSdk.locateAndroidSdk() runs on tool startup for every command. Because the AndroidSdk constructor eagerly executed reinitialize(), it performed synchronous directory listings across build-tools and platforms directories and read build.prop files on disk. This introduced unnecessary disk I/O and latency to tool startup, including for commands completely unrelated to Android (such as flutter config or iOS-specific workflows).

Changes

  • Defers synchronous scanning of build-tools and platform directories (reinitialize()) out of the AndroidSdk constructor so that object instantiation is fast and side-effect free.
  • sdkVersions and latestVersion are evaluated lazily on first access.
  • Retains _fileSystem on the instance so that explicit calls to reinitialize() continue to refresh platform and version state with the expected filesystem.

Related Issues

Part of #47161 (flutter_tools modular dependency injection migration).

Tests

  • Updated existing test in packages/flutter_tools/test/general.shard/android/android_sdk_test.dart (constructing an AndroidSdk handles no matching lines in build.prop) to access sdk.latestVersion so that build.prop parsing error-handling is executed during lazy initialization.
  • Added unit tests verifying:
    • Constructor instantiation does not initialize sdkVersions or latestVersion or scan platform directories.
    • sdkVersions and latestVersion evaluate lazily on first access across independent instances.
    • Calling reinitialize() updates sdkVersions and latestVersion and correctly falls back to stored _fileSystem.

… discovery

Defers synchronous scanning of build-tools and platform directories (`reinitialize()`)
out of the `AndroidSdk` constructor so that object instantiation is fast and side-effect free.
Access to `sdkVersions` and `latestVersion` now evaluates discovery lazily on first access.
Explicit calls to `reinitialize()` continue to refresh platform and version state as before.
@bkonyi
bkonyi requested a review from a team as a code owner August 28, 2026 17:04
@bkonyi
bkonyi requested review from jesswrd and removed request for a team August 28, 2026 17:04
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 28, 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 the AndroidSdk class to lazily initialize sdkVersions and latestVersion on first access instead of during constructor instantiation. This change is supported by new tests verifying the lazy evaluation and reinitialization behavior. The review feedback recommends adding triple-slash (///) documentation comments to the newly introduced private members (_fileSystem, _reinitialized, and _ensureInitialized()) to comply with the repository's style guide.

Comment thread packages/flutter_tools/lib/src/android/android_sdk.dart Outdated
… in AndroidSdk

Removes redundant `fileSystem` parameter and field from `AndroidSdk` and `reinitialize()`,
relying directly on `directory.fileSystem`.
@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 Aug 28, 2026
@bkonyi
bkonyi requested a review from jason-simmons August 28, 2026 17:12
bkonyi added 3 commits August 28, 2026 18:32
…dle_builder_test.dart

Updates android_gradle_builder_test.dart to align with AndroidSdk constructor signature.
jesswrd
jesswrd previously approved these changes Sep 15, 2026
jesswrd
jesswrd previously approved these changes Sep 16, 2026

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

RSLGTM

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 29, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 29, 2026
@auto-submit

auto-submit Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/191972, because The base commit of the PR is older than 7 days and can not be merged. Please merge the latest changes from the main into this branch and resubmit the PR.

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 29, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Sep 29, 2026
Merged via the queue into flutter:master with commit 36b4b8f Sep 29, 2026
22 of 23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 29, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Sep 30, 2026
flutter/flutter@55b8f88...d649d2b

2026-09-30 [email protected] [Android] Update the CLI to reject passing engine configuration flags with a prebuilt binary in release mode (flutter/flutter#190870)
2026-09-30 [email protected] Roll Skia from af1e8b356dd1 to c7b323126bf0 (1 revision) (flutter/flutter#193569)
2026-09-30 [email protected] [web] Respect text affinity in getLineBoundary at a soft wrap (flutter/flutter#192664)
2026-09-30 [email protected] [tool] Migrate TestCommand and platform runner to modular dependency injection (flutter/flutter#190789)
2026-09-30 [email protected] Roll Skia from 3a20a0464d25 to af1e8b356dd1 (1 revision) (flutter/flutter#193564)
2026-09-30 [email protected] Roll Skia from 554ef62d11a9 to 3a20a0464d25 (6 revisions) (flutter/flutter#193556)
2026-09-30 [email protected] Use null aware elements in `platform_views.dart` (flutter/flutter#193214)
2026-09-30 [email protected] Replace deprecated `withOpacity` in `flutter_logo.dart` (flutter/flutter#192583)
2026-09-30 [email protected] Roll Skia from f441ca223b2b to 554ef62d11a9 (72 revisions) (flutter/flutter#193548)
2026-09-30 [email protected] Fix StrcmpFixed matching prefixes in the Vulkan embedder tests (flutter/flutter#192999)
2026-09-30 [email protected] Update Engine to test Android 17 AVD (flutter/flutter#193239)
2026-09-29 [email protected] Give the Vulkan test context the extensions Impeller requires (flutter/flutter#193002)
2026-09-29 [email protected] Rename "subtext" to "supportingTextPadding" in `FormField`'s documentation (flutter/flutter#183582)
2026-09-29 [email protected] Set --no-js-strongly-connected-components for DDC compiles by default (flutter/flutter#193485)
2026-09-29 [email protected] Split pre-AGP 8.3 module AAR test into a Java 17 pinned target (flutter/flutter#193467)
2026-09-29 [email protected] Remove no-shuffle from gen_defaults_test (flutter/flutter#192280)
2026-09-29 [email protected] Update guidance on bumping Dart (flutter/flutter#193131)
2026-09-29 [email protected] Remove vestigial `download_jdk` gclient var (flutter/flutter#188571)
2026-09-29 [email protected] Roll pub packages (flutter/flutter#193522)
2026-09-29 [email protected] [flutter_tools] Lazily initialize AndroidSdk platform and build-tools discovery (flutter/flutter#191972)
2026-09-29 [email protected] [tool] Migrate Web build subcommands and toolchain to modular dependency injection (flutter/flutter#190783)
2026-09-29 [email protected] Pin androidx.test dependencies in integration_test (flutter/flutter#193316)
2026-09-29 [email protected] [flutter_tools] Include flutter.js.map in web builds (flutter/flutter#192257)
2026-09-29 [email protected] [WebParagraph] Fixing edge cases for wrapping text (with newlines) (flutter/flutter#189858)
2026-09-29 [email protected] Bump customer testing version for flutter/devtools update (flutter/flutter#193511)
2026-09-29 [email protected] Roll Packages from ba0364a to 3c6ce59 (14 revisions) (flutter/flutter#193507)
2026-09-29 [email protected] [flutter_tools] Guard socket streams and done futures against socket reset errors (flutter/flutter#192941)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
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