From 194bf111b6c05e65fe04e9b8b187f3987ebd040f Mon Sep 17 00:00:00 2001 From: Stanislav Baranov Date: Mon, 10 Dec 2018 12:19:22 -0800 Subject: [PATCH] Generalize runFromBundle to support multiple bundlePaths (flutter/engine#7151) --- .../io/flutter/view/FlutterNativeView.java | 31 +++++++----- .../io/flutter/view/FlutterRunArguments.java | 1 + .../android/platform_view_android_jni.cc | 50 ++++++++----------- 3 files changed, 42 insertions(+), 40 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java index bb5e132d46..bf49b43c2a 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterNativeView.java @@ -76,12 +76,20 @@ public class FlutterNativeView implements BinaryMessenger { } public void runFromBundle(FlutterRunArguments args) { - if (args.bundlePath == null) { - throw new AssertionError("A bundlePath must be specified"); + if (args.bundlePath == null && args.bundlePaths.length == 0) { + throw new AssertionError("Either bundlePath or bundlePaths must be specified"); + } else if ((args.bundlePath != null || args.defaultPath != null) && + args.bundlePaths.length != 0) { + throw new AssertionError("Can't specify both bundlePath and bundlePaths"); } else if (args.entrypoint == null) { - throw new AssertionError("An entrypoint must be specified"); + throw new AssertionError("An entrypoint must be specified"); + } + if (args.bundlePaths.length != 0) { + runFromBundleInternal(args.bundlePaths, args.entrypoint, args.libraryPath); + } else { + runFromBundleInternal(new String[] {args.bundlePath, args.defaultPath}, + args.entrypoint, args.libraryPath); } - runFromBundleInternal(args.bundlePath, args.entrypoint, args.libraryPath, args.defaultPath); } /** @@ -92,17 +100,17 @@ public class FlutterNativeView implements BinaryMessenger { @Deprecated public void runFromBundle(String bundlePath, String defaultPath, String entrypoint, boolean reuseRuntimeController) { - runFromBundleInternal(bundlePath, entrypoint, null, defaultPath); + runFromBundleInternal(new String[] {bundlePath, defaultPath}, entrypoint, null); } - private void runFromBundleInternal(String bundlePath, String entrypoint, - String libraryPath, String defaultPath) { + private void runFromBundleInternal(String[] bundlePaths, String entrypoint, + String libraryPath) { assertAttached(); if (applicationIsRunning) throw new AssertionError( "This Flutter engine instance is already running an application"); - nativeRunBundleAndSnapshotFromLibrary(mNativePlatformView, bundlePath, - defaultPath, entrypoint, libraryPath, mContext.getResources().getAssets()); + nativeRunBundleAndSnapshotFromLibrary(mNativePlatformView, bundlePaths, + entrypoint, libraryPath, mContext.getResources().getAssets()); applicationIsRunning = true; } @@ -240,9 +248,8 @@ public class FlutterNativeView implements BinaryMessenger { private static native void nativeDetach(long nativePlatformViewAndroid); private static native void nativeRunBundleAndSnapshotFromLibrary( - long nativePlatformViewAndroid, String bundlePath, - String defaultPath, String entrypoint, String libraryUrl, - AssetManager manager); + long nativePlatformViewAndroid, String[] bundlePaths, + String entrypoint, String libraryUrl, AssetManager manager); private static native String nativeGetObservatoryUri(); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterRunArguments.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterRunArguments.java index 04e13afbb5..9440933031 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterRunArguments.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterRunArguments.java @@ -9,6 +9,7 @@ package io.flutter.view; * the first time. */ public class FlutterRunArguments { + public String[] bundlePaths; public String bundlePath; public String entrypoint; public String libraryPath; diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc index be42414b6c..69e40dd9e1 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni.cc @@ -236,15 +236,17 @@ std::unique_ptr CreateIsolateConfiguration( static void RunBundleAndSnapshotFromLibrary(JNIEnv* env, jobject jcaller, jlong shell_holder, - jstring jbundlepath, - jstring jdefaultPath, + jobjectArray jbundlepaths, jstring jEntrypoint, jstring jLibraryUrl, jobject jAssetManager) { auto asset_manager = std::make_shared(); + for (const auto& bundlepath : + fml::jni::StringArrayToVector(env, jbundlepaths)) { + if (bundlepath.empty()) { + continue; + } - const auto bundlepath = fml::jni::JavaStringToString(env, jbundlepath); - if (bundlepath.size() > 0) { // If we got a bundle path, attempt to use that as a directory asset // bundle or a zip asset bundle. const auto file_ext_index = bundlepath.rfind("."); @@ -255,28 +257,21 @@ static void RunBundleAndSnapshotFromLibrary(JNIEnv* env, asset_manager->PushBack( std::make_unique(fml::OpenDirectory( bundlepath.c_str(), false, fml::FilePermission::kRead))); + + // Use the last path component of the bundle path to determine the + // directory in the APK assets. + const auto last_slash_index = bundlepath.rfind("/", bundlepath.size()); + if (last_slash_index != std::string::npos) { + auto apk_asset_dir = bundlepath.substr( + last_slash_index + 1, bundlepath.size() - last_slash_index); + + asset_manager->PushBack(std::make_unique( + env, // jni environment + jAssetManager, // asset manager + std::move(apk_asset_dir)) // apk asset dir + ); + } } - - // Use the last path component of the bundle path to determine the - // directory in the APK assets. - const auto last_slash_index = bundlepath.rfind("/", bundlepath.size()); - if (last_slash_index != std::string::npos) { - auto apk_asset_dir = bundlepath.substr( - last_slash_index + 1, bundlepath.size() - last_slash_index); - - asset_manager->PushBack(std::make_unique( - env, // jni environment - jAssetManager, // asset manager - std::move(apk_asset_dir)) // apk asset dir - ); - } - } - - const auto defaultpath = fml::jni::JavaStringToString(env, jdefaultPath); - if (defaultpath.size() > 0) { - asset_manager->PushBack( - std::make_unique(fml::OpenDirectory( - defaultpath.c_str(), false, fml::FilePermission::kRead))); } auto isolate_configuration = CreateIsolateConfiguration(*asset_manager); @@ -591,9 +586,8 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { }, { .name = "nativeRunBundleAndSnapshotFromLibrary", - .signature = - "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;" - "Ljava/lang/String;Landroid/content/res/AssetManager;)V", + .signature = "(J[Ljava/lang/String;Ljava/lang/String;" + "Ljava/lang/String;Landroid/content/res/AssetManager;)V", .fnPtr = reinterpret_cast(&shell::RunBundleAndSnapshotFromLibrary), },