Skip to content

Commit be36ba6

Browse files
authored
Generalize runFromBundle to support multiple bundlePaths (flutter#7151)
1 parent da9985a commit be36ba6

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

shell/platform/android/io/flutter/view/FlutterNativeView.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,20 @@ public void assertAttached() {
7676
}
7777

7878
public void runFromBundle(FlutterRunArguments args) {
79-
if (args.bundlePath == null) {
80-
throw new AssertionError("A bundlePath must be specified");
79+
if (args.bundlePath == null && args.bundlePaths.length == 0) {
80+
throw new AssertionError("Either bundlePath or bundlePaths must be specified");
81+
} else if ((args.bundlePath != null || args.defaultPath != null) &&
82+
args.bundlePaths.length != 0) {
83+
throw new AssertionError("Can't specify both bundlePath and bundlePaths");
8184
} else if (args.entrypoint == null) {
82-
throw new AssertionError("An entrypoint must be specified");
85+
throw new AssertionError("An entrypoint must be specified");
86+
}
87+
if (args.bundlePaths.length != 0) {
88+
runFromBundleInternal(args.bundlePaths, args.entrypoint, args.libraryPath);
89+
} else {
90+
runFromBundleInternal(new String[] {args.bundlePath, args.defaultPath},
91+
args.entrypoint, args.libraryPath);
8392
}
84-
runFromBundleInternal(args.bundlePath, args.entrypoint, args.libraryPath, args.defaultPath);
8593
}
8694

8795
/**
@@ -92,17 +100,17 @@ public void runFromBundle(FlutterRunArguments args) {
92100
@Deprecated
93101
public void runFromBundle(String bundlePath, String defaultPath, String entrypoint,
94102
boolean reuseRuntimeController) {
95-
runFromBundleInternal(bundlePath, entrypoint, null, defaultPath);
103+
runFromBundleInternal(new String[] {bundlePath, defaultPath}, entrypoint, null);
96104
}
97105

98-
private void runFromBundleInternal(String bundlePath, String entrypoint,
99-
String libraryPath, String defaultPath) {
106+
private void runFromBundleInternal(String[] bundlePaths, String entrypoint,
107+
String libraryPath) {
100108
assertAttached();
101109
if (applicationIsRunning)
102110
throw new AssertionError(
103111
"This Flutter engine instance is already running an application");
104-
nativeRunBundleAndSnapshotFromLibrary(mNativePlatformView, bundlePath,
105-
defaultPath, entrypoint, libraryPath, mContext.getResources().getAssets());
112+
nativeRunBundleAndSnapshotFromLibrary(mNativePlatformView, bundlePaths,
113+
entrypoint, libraryPath, mContext.getResources().getAssets());
106114

107115
applicationIsRunning = true;
108116
}
@@ -240,9 +248,8 @@ private void onPreEngineRestart() {
240248
private static native void nativeDetach(long nativePlatformViewAndroid);
241249

242250
private static native void nativeRunBundleAndSnapshotFromLibrary(
243-
long nativePlatformViewAndroid, String bundlePath,
244-
String defaultPath, String entrypoint, String libraryUrl,
245-
AssetManager manager);
251+
long nativePlatformViewAndroid, String[] bundlePaths,
252+
String entrypoint, String libraryUrl, AssetManager manager);
246253

247254
private static native String nativeGetObservatoryUri();
248255

shell/platform/android/io/flutter/view/FlutterRunArguments.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* the first time.
1010
*/
1111
public class FlutterRunArguments {
12+
public String[] bundlePaths;
1213
public String bundlePath;
1314
public String entrypoint;
1415
public String libraryPath;

shell/platform/android/platform_view_android_jni.cc

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,17 @@ std::unique_ptr<IsolateConfiguration> CreateIsolateConfiguration(
236236
static void RunBundleAndSnapshotFromLibrary(JNIEnv* env,
237237
jobject jcaller,
238238
jlong shell_holder,
239-
jstring jbundlepath,
240-
jstring jdefaultPath,
239+
jobjectArray jbundlepaths,
241240
jstring jEntrypoint,
242241
jstring jLibraryUrl,
243242
jobject jAssetManager) {
244243
auto asset_manager = std::make_shared<blink::AssetManager>();
244+
for (const auto& bundlepath :
245+
fml::jni::StringArrayToVector(env, jbundlepaths)) {
246+
if (bundlepath.empty()) {
247+
continue;
248+
}
245249

246-
const auto bundlepath = fml::jni::JavaStringToString(env, jbundlepath);
247-
if (bundlepath.size() > 0) {
248250
// If we got a bundle path, attempt to use that as a directory asset
249251
// bundle or a zip asset bundle.
250252
const auto file_ext_index = bundlepath.rfind(".");
@@ -255,30 +257,23 @@ static void RunBundleAndSnapshotFromLibrary(JNIEnv* env,
255257
asset_manager->PushBack(
256258
std::make_unique<blink::DirectoryAssetBundle>(fml::OpenDirectory(
257259
bundlepath.c_str(), false, fml::FilePermission::kRead)));
258-
}
259260

260-
// Use the last path component of the bundle path to determine the
261-
// directory in the APK assets.
262-
const auto last_slash_index = bundlepath.rfind("/", bundlepath.size());
263-
if (last_slash_index != std::string::npos) {
264-
auto apk_asset_dir = bundlepath.substr(
265-
last_slash_index + 1, bundlepath.size() - last_slash_index);
266-
267-
asset_manager->PushBack(std::make_unique<blink::APKAssetProvider>(
268-
env, // jni environment
269-
jAssetManager, // asset manager
270-
std::move(apk_asset_dir)) // apk asset dir
271-
);
261+
// Use the last path component of the bundle path to determine the
262+
// directory in the APK assets.
263+
const auto last_slash_index = bundlepath.rfind("/", bundlepath.size());
264+
if (last_slash_index != std::string::npos) {
265+
auto apk_asset_dir = bundlepath.substr(
266+
last_slash_index + 1, bundlepath.size() - last_slash_index);
267+
268+
asset_manager->PushBack(std::make_unique<blink::APKAssetProvider>(
269+
env, // jni environment
270+
jAssetManager, // asset manager
271+
std::move(apk_asset_dir)) // apk asset dir
272+
);
273+
}
272274
}
273275
}
274276

275-
const auto defaultpath = fml::jni::JavaStringToString(env, jdefaultPath);
276-
if (defaultpath.size() > 0) {
277-
asset_manager->PushBack(
278-
std::make_unique<blink::DirectoryAssetBundle>(fml::OpenDirectory(
279-
defaultpath.c_str(), false, fml::FilePermission::kRead)));
280-
}
281-
282277
auto isolate_configuration = CreateIsolateConfiguration(*asset_manager);
283278
if (!isolate_configuration) {
284279
FML_DLOG(ERROR)
@@ -591,9 +586,8 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
591586
},
592587
{
593588
.name = "nativeRunBundleAndSnapshotFromLibrary",
594-
.signature =
595-
"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;"
596-
"Ljava/lang/String;Landroid/content/res/AssetManager;)V",
589+
.signature = "(J[Ljava/lang/String;Ljava/lang/String;"
590+
"Ljava/lang/String;Landroid/content/res/AssetManager;)V",
597591
.fnPtr =
598592
reinterpret_cast<void*>(&shell::RunBundleAndSnapshotFromLibrary),
599593
},

0 commit comments

Comments
 (0)