Reland "[Android] Add mechanism for setting Android engine flags via Android manifest (take 2)" (#182522)

### Overview
Re-lands https://github.com/flutter/flutter/pull/181632 which TLDR does
the following:

**Same old same old changes from original PR**
- Refactors all engine flags recognized by the Flutter Android embedding
(specifically those previously recognized by
[`FlutterShellArgs`](https://github.com/flutter/flutter/blob/4f5478cce38d837e14b7a032a12500d3fc0f1310/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java)
and all of those used to initialize the engine in `FlutterLoader`) into
a new class `FlutterEngineFlags` (`FlutterShellArgs` is kept for now to
maintain backwards compatibility; see
https://github.com/flutter/flutter/issues/182153 for details)
- Adds the ability to set engine flags (that are recognized by
`FlutterEngineFlags`) via the manifest
- Adds warnings for developers that set engine flags via `Intent`s as
this will be disabled as part of
https://github.com/flutter/flutter/issues/180686.
- Disallows debug/test only flags in release mode

**New changes/fixes**

> [!NOTE]
> This re-land was required because when previously submitted, it caused
b/483299339. I have confirmed that this bug has been fixed in this PR;
see go/flutter-android-setting-flags-in-manifest-perf-comparison for A/B
test results with the previous version of this PR
(https://github.com/flutter/flutter/pull/181632) and commit
https://github.com/flutter/flutter/pull/182522/commits/d6d4a07ede2052db4a861528ff05df8175bd9d37
of this PR.

- [Critical change] To fix the bug caught by b/483299339,
`--merged-platform-ui-thread` flag is added to `FlutterEngineFlag`s
- [Behavior change, not critical] To avoid more bugs like b/483299339,
`FlutterLoader` allows unrecognized flags specified by the command line
or `Intent` extras to be used to initialize the engine for now (same
behavior as before my original change but I plan to secure this in the
future; see https://github.com/flutter/flutter/issues/182557)
- [Small refactor] To speed up manifest argument processing time,
`FlutterLoader` loops through all recognized `FlutterEngineFlag`s to
check if they are present in an application's manifest versus looping
through all metadata in an application's manifest and looking for
recognized `FlutterEngineFlag`s (small improvement for apps with large
manifests)
- [Small refactor] To speed up command line argument processing time,
`FlutterLoader` processes the `--aot-shared-library-name` flag in the
same loop as the other flags, reducing the number of loops
`ensureInitializationComplete` has to do (minimal improvement perf-wise
but kept it in because it's more readable to me)

### Full description
(Mostly copied from https://github.com/flutter/flutter/pull/181632)

> [!NOTE]
> This PR is based on conversation & feedback on
go/flutter-android-harden-engine-shell-arguments.

Adds a mechanism for setting Android engine flags via the manifest. If a
flag is specified on the command line and in manifest metadata, the
value specified on the command line will take precedence. Documentation
is added on this mechanism

Additionally, this PR removes the exposure of`--cache-sksl` command line
flag as per
[https://github.com/flutter/flutter/issues/140310#issuecomment-2708459007](https://www.google.com/url?q=https://github.com/flutter/flutter/issues/140310%23issuecomment-2708459007&sa=D&source=docs&ust=1761156167162464&usg=AOvVaw3a8ubXTtv3apknY2-P9dKe).

Additionally, this PR adds documentation for the only two supported ways
of setting engine flags moving forward -- via the command line or
manifest. The `Intent` mechanism will be removed when
https://github.com/flutter/flutter/issues/180686 is completed (intended
to be a follow up to this PR).

As the unit tests in this PR only cover setting flags via manifest in
debug mode, I will follow up this PR with an integration test to test
that flags are appropriately respected/ignored in release mode. See
https://github.com/flutter/flutter/pull/178383 for my currently working
but WIP draft.

Part of https://github.com/flutter/flutter/issues/172553.

# Follow up work:

- [ ] **Migrate flags all un-migrated flags `FlutterEngineFlags`**
https://github.com/flutter/flutter/issues/182852
- [ ] **Add integration test for this new added mechanism.** This will
land as an immediate follow-up to this PR. WIP in
https://github.com/flutter/flutter/pull/182241.
- [ ] **Remove support for setting shell arguments via `Intent`.** This
task will be a follow up to this work + the integration test landing,
and will complete work for
https://github.com/flutter/flutter/issues/172553. See
https://github.com/flutter/flutter/issues/180686 for details.
- [ ] **Only allow known engine flags to be passed to the engine.** This
is more of a P2 but would be a nice additional security feature; see
https://github.com/flutter/flutter/issues/182557.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot 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.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
https://dart.googlesource.com/external/github.com/flutter/flutter/+/994b73f795ee7c5d0026d52c4b8be25334d673e4
2 files changed
tree: f72ce86a67360f39035282ae78b34983b3aa69ba
  1. engine/
  2. tools/
  3. .gitignore
  4. commits.json
  5. DEPS
  6. OWNERS
  7. README.md
README.md

Monorepo

A gclient solution for checking out Dart and Flutter source trees

Monorepo is:

  • Optimized for Tip-of-Tree testing: The Monorepo DEPS used to check out Dart and Flutter dependencies comes from the Flutter engine DEPS with updated dependencies from Dart.

Checking out Monorepo

With depot_tools installed and on your path, create a directory for your monorepo checkout and run these commands to create a gclient solution in that directory:

mkdir monorepo
cd monorepo
gclient config --unmanaged https://dart.googlesource.com/monorepo
gclient sync -D

This gives you a checkout in the monorepo directory that contains:

monorepo/
  DEPS - the DEPS used for this gclient checkout
  commits.json - the pinned commits for Dart, flutter/engine,
                 and flutter/flutter
  tools/ - scripts used to create monorepo DEPS
engine/src/ - the flutter/buildroot repo
    flutter/ - the flutter/engine repo
    out/ - the build directory, where Flutter engine builds are created
    third_party/ - Flutter dependencies checked out by DEPS
      dart/ - the Dart SDK checkout.
        third_party - Dart dependencies, also used by Flutter
flutter/ - the flutter/flutter repo

Building Flutter engine

Flutter's instructions for building the engine are at Compiling the engine

They can be followed closely, with a few changes:

  • Googlers working on Dart do not need to switch to Fuchsia's Goma RBE, except for Windows. The GOMA_DIR enviroment variable can just point to the .cipd_bin directory in a depot_tools installation, and just goma_ctl ensure_start is sufficient.
  • The --no-prebuilt-dart-sdk option has to be added to every gn command, so that the build is set up to build and use a local Dart SDK.
  • The --full-dart-sdk option must be added to gn for the host build target if you will be building web or desktop apps.

Example build commands that work on linux:

MONOREPO_PATH=$PWD
if [[ ! $PATH =~ (^|:)$MONOREPO_PATH/flutter/bin(:|$) ]]; then
  PATH=$MONOREPO_PATH/flutter/bin:$PATH
fi

export GOMA_DIR=$(dirname $(command -v gclient))/.cipd_bin
goma_ctl ensure_start

pushd engine/src
flutter/tools/gn --goma --no-prebuilt-dart-sdk --unoptimized --full-dart-sdk
autoninja -C out/host_debug_unopt
popd

Building Flutter apps

The Flutter commands used to build and run apps will use the locally built Flutter engine and Dart SDK, instead of the one downloaded by the Flutter tool, if the --local-engine option is provided.

For example, to build and run the Flutter spinning square sample on the web platform,

MONOREPO_PATH=$PWD
cd flutter/examples/layers
flutter --local-engine=host_debug_unopt \
  -d chrome run widgets/spinning_square.dart
cd $MONOREPO_PATH

To build for desktop, specify the desktop platform device in flutter run as -d macos or -d linux or -d windows. You may also need to run the command

flutter create --platforms=windows,macos,linux

on existing apps, such as sample apps. New apps created with flutter create already include these support files. Details of desktop support are at Desktop Support for Flutter

Testing

Tests in the Flutter source tree can be run with the flutter test command, run in the directory of a package containing tests. For example:

MONOREPO_PATH=$PWD
cd flutter/packages/flutter
flutter test --local-engine=host_debug_unopt
cd $MONOREPO_PATH

Troubleshooting

Please file an issue or email the dart-engprod team with any problems with or questions about using monorepo.

We will update this documentation to address them.

  • flutter commands may download the engine and Dart SDK files for the configured channel, even though they will be using the local engine and its SDK.

Windows

  • On Windows, gclient sync needs to be run in an administrator session, because some installed dependencies create symlinks.