Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b9255eb

Browse files
committed
One direction works
1 parent fc16954 commit b9255eb

File tree

9 files changed

+133
-55
lines changed

9 files changed

+133
-55
lines changed

shell/common/platform_view.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,9 @@ Dart_Handle PlatformView::OnDartLoadLibrary(intptr_t loading_unit_id) {
157157
return Dart_Null();
158158
}
159159

160+
Dart_Handle PlatformView::CompleteDartLoadLibrary(intptr_t loading_unit_id,
161+
std::string lib_name) {
162+
return Dart_Null();
163+
}
164+
160165
} // namespace flutter

shell/common/platform_view.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ class PlatformView {
586586

587587
virtual Dart_Handle OnDartLoadLibrary(intptr_t loading_unit_id);
588588

589+
virtual Dart_Handle CompleteDartLoadLibrary(intptr_t loading_unit_id,
590+
std::string lib_name);
591+
589592
protected:
590593
PlatformView::Delegate& delegate_;
591594
const TaskRunners task_runners_;

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,19 +972,26 @@ public void downloadDynamicFeature(int loadingUnitId) {
972972
// This should be called for every loading unit to be loaded into the dart isolate.
973973
@UiThread
974974
public void loadDartLibrary(
975-
@NonNull String bundlePath,
975+
int loadingUnitId,
976+
@NonNull String libPath,
977+
@NonNull String libName,
976978
@NonNull AssetManager assetManager) {
977979
ensureRunningOnMainThread();
978980
ensureAttachedToNative();
979981
nativeLoadDartLibrary(
980982
nativePlatformViewId,
981-
bundlePath,
983+
loadingUnitId,
984+
libPath,
985+
libName,
982986
assetManager);
987+
Log.e("flutter", "loadDartLibrary FlutterJNI");
983988
}
984989
private native void nativeLoadDartLibrary(
985990
long nativePlatformViewId,
986-
@NonNull String bundlePath,
987-
@NonNull AssetManager manager);
991+
int loadingUnitId,
992+
@NonNull String libPath,
993+
@NonNull String libName,
994+
@NonNull AssetManager assetManager);
988995
// @SuppressWarnings("unused")
989996
// @UiThread
990997
// public void loadDartLibrary(@NonNull String libPath) {

shell/platform/android/io/flutter/embedding/engine/dynamicfeatures/PlayStoreDynamicFeatureManager.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.android.play.core.splitinstall.model.SplitInstallErrorCode;
2929
import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus;
3030
import io.flutter.embedding.engine.FlutterJNI;
31-
import io.flutter.embedding.engine.loader.FlutterApplicationInfo;
3231
import java.util.HashMap;
3332
import java.util.Map;
3433

@@ -58,9 +57,9 @@ public void onStateUpdate(SplitInstallSessionState state) {
5857
}
5958
case SplitInstallSessionStatus.INSTALLED: {
6059
Log.e("flutter", "Module \"" + sessionIdToName.get(state.sessionId()) + "\" (sessionId " + state.sessionId() + ") installed successfully.");
60+
extractModule(sessionIdToName.get(state.sessionId()), sessionIdToLoadingUnitId.get(state.sessionId()));
6161
sessionIdToName.remove(state.sessionId());
6262
sessionIdToLoadingUnitId.remove(state.sessionId());
63-
extractModule(sessionIdToName.get(state.sessionId()), sessionIdToLoadingUnitId.get(state.sessionId()));
6463
break;
6564
}
6665
case SplitInstallSessionStatus.CANCELED: {
@@ -105,10 +104,16 @@ public PlayStoreDynamicFeatureManager(Context context, FlutterJNI flutterJNI) {
105104
listener = new FeatureInstallStateUpdatedListener();
106105
splitInstallManager.registerListener(listener);
107106
sessionIdToName = new HashMap();
107+
sessionIdToLoadingUnitId = new HashMap();
108108
}
109109

110110
public void downloadModule(@NonNull String moduleName, int loadingUnitId) {
111111
Log.e("flutter", "INSTALLING MODULE " + moduleName);
112+
if (splitInstallManager.getInstalledModules().contains(moduleName)) {
113+
loadDartModules(moduleName, loadingUnitId);
114+
return;
115+
}
116+
112117
SplitInstallRequest request =
113118
SplitInstallRequest
114119
.newBuilder()
@@ -152,7 +157,6 @@ public void extractModule(@NonNull String moduleName, int loadingUnitId) {
152157

153158
try {
154159
context = context.createPackageContext(context.getPackageName(), 0);
155-
AssetManager am = context.getAssets();
156160
// We only load dart shared lib for the loading unit id requested. Other loading units in the
157161
// dynamic feature module are not loaded, but can be loaded by calling again with their loading
158162
// unit id.
@@ -173,16 +177,28 @@ public void loadDartModules(@NonNull String moduleName, int loadingUnitId) {
173177
if (android.os.Build.VERSION.SDK_INT >= 23) {
174178
clearTextPermitted = NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted();
175179
}
176-
FlutterApplicationInfo flutterApplicationInfo = new FlutterApplicationInfo(
177-
"app.so-" + loadingUnitId + ".part.so", // AOT_SHARED_LIBRARY_NAME
178-
null, // VM_SNAPSHOT_DATA_KEY
179-
null, // ISOLATE_SNAPSHOT_DATA_KEY
180-
null, // FLUTTER_ASSETS_DIR_KEY
181-
null, // Network policy
182-
appInfo.nativeLibraryDir,
183-
clearTextPermitted);
180+
181+
String aotSharedLibraryName = "app.so-" + loadingUnitId + ".part.so";
182+
String nativeLibraryDir = appInfo.nativeLibraryDir;
183+
// FlutterApplicationInfo flutterApplicationInfo = new FlutterApplicationInfo(
184+
// "app.so-" + loadingUnitId + ".part.so", // AOT_SHARED_LIBRARY_NAME
185+
// null, // VM_SNAPSHOT_DATA_KEY
186+
// null, // ISOLATE_SNAPSHOT_DATA_KEY
187+
// null, // FLUTTER_ASSETS_DIR_KEY
188+
// null, // Network policy
189+
// appInfo.nativeLibraryDir,
190+
// clearTextPermitted);
191+
// final String aotSharedLibraryName;
192+
// final String vmSnapshotData;
193+
// final String isolateSnapshotData;
194+
// final String flutterAssetsDir;
195+
// final String domainNetworkPolicy;
196+
// final String nativeLibraryDir;
197+
// final boolean clearTextPermitted;
198+
AssetManager assetManager = context.getAssets();
184199

185200
// find one or more .so file and initiate callbacks from https://github.com/flutter/flutter/issues/62229
201+
flutterJNI.loadDartLibrary(loadingUnitId, nativeLibraryDir, aotSharedLibraryName, assetManager);
186202
}
187203

188204
@NonNull

shell/platform/android/jni/platform_view_android_jni.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "flutter/flow/embedded_views.h"
1212
#include "flutter/lib/ui/window/platform_message.h"
1313
#include "flutter/shell/platform/android/surface/android_native_window.h"
14-
#include "third_party/dart/runtime/include/dart_api.h"
1514
#include "third_party/skia/include/core/SkMatrix.h"
1615

1716
#if OS_ANDROID
@@ -197,8 +196,7 @@ class PlatformViewAndroidJNI {
197196

198197
virtual double GetDisplayRefreshRate() = 0;
199198

200-
virtual Dart_Handle FlutterViewDownloadDynamicFeature(
201-
int loading_unit_id) = 0;
199+
virtual bool FlutterViewDownloadDynamicFeature(int loading_unit_id) = 0;
202200
};
203201

204202
} // namespace flutter

shell/platform/android/platform_view_android.cc

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#include <memory>
88
#include <utility>
99

10+
#include "flutter/fml/command_line.h"
1011
#include "flutter/fml/synchronization/waitable_event.h"
1112
#include "flutter/shell/common/shell_io_manager.h"
13+
#include "flutter/shell/common/switches.h"
1214
#include "flutter/shell/gpu/gpu_surface_gl_delegate.h"
1315
#include "flutter/shell/platform/android/android_context_gl.h"
1416
#include "flutter/shell/platform/android/android_external_texture_gl.h"
@@ -323,7 +325,48 @@ PlatformViewAndroid::ComputePlatformResolvedLocales(
323325

324326
// |PlatformView|
325327
Dart_Handle PlatformViewAndroid::OnDartLoadLibrary(intptr_t loading_unit_id) {
326-
return jni_facade_->FlutterViewDownloadDynamicFeature(loading_unit_id);
328+
if (jni_facade_->FlutterViewDownloadDynamicFeature(loading_unit_id)) {
329+
return Dart_Null();
330+
}
331+
return Dart_Null(); // TODO: RETURN ERROR
332+
}
333+
334+
// |PlatformView|
335+
Dart_Handle PlatformViewAndroid::CompleteDartLoadLibrary(
336+
intptr_t loading_unit_id,
337+
std::string lib_name) {
338+
std::vector<std::string> args;
339+
args.push_back("flutter");
340+
args.push_back("--aot-shared-library-name=" + lib_name);
341+
// args.push_back("--" + "aot-shared-library-name" + "=" +
342+
// fml::jni::JavaStringToString(env, jLibPath));
343+
// shellArgs.add(
344+
// "--" + AOT_SHARED_LIBRARY_NAME + "=" +
345+
// flutterApplicationInfo.aotSharedLibraryName);
346+
347+
// // Most devices can load the AOT shared library based on the library name
348+
// // with no directory path. Provide a fully qualified path to the library
349+
// // as a workaround for devices where that fails.
350+
// shellArgs.add(
351+
// "--"
352+
// + AOT_SHARED_LIBRARY_NAME
353+
// + "="
354+
// + flutterApplicationInfo.nativeLibraryDir
355+
// + File.separator
356+
// + flutterApplicationInfo.aotSharedLibraryName);
357+
auto command_line = fml::CommandLineFromIterators(args.begin(), args.end());
358+
359+
auto settings = SettingsFromCommandLine(command_line);
360+
361+
fml::RefPtr<DartSnapshot> isolateSnapshot =
362+
DartSnapshot::IsolateSnapshotFromSettings(settings);
363+
364+
// ANDROID_SHELL_HOLDER->Launch(std::move(config));
365+
366+
Dart_Handle result = Dart_DeferredLoadComplete(
367+
loading_unit_id, isolateSnapshot->GetDataMapping(),
368+
isolateSnapshot->GetInstructionsMapping());
369+
return result;
327370
}
328371

329372
void PlatformViewAndroid::InstallFirstFrameCallback() {

shell/platform/android/platform_view_android.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class PlatformViewAndroid final : public PlatformView {
7878
int64_t texture_id,
7979
const fml::jni::JavaObjectWeakGlobalRef& surface_texture);
8080

81+
// |PlatformView|
82+
Dart_Handle CompleteDartLoadLibrary(intptr_t loading_unit_id,
83+
std::string lib_name) override;
84+
8185
private:
8286
const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
8387

shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313

1414
#include "flutter/assets/directory_asset_bundle.h"
1515
#include "flutter/common/settings.h"
16+
#include "flutter/fml/command_line.h"
1617
#include "flutter/fml/file.h"
1718
#include "flutter/fml/platform/android/jni_util.h"
1819
#include "flutter/fml/platform/android/jni_weak_ref.h"
1920
#include "flutter/fml/platform/android/scoped_java_ref.h"
2021
#include "flutter/fml/size.h"
2122
#include "flutter/lib/ui/plugins/callback_cache.h"
2223
#include "flutter/runtime/dart_service_isolate.h"
24+
#include "flutter/runtime/dart_snapshot.h"
2325
#include "flutter/shell/common/run_configuration.h"
26+
#include "flutter/shell/common/switches.h"
2427
#include "flutter/shell/platform/android/android_external_texture_gl.h"
2528
#include "flutter/shell/platform/android/android_shell_holder.h"
2629
#include "flutter/shell/platform/android/apk_asset_provider.h"
@@ -514,48 +517,46 @@ static jboolean FlutterTextUtilsIsRegionalIndicator(JNIEnv* env,
514517
static void LoadDartLibrary(JNIEnv* env,
515518
jobject obj,
516519
jlong shell_holder,
517-
jstring jLibPath,
520+
jint jLoadingUnitId,
521+
jstring jAssetPath,
522+
jstring jLibName,
518523
jobject jAssetManager) {
524+
FML_LOG(ERROR) << "LoadDartLibrary Native";
519525
// see RunBundleAndSnapshotFromLibrary above for dart loading code
520526
auto asset_manager = std::make_shared<flutter::AssetManager>();
521527

522528
asset_manager->PushBack(std::make_unique<flutter::APKAssetProvider>(
523-
env, // jni environment
524-
jAssetManager, // asset manager
525-
fml::jni::JavaStringToString(env, jLibPath)) // apk asset dir
529+
env, // jni environment
530+
jAssetManager, // asset manager
531+
fml::jni::JavaStringToString(env, jAssetPath)) // apk asset dir
526532
);
527533

528-
std::unique_ptr<IsolateConfiguration> isolate_configuration;
529-
if (flutter::DartVM::IsRunningPrecompiledCode()) {
530-
isolate_configuration = IsolateConfiguration::CreateForAppSnapshot();
531-
} else {
532-
std::unique_ptr<fml::Mapping> kernel_blob =
533-
fml::FileMapping::CreateReadOnly(
534-
ANDROID_SHELL_HOLDER->GetSettings().application_kernel_asset);
535-
if (!kernel_blob) {
536-
FML_DLOG(ERROR) << "Unable to load the kernel blob asset.";
537-
return;
538-
}
539-
isolate_configuration =
540-
IsolateConfiguration::CreateForKernel(std::move(kernel_blob));
541-
}
542-
543-
RunConfiguration config(std::move(isolate_configuration),
544-
std::move(asset_manager));
534+
// std::unique_ptr<IsolateConfiguration> isolate_configuration;
535+
// if (flutter::DartVM::IsRunningPrecompiledCode()) {
536+
// isolate_configuration = IsolateConfiguration::CreateForAppSnapshot();
537+
// } else {
538+
// std::unique_ptr<fml::Mapping> kernel_blob =
539+
// fml::FileMapping::CreateReadOnly(
540+
// ANDROID_SHELL_HOLDER->GetSettings().application_kernel_asset);
541+
// if (!kernel_blob) {
542+
// FML_DLOG(ERROR) << "Unable to load the kernel blob asset.";
543+
// return;
544+
// }
545+
// isolate_configuration =
546+
// IsolateConfiguration::CreateForKernel(std::move(kernel_blob));
547+
// }
545548

546-
// {
547-
// auto entrypoint = fml::jni::JavaStringToString(env, jEntrypoint);
548-
// auto libraryUrl = fml::jni::JavaStringToString(env, jLibraryUrl);
549+
// RunConfiguration config(std::move(isolate_configuration),
550+
// std::move(asset_manager));
549551

550-
// if ((entrypoint.size() > 0) && (libraryUrl.size() > 0)) {
551-
// config.SetEntrypointAndLibrary(std::move(entrypoint),
552-
// std::move(libraryUrl));
553-
// } else if (entrypoint.size() > 0) {
554-
// config.SetEntrypoint(std::move(entrypoint));
555-
// }
552+
ANDROID_SHELL_HOLDER->GetPlatformView()->CompleteDartLoadLibrary(
553+
static_cast<intptr_t>(jLoadingUnitId),
554+
fml::jni::JavaStringToString(env, jLibName));
555+
FML_LOG(ERROR) << "LoadDartLibrary Native Finished";
556+
// if (Dart_IsError(result)) {
557+
// return;
556558
// }
557559

558-
ANDROID_SHELL_HOLDER->Launch(std::move(config));
559560
// ANDROID_SHELL_HOLDER->DynamicFeatureInstallSuccess();
560561
}
561562

@@ -730,7 +731,8 @@ bool RegisterApi(JNIEnv* env) {
730731
{
731732
.name = "nativeLoadDartLibrary",
732733
.signature =
733-
"(JLjava/lang/String;Landroid/content/res/AssetManager;)V",
734+
"(JILjava/lang/String;Ljava/lang/String;Landroid/content/"
735+
"res/AssetManager;)V",
734736
.fnPtr = reinterpret_cast<void*>(&LoadDartLibrary),
735737
},
736738
{
@@ -1420,20 +1422,20 @@ double PlatformViewAndroidJNIImpl::GetDisplayRefreshRate() {
14201422
return static_cast<double>(env->GetStaticFloatField(clazz, fid));
14211423
}
14221424

1423-
Dart_Handle PlatformViewAndroidJNIImpl::FlutterViewDownloadDynamicFeature(
1425+
bool PlatformViewAndroidJNIImpl::FlutterViewDownloadDynamicFeature(
14241426
int loading_unit_id) {
14251427
JNIEnv* env = fml::jni::AttachCurrentThread();
14261428

14271429
auto java_object = java_object_.get(env);
14281430
if (java_object.is_null()) {
1429-
return Dart_Null();
1431+
return true;
14301432
}
14311433

14321434
env->CallObjectMethod(java_object.obj(), g_download_dynamic_feature_method,
14331435
loading_unit_id);
14341436

14351437
FML_CHECK(CheckException(env));
1436-
return Dart_Null();
1438+
return true;
14371439
}
14381440

14391441
} // namespace flutter

shell/platform/android/platform_view_android_jni_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
8080

8181
double GetDisplayRefreshRate() override;
8282

83-
Dart_Handle FlutterViewDownloadDynamicFeature(int loading_unit_id) override;
83+
bool FlutterViewDownloadDynamicFeature(int loading_unit_id) override;
8484

8585
private:
8686
// Reference to FlutterJNI object.

0 commit comments

Comments
 (0)