-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Reland "[Android] Add mechanism for setting Android engine flags via Android manifest (take 2)" #182522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Reland "[Android] Add mechanism for setting Android engine flags via Android manifest (take 2)" #182522
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
3d83f89
Refactor FlutterShellArgs
camsim99 be6d388
format
camsim99 d8ae1db
Apply suggestion from @gemini-code-assist[bot]
camsim99 00095d6
allow EnableDartProfiling in release mode
camsim99 16b2dbb
Merge branch 'fix_flutter_shell_args' of github.com:camsim99/flutter …
camsim99 6bd6535
add test for enable-dart-profiling
camsim99 66397ed
[Android] Add mechanism for setting Android engine flags via Android …
camsim99 3c8649f
Merge branch 'fix_flutter_shell_args' into reland_flags
camsim99 fcccb66
add note about deprecation suppressions
camsim99 3733474
reduce loops from 2 to 1
camsim99 9a32c37
add merged-platform-ui-thread
camsim99 09ab372
add internal flags
camsim99 ecb6ea0
add all switch_defs and rename manifest file
camsim99 e546283
add test flag + logs
camsim99 3f6e95a
add renamed markdown file
camsim99 421fb77
loop through flags instead of manifest
camsim99 a2a4908
some good stuff some bad
camsim99 78fe7c7
undo adding unecessary flags
camsim99 ff053d3
add back necessary flags
camsim99 e2f0d28
self review
camsim99 d6d4a07
small fixes + impeller fix
camsim99 f7b0c12
self review
camsim99 8916140
add back flutterloader flags
camsim99 5fca5fd
handle empty and specified boolean values
camsim99 659df01
extra test :)
camsim99 6dc62a7
take matt's suggestion
camsim99 eb9b5e2
Merge remote-tracking branch 'upstream/master' into reland_flags
camsim99 6a1bc5b
remove cache_sksl and clarify command line flag availability
camsim99 45120eb
Merge remote-tracking branch 'upstream/master' into reland_flags
camsim99 06fb8d9
rework warning
camsim99 f3e6ff4
Merge remote-tracking branch 'upstream/master' into reland_flags
camsim99 5c02a63
rework other warning for consistency
camsim99 ff47dad
Merge remote-tracking branch 'upstream/master' into reland_flags
camsim99 97c27e9
refactor hcpp
camsim99 0170936
remove print statement
camsim99 accedba
remove enable-surface-control
camsim99 409df0f
minor hcpp correction
camsim99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| # Setting Flutter Android engine flags | ||
|
|
||
| You can set flags for the Flutter engine on Android in two different ways: | ||
|
|
||
| - From the command line when launching an app with the Flutter tool | ||
| - Via `AndroidManifest.xml` metadata (static, per-build configuration) | ||
|
|
||
| Flags available on Android may be set via the command line **and/or** via | ||
| manifest metadata depending on the flag. See | ||
| `src/flutter/shell/platform/android/io/flutter/embedding/engine/` | ||
| `FlutterEngineFlags.java` for the list of flags that can be set for | ||
| the Android shell, and see `src/flutter/shell/common/switch_defs.h` | ||
| for the list of all supported flags. | ||
|
|
||
| For flags that can be set on the command line and via the manifest, | ||
| see below to determine which method to use. | ||
|
|
||
| ## When to use manifest metadata versus the command line | ||
|
|
||
| Use the manifest when: | ||
|
|
||
| - You want a fixed, reproducible baseline of engine flags | ||
| for your app across all launches. This is ideal for CI and for enforcing a | ||
| consistent configuration for your app. | ||
| - You want to vary flags by build mode or product flavor | ||
| via manifest merging. For example, place metadata in | ||
| `src/debug/AndroidManifest.xml`, `src/profile/AndroidManifest.xml`, and | ||
| `src/release/AndroidManifest.xml` (or per-flavor manifests) to tailor flags | ||
| per variant. | ||
|
|
||
| Use the command line when: | ||
|
|
||
| - You want to quickly experiment with a flag for a single run of your app. | ||
| - You need to override a flag that is already set in the manifest temporarily for debugging | ||
| or testing purposes. | ||
|
|
||
| **Note: If a flag is specified both on the command line and in the manifest, | ||
| the command-line value takes precedence at runtime.** | ||
|
|
||
| See below for details on using each method. | ||
|
|
||
| ## How to set engine flags from the command line | ||
|
|
||
| When you run a standalone Flutter app with the Flutter tool, engine flags | ||
| can be passed directly and are forwarded to the Android engine. Examples: | ||
|
|
||
| ```bash | ||
| flutter run --trace-startup \ | ||
| --enable-software-rendering \ | ||
| --dart-flags="--enable-asserts" | ||
| ``` | ||
|
|
||
| Notes: | ||
|
|
||
| - Flags that take values use the `--flag=value` form (with `=`). The Flutter | ||
| tool forwards them in that form to the Android embedding. | ||
|
|
||
| ## How to set engine flags in the manifest | ||
|
|
||
| All manifest metadata keys must be prefixed with the package name | ||
| `io.flutter.embedding.android` and are suffixed with the metadata name for the | ||
| related command line flag as determined in | ||
| `src/flutter/shell/platform/android/io/flutter/embedding/engine/` | ||
| `FlutterEngineFlags.java`. For example, the `--impeller-lazy-shader-mode=` | ||
| command line flag corresponds to the metadata key | ||
| `io.flutter.embedding.android.ImpellerLazyShaderInitialization`. | ||
|
|
||
| For flags that take values, set the numeric, string, or boolean value (without | ||
| the leading `--flag=` prefix). | ||
|
|
||
| ### Examples | ||
|
|
||
| Set the `--trace-to-file=` flag to `some_file.txt`: | ||
|
|
||
| ```xml | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="com.example.myapp"> | ||
| <application ...> | ||
| <meta-data | ||
| android:name="io.flutter.embedding.android.TraceToFile" | ||
| android:value="some_file.txt"/> | ||
| ... | ||
| </application> | ||
| </manifest> | ||
| ``` | ||
|
|
||
| Set the `--enable-flutter-gpu` flag: | ||
|
|
||
| ```xml | ||
| <meta-data | ||
| android:name="io.flutter.embedding.android.EnableFlutterGPU" | ||
| android:value=true | ||
| /> | ||
| ``` | ||
|
|
||
| For flags that take boolean values, if you omit a value entirely, it | ||
| will be assumed to be true. For example, this is the same as the | ||
| example above: | ||
|
|
||
| ```xml | ||
| <meta-data | ||
| android:name="io.flutter.embedding.android.EnableFlutterGPU" | ||
| /> | ||
| ``` | ||
|
|
||
| ## Release-mode restrictions | ||
|
|
||
| - Some flags are not allowed in release mode. The Android embedding enforces | ||
| this policy (see `src/flutter/shell/platform/android/io/flutter/ | ||
| embedding/engine/FlutterEngineFlags`, which marks allowed flags | ||
| with `allowedInRelease`). If a disallowed flag is set in release, it will | ||
| be ignored. | ||
| - If you need different behavior in release vs debug/profile mode, configure it | ||
| via variant-specific manifests or product flavors. | ||
|
|
||
| ## How to set engine flags dynamically | ||
|
|
||
| As of the writing of this document, setting Flutter shell arguments via an | ||
| Android `Intent` is no longer supported. If you need per-launch or | ||
| runtime-controlled flags in an add-to-app integration, you may do so | ||
| programatically before engine initialization. | ||
|
|
||
| To do that, supply engine arguments directly to a `FlutterEngine` with the | ||
| desired flags from the earliest point you can control in your | ||
| application. For example, if you are writing an add-to-app app that launches | ||
| a `FlutterActivity` or `FlutterFragment`, then you can cache a | ||
| `FlutterEngine` that is initialized with your desired | ||
| engine flags: | ||
|
|
||
| ```kotlin | ||
| // Your native Android application | ||
| class MyApp : Application() { | ||
| override fun onCreate() { | ||
| super.onCreate() | ||
| // Initialize the Flutter engine with desired flags | ||
| val args = arrayOf( | ||
| "--trace-startup", | ||
| "--trace-to-file=some_file.txt", | ||
| "--enable-software-rendering" | ||
| ) | ||
| val flutterEngine = FlutterEngine(this, args) | ||
|
|
||
| // Start executing Dart code in the FlutterEngine | ||
| flutterEngine.dartExecutor.executeDartEntrypoint( | ||
| DartEntrypoint.createDefault() | ||
| ) | ||
|
|
||
| // Store the engine in the cache for later use | ||
| FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Then, your `Activity` can launch a `FlutterActivity` or `FlutterFragment` | ||
| with that cached `FlutterEngine`: | ||
|
|
||
| ```kotlin | ||
| // Start a FlutterActivity using the cached engine... | ||
| val intent = FlutterActivity.withCachedEngine("my_engine_id").build(this) | ||
| startActivity(intent) | ||
|
|
||
| // Or launch a FlutterFragment using the cached engine | ||
| val flutterFragment = FlutterFragment.withCachedEngine("my_engine_id").build() | ||
| supportFragmentManager | ||
| .beginTransaction() | ||
| .add(R.id.fragment_container, flutterFragment, TAG_FLUTTER_FRAGMENT) | ||
| .commit() | ||
| ``` | ||
|
|
||
| For a normal Flutter Android app, you can create and initialize a `FlutterEngine` | ||
| with your desired flags the same as in the example above, then override | ||
| `provideFlutterEngine` in your app's `FlutterActivity` to provide the | ||
| configured `FlutterEngine`. For example: | ||
|
|
||
| ```kotlin | ||
| // Your Flutter Android application | ||
| class MyApplication : FlutterApplication() { | ||
| override fun onCreate() { | ||
| super.onCreate() | ||
|
|
||
| val args = arrayOf( | ||
| "--trace-startup", | ||
| "--trace-to-file=some_file.txt", | ||
| "--enable-software-rendering" | ||
| ) | ||
| val flutterEngine = FlutterEngine(this, args) | ||
| flutterEngine.dartExecutor.executeDartEntrypoint( | ||
| DartExecutor.DartEntrypoint.createDefault() | ||
| ) | ||
| FlutterEngineCache | ||
| .getInstance() | ||
| .put(MY_ENGINE_ID, flutterEngine) | ||
| } | ||
| } | ||
|
camsim99 marked this conversation as resolved.
|
||
|
|
||
| // Your Flutter Android Activity | ||
| class MainActivity: FlutterActivity() { | ||
| override fun provideFlutterEngine(context: Context): FlutterEngine? { | ||
| return FlutterEngineCache | ||
| .getInstance() | ||
| .get(MyApplication.MY_ENGINE_ID) | ||
| } | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.