Skip to content

Commit cabb3f6

Browse files
authored
[Android] Inject route into manifest when specified on the command line in release mode (flutter#191328)
Follow up to flutter#190222. Routes `route` through the manifest when used in release mode to follow the precedent of other Android engine flags as part of the work towards flutter#190461. Separate PR created for clarity; `route` is parsed apart from most other engine flags. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **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 [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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
1 parent c3dc059 commit cabb3f6

10 files changed

Lines changed: 586 additions & 74 deletions

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.EXTRA_DESTROY_ENGINE_WITH_ACTIVITY;
1919
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.EXTRA_ENABLE_STATE_RESTORATION;
2020
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.EXTRA_INITIAL_ROUTE;
21-
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.INITIAL_ROUTE_META_DATA_KEY;
2221
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.NORMAL_THEME_META_DATA_KEY;
2322
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.deepLinkEnabled;
2423

@@ -1165,39 +1164,36 @@ public String getDartEntrypointLibraryUri() {
11651164
/**
11661165
* The initial route that a Flutter app will render upon loading and executing its Dart code.
11671166
*
1168-
* <p>This preference can be controlled with 2 methods:
1167+
* <p>This preference can be controlled with 3 methods:
11691168
*
11701169
* <ol>
1171-
* <li>Pass a boolean as {@link FlutterActivityLaunchConfigs#EXTRA_INITIAL_ROUTE} with the
1170+
* <li>Pass a String as {@link FlutterActivityLaunchConfigs#EXTRA_INITIAL_ROUTE} with the
11721171
* launching {@code Intent}, or
1172+
* <li>Set a {@code --route} command line flag via engine shell arguments in the application
1173+
* manifest meta-data (see {@link
1174+
* FlutterActivityLaunchConfigs#getInitialRouteFromManifest(Context)}), or
11731175
* <li>Set a {@code <meta-data>} called {@link
11741176
* FlutterActivityLaunchConfigs#INITIAL_ROUTE_META_DATA_KEY} for this {@code Activity} in
11751177
* the Android manifest.
11761178
* </ol>
11771179
*
1178-
* If both preferences are set, the {@code Intent} preference takes priority.
1180+
* If multiple preferences are set, the {@code Intent} preference takes highest priority, followed
1181+
* by the manifest engine arguments, followed by the {@code Activity} {@code <meta-data>}.
11791182
*
1180-
* <p>The reason that a {@code <meta-data>} preference is supported is because this {@code
1183+
* <p>The reason that {@code <meta-data>} preferences are supported is because this {@code
11811184
* Activity} might be the very first {@code Activity} launched, which means the developer won't
11821185
* have control over the incoming {@code Intent}.
11831186
*
11841187
* <p>Subclasses may override this method to directly control the initial route.
11851188
*
1186-
* <p>If this method returns null and the {@code shouldHandleDeeplinking} returns true, the
1187-
* initial route is derived from the {@code Intent} through the Intent.getData() instead.
1189+
* <p>If this method returns null and {@link #shouldHandleDeeplinking()} returns true, the initial
1190+
* route is derived from the {@code Intent} through the Intent.getData() instead.
11881191
*/
11891192
public String getInitialRoute() {
1190-
if (getIntent().hasExtra(EXTRA_INITIAL_ROUTE)) {
1191-
return getIntent().getStringExtra(EXTRA_INITIAL_ROUTE);
1192-
}
1193-
11941193
try {
1195-
Bundle metaData = getMetaData();
1196-
String desiredInitialRoute =
1197-
metaData != null ? metaData.getString(INITIAL_ROUTE_META_DATA_KEY) : null;
1198-
return desiredInitialRoute;
1194+
return FlutterActivityLaunchConfigs.getInitialRoute(getIntent(), this, getMetaData());
11991195
} catch (PackageManager.NameNotFoundException e) {
1200-
return null;
1196+
return FlutterActivityLaunchConfigs.getInitialRoute(getIntent(), this, null);
12011197
}
12021198
}
12031199

@@ -1313,7 +1309,7 @@ protected FlutterEngine getFlutterEngine() {
13131309
protected Bundle getMetaData() throws PackageManager.NameNotFoundException {
13141310
ActivityInfo activityInfo =
13151311
getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
1316-
return activityInfo.metaData;
1312+
return activityInfo != null ? activityInfo.metaData : null;
13171313
}
13181314

13191315
@Nullable

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterActivityLaunchConfigs.java‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
package io.flutter.embedding.android;
66

7+
import android.content.Context;
8+
import android.content.Intent;
9+
import android.content.pm.ApplicationInfo;
10+
import android.content.pm.PackageManager;
711
import android.os.Bundle;
12+
import androidx.annotation.NonNull;
13+
import androidx.annotation.Nullable;
14+
import io.flutter.embedding.engine.loader.FlutterLoader;
15+
import java.util.List;
816

917
/** Collection of Flutter launch configuration options. */
1018
// This class is public so that Flutter app developers can reference
@@ -62,5 +70,57 @@ public static boolean deepLinkEnabled(Bundle metaData) {
6270
}
6371
}
6472

73+
/**
74+
* Extracts the initial route from the application's manifest shell arguments, if present.
75+
*
76+
* @param context The application context.
77+
* @return The initial route if defined in the manifest shell arguments, otherwise null.
78+
*/
79+
@Nullable
80+
public static String getInitialRouteFromManifest(@NonNull Context context) {
81+
try {
82+
ApplicationInfo appInfo =
83+
context
84+
.getPackageManager()
85+
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
86+
List<String> shellArgs = FlutterLoader.getManifestEngineShellArgs(appInfo.metaData);
87+
if (shellArgs != null) {
88+
final String routeFlag = "--route=";
89+
for (String arg : shellArgs) {
90+
if (arg != null && arg.startsWith(routeFlag)) {
91+
return arg.substring(routeFlag.length());
92+
}
93+
}
94+
}
95+
} catch (PackageManager.NameNotFoundException e) {
96+
// Ignore
97+
}
98+
return null;
99+
}
100+
101+
/**
102+
* Resolves the initial route for an activity based on Intent extras, manifest engine arguments,
103+
* or Activity meta-data.
104+
*
105+
* @param intent The launching intent.
106+
* @param context The application context.
107+
* @param metaData The Activity's metadata bundle, or null.
108+
* @return The initial route if defined, otherwise null.
109+
*/
110+
@Nullable
111+
public static String getInitialRoute(
112+
@NonNull Intent intent, @NonNull Context context, @Nullable Bundle metaData) {
113+
if (intent.hasExtra(EXTRA_INITIAL_ROUTE)) {
114+
return intent.getStringExtra(EXTRA_INITIAL_ROUTE);
115+
}
116+
117+
String routeFromManifest = getInitialRouteFromManifest(context);
118+
if (routeFromManifest != null) {
119+
return routeFromManifest;
120+
}
121+
122+
return metaData != null ? metaData.getString(INITIAL_ROUTE_META_DATA_KEY) : null;
123+
}
124+
65125
private FlutterActivityLaunchConfigs() {}
66126
}

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragmentActivity.java‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.EXTRA_DART_ENTRYPOINT_ARGS;
1818
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.EXTRA_DESTROY_ENGINE_WITH_ACTIVITY;
1919
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.EXTRA_INITIAL_ROUTE;
20-
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.INITIAL_ROUTE_META_DATA_KEY;
2120
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.NORMAL_THEME_META_DATA_KEY;
2221
import static io.flutter.embedding.android.FlutterActivityLaunchConfigs.deepLinkEnabled;
2322

@@ -809,7 +808,7 @@ protected String getAppBundlePath() {
809808
protected Bundle getMetaData() throws PackageManager.NameNotFoundException {
810809
ActivityInfo activityInfo =
811810
getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
812-
return activityInfo.metaData;
811+
return activityInfo != null ? activityInfo.metaData : null;
813812
}
814813

815814
/**
@@ -874,39 +873,36 @@ public String getDartEntrypointLibraryUri() {
874873
/**
875874
* The initial route that a Flutter app will render upon loading and executing its Dart code.
876875
*
877-
* <p>This preference can be controlled with 2 methods:
876+
* <p>This preference can be controlled with 3 methods:
878877
*
879878
* <ol>
880-
* <li>Pass a boolean as {@link FlutterActivityLaunchConfigs#EXTRA_INITIAL_ROUTE} with the
879+
* <li>Pass a String as {@link FlutterActivityLaunchConfigs#EXTRA_INITIAL_ROUTE} with the
881880
* launching {@code Intent}, or
881+
* <li>Set a {@code --route} command line flag via engine shell arguments in the application
882+
* manifest meta-data (see {@link
883+
* FlutterActivityLaunchConfigs#getInitialRouteFromManifest(Context)}), or
882884
* <li>Set a {@code <meta-data>} called {@link
883885
* FlutterActivityLaunchConfigs#INITIAL_ROUTE_META_DATA_KEY} for this {@code Activity} in
884886
* the Android manifest.
885887
* </ol>
886888
*
887-
* If both preferences are set, the {@code Intent} preference takes priority.
889+
* If multiple preferences are set, the {@code Intent} preference takes highest priority, followed
890+
* by the manifest engine arguments, followed by the {@code Activity} {@code <meta-data>}.
888891
*
889-
* <p>The reason that a {@code <meta-data>} preference is supported is because this {@code
892+
* <p>The reason that {@code <meta-data>} preferences are supported is because this {@code
890893
* Activity} might be the very first {@code Activity} launched, which means the developer won't
891894
* have control over the incoming {@code Intent}.
892895
*
893896
* <p>Subclasses may override this method to directly control the initial route.
894897
*
895-
* <p>If this method returns null and the {@code shouldHandleDeeplinking} returns true, the
896-
* initial route is derived from the {@code Intent} through the Intent.getData() instead.
898+
* <p>If this method returns null and {@link #shouldHandleDeeplinking()} returns true, the initial
899+
* route is derived from the {@code Intent} through the Intent.getData() instead.
897900
*/
898901
protected String getInitialRoute() {
899-
if (getIntent().hasExtra(EXTRA_INITIAL_ROUTE)) {
900-
return getIntent().getStringExtra(EXTRA_INITIAL_ROUTE);
901-
}
902-
903902
try {
904-
Bundle metaData = getMetaData();
905-
String desiredInitialRoute =
906-
metaData != null ? metaData.getString(INITIAL_ROUTE_META_DATA_KEY) : null;
907-
return desiredInitialRoute;
903+
return FlutterActivityLaunchConfigs.getInitialRoute(getIntent(), this, getMetaData());
908904
} catch (PackageManager.NameNotFoundException e) {
909-
return null;
905+
return FlutterActivityLaunchConfigs.getInitialRoute(getIntent(), this, null);
910906
}
911907
}
912908

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java‎

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,40 @@ public class FlutterLoader {
6161
private static final String ANDROID_ENGINE_SHELL_ARGS_KEY =
6262
"io.flutter.app.androidEngineShellArgs";
6363

64+
/**
65+
* Reads the {@code io.flutter.app.androidEngineShellArgs} metadata from the application manifest
66+
* and parses it into a List of Strings.
67+
*
68+
* @param applicationMetaData The application's metadata bundle.
69+
* @return A List of shell arguments if present and valid, otherwise null.
70+
*/
71+
@Nullable
72+
public static List<String> getManifestEngineShellArgs(@Nullable Bundle applicationMetaData) {
73+
if (applicationMetaData == null) {
74+
return null;
75+
}
76+
String androidEngineShellArgsValue =
77+
applicationMetaData.getString(ANDROID_ENGINE_SHELL_ARGS_KEY);
78+
if (androidEngineShellArgsValue != null && !androidEngineShellArgsValue.isEmpty()) {
79+
try {
80+
JSONArray shellArgsJson = new JSONArray(androidEngineShellArgsValue);
81+
List<String> parsedArgs = new ArrayList<>();
82+
for (int i = 0; i < shellArgsJson.length(); i++) {
83+
parsedArgs.add(shellArgsJson.getString(i));
84+
}
85+
return parsedArgs;
86+
} catch (JSONException e) {
87+
Log.e(
88+
TAG,
89+
"Exception parsing shell arguments "
90+
+ androidEngineShellArgsValue
91+
+ " from manifest: "
92+
+ e);
93+
}
94+
}
95+
return null;
96+
}
97+
6498
private static FlutterLoader instance;
6599

66100
private boolean enableSoftwareRendering = false;
@@ -419,48 +453,35 @@ void ensureInitializationComplete(
419453
// the manifest. These settings will take precedent over any flag configurations
420454
// specified by appplication manifest metadata.
421455
if (isRelease) {
422-
String androidEngineShellArgsValue =
423-
applicationMetaData.getString(ANDROID_ENGINE_SHELL_ARGS_KEY);
424-
if (androidEngineShellArgsValue != null && !androidEngineShellArgsValue.isEmpty()) {
425-
try {
426-
JSONArray shellArgsJson = new JSONArray(androidEngineShellArgsValue);
427-
for (int i = 0; i < shellArgsJson.length(); i++) {
428-
String arg = shellArgsJson.getString(i);
429-
430-
FlutterEngineFlags.Flag flag = FlutterEngineFlags.getFlagByEngineArgument(arg);
431-
if (flag == null) {
432-
// TODO(camsim99): Reject unknown flags specified on the command line:
433-
// https://github.com/flutter/flutter/issues/182557.
434-
shellArgs.add(arg);
435-
continue;
436-
} else if (flag.equals(FlutterEngineFlags.TEST_FLAG)) {
437-
Log.w(
438-
TAG,
439-
"For testing purposes only: test flag specified on the command line was loaded by the FlutterLoader.");
440-
continue;
441-
} else if (!flag.allowedInRelease) {
442-
Log.e(
443-
TAG,
444-
"Flag " + arg + " is not allowed in release builds and will be ignored.");
445-
continue;
446-
} else if (flag.equals(FlutterEngineFlags.AOT_SHARED_LIBRARY_NAME)
447-
|| flag.equals(FlutterEngineFlags.DEPRECATED_AOT_SHARED_LIBRARY_NAME)) {
448-
// Perform security check for path containing application's compiled Dart
449-
// code and potentially user-provided compiled native code.
450-
String aotSharedLibraryPath = arg.substring(flag.engineArgument.length());
451-
maybeAddAotSharedLibraryNameArg(
452-
applicationContext, aotSharedLibraryPath, shellArgs);
453-
continue;
454-
}
456+
List<String> parsedArgs = getManifestEngineShellArgs(applicationMetaData);
457+
if (parsedArgs != null) {
458+
for (String arg : parsedArgs) {
459+
FlutterEngineFlags.Flag flag = FlutterEngineFlags.getFlagByEngineArgument(arg);
460+
if (flag == null) {
461+
// TODO(camsim99): Reject unknown flags specified on the command line:
462+
// https://github.com/flutter/flutter/issues/182557.
455463
shellArgs.add(arg);
464+
continue;
465+
} else if (flag.equals(FlutterEngineFlags.TEST_FLAG)) {
466+
Log.w(
467+
TAG,
468+
"For testing purposes only: test flag specified on the command line was loaded by the FlutterLoader.");
469+
continue;
470+
} else if (!flag.allowedInRelease) {
471+
Log.e(
472+
TAG, "Flag " + arg + " is not allowed in release builds and will be ignored.");
473+
continue;
474+
} else if (flag.equals(FlutterEngineFlags.AOT_SHARED_LIBRARY_NAME)
475+
|| flag.equals(FlutterEngineFlags.DEPRECATED_AOT_SHARED_LIBRARY_NAME)) {
476+
// Perform security check for path containing application's compiled Dart
477+
// code and potentially user-provided compiled native code.
478+
String aotSharedLibraryPath = arg.substring(flag.engineArgument.length());
479+
maybeAddAotSharedLibraryNameArg(
480+
applicationContext, aotSharedLibraryPath, shellArgs);
481+
continue;
456482
}
457-
} catch (JSONException j) {
458-
Log.e(
459-
TAG,
460-
"Exception parsing shell arguments "
461-
+ androidEngineShellArgsValue
462-
+ " from manifest: "
463-
+ j);
483+
484+
shellArgs.add(arg);
464485
}
465486
}
466487
}

0 commit comments

Comments
 (0)