Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | Package Attributes | Static Package Attributes | List of all members
io.flutter.embedding.engine.loader.FlutterLoader Class Reference

Classes

class  Settings
 

Public Member Functions

 FlutterLoader ()
 
 FlutterLoader (@NonNull FlutterJNI flutterJNI)
 
 FlutterLoader (@NonNull FlutterJNI flutterJNI, @NonNull ExecutorService executorService)
 
void startInitialization (@NonNull Context applicationContext)
 
void startInitialization (@NonNull Context applicationContext, @NonNull Settings settings)
 
void ensureInitializationComplete ( @NonNull Context applicationContext, @Nullable String[] args)
 
void ensureInitializationCompleteAsync ( @NonNull Context applicationContext, @Nullable String[] args, @NonNull Handler callbackHandler, @NonNull Runnable callback)
 
boolean initialized ()
 
String findAppBundlePath ()
 
String getLookupKeyForAsset (@NonNull String asset)
 
String getLookupKeyForAsset (@NonNull String asset, @NonNull String packageName)
 
boolean automaticallyRegisterPlugins ()
 

Package Attributes

Future< InitResult > initResultFuture
 

Static Package Attributes

static final String AOT_SHARED_LIBRARY_NAME = "aot-shared-library-name"
 
static final String AOT_VMSERVICE_SHARED_LIBRARY_NAME = "aot-vmservice-shared-library-name"
 
static final String SNAPSHOT_ASSET_PATH_KEY = "snapshot-asset-path"
 
static final String VM_SNAPSHOT_DATA_KEY = "vm-snapshot-data"
 
static final String ISOLATE_SNAPSHOT_DATA_KEY = "isolate-snapshot-data"
 
static final String FLUTTER_ASSETS_DIR_KEY = "flutter-assets-dir"
 
static final String AUTOMATICALLY_REGISTER_PLUGINS_KEY = "automatically-register-plugins"
 

Detailed Description

Finds Flutter resources in an application APK and also loads Flutter's native library.

Definition at line 35 of file FlutterLoader.java.

Constructor & Destructor Documentation

◆ FlutterLoader() [1/3]

io.flutter.embedding.engine.loader.FlutterLoader.FlutterLoader ( )
inline

Creates a FlutterLoader that uses a default constructed FlutterJNI and ExecutorService.

Definition at line 84 of file FlutterLoader.java.

84 {
85 this(FlutterInjector.instance().getFlutterJNIFactory().provideFlutterJNI());
86 }

◆ FlutterLoader() [2/3]

io.flutter.embedding.engine.loader.FlutterLoader.FlutterLoader ( @NonNull FlutterJNI  flutterJNI)
inline

Creates a FlutterLoader that uses a default constructed ExecutorService.

Parameters
flutterJNIThe FlutterJNI instance to use for loading the libflutter.so C++ library, setting up the font manager, and calling into C++ initialization.

Definition at line 94 of file FlutterLoader.java.

94 {
95 this(flutterJNI, FlutterInjector.instance().executorService());
96 }

◆ FlutterLoader() [3/3]

io.flutter.embedding.engine.loader.FlutterLoader.FlutterLoader ( @NonNull FlutterJNI  flutterJNI,
@NonNull ExecutorService  executorService 
)
inline

Creates a FlutterLoader with the specified FlutterJNI.

Parameters
flutterJNIThe FlutterJNI instance to use for loading the libflutter.so C++ library, setting up the font manager, and calling into C++ initialization.
executorServiceThe ExecutorService to use when creating new threads.

Definition at line 105 of file FlutterLoader.java.

105 {
106 this.flutterJNI = flutterJNI;
107 this.executorService = executorService;
108 }

Member Function Documentation

◆ automaticallyRegisterPlugins()

boolean io.flutter.embedding.engine.loader.FlutterLoader.automaticallyRegisterPlugins ( )
inline

Returns the configuration on whether flutter engine should automatically register plugins.

Definition at line 489 of file FlutterLoader.java.

489 {
490 return flutterApplicationInfo.automaticallyRegisterPlugins;
491 }

◆ ensureInitializationComplete()

void io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete ( @NonNull Context  applicationContext,
@Nullable String[]  args 
)
inline

Blocks until initialization of the native system has completed.

Calling this method multiple times has no effect.

Parameters
applicationContextThe Android application context.
argsFlags sent to the Flutter runtime.

Definition at line 245 of file FlutterLoader.java.

246 {
247 if (initialized) {
248 return;
249 }
250 if (Looper.myLooper() != Looper.getMainLooper()) {
251 throw new IllegalStateException(
252 "ensureInitializationComplete must be called on the main thread");
253 }
254 if (settings == null) {
255 throw new IllegalStateException(
256 "ensureInitializationComplete must be called after startInitialization");
257 }
258
259 try (TraceSection e = TraceSection.scoped("FlutterLoader#ensureInitializationComplete")) {
260 InitResult result = initResultFuture.get();
261
262 List<String> shellArgs = new ArrayList<>();
263 shellArgs.add("--icu-symbol-prefix=_binary_icudtl_dat");
264
265 shellArgs.add(
266 "--icu-native-lib-path="
267 + flutterApplicationInfo.nativeLibraryDir
268 + File.separator
269 + DEFAULT_LIBRARY);
270 if (args != null) {
271 Collections.addAll(shellArgs, args);
272 }
273
274 String kernelPath = null;
275 if (BuildConfig.DEBUG || BuildConfig.JIT_RELEASE) {
276 String snapshotAssetPath =
277 result.dataDirPath + File.separator + flutterApplicationInfo.flutterAssetsDir;
278 kernelPath = snapshotAssetPath + File.separator + DEFAULT_KERNEL_BLOB;
279 shellArgs.add("--" + SNAPSHOT_ASSET_PATH_KEY + "=" + snapshotAssetPath);
280 shellArgs.add("--" + VM_SNAPSHOT_DATA_KEY + "=" + flutterApplicationInfo.vmSnapshotData);
281 shellArgs.add(
282 "--" + ISOLATE_SNAPSHOT_DATA_KEY + "=" + flutterApplicationInfo.isolateSnapshotData);
283 } else {
284 shellArgs.add(
285 "--" + AOT_SHARED_LIBRARY_NAME + "=" + flutterApplicationInfo.aotSharedLibraryName);
286
287 // Most devices can load the AOT shared library based on the library name
288 // with no directory path. Provide a fully qualified path to the library
289 // as a workaround for devices where that fails.
290 shellArgs.add(
291 "--"
293 + "="
294 + flutterApplicationInfo.nativeLibraryDir
295 + File.separator
296 + flutterApplicationInfo.aotSharedLibraryName);
297
298 // In profile mode, provide a separate library containing a snapshot for
299 // launching the Dart VM service isolate.
300 if (BuildConfig.PROFILE) {
301 shellArgs.add(
302 "--" + AOT_VMSERVICE_SHARED_LIBRARY_NAME + "=" + VMSERVICE_SNAPSHOT_LIBRARY);
303 }
304 }
305
306 shellArgs.add("--cache-dir-path=" + result.engineCachesPath);
307 if (flutterApplicationInfo.domainNetworkPolicy != null) {
308 shellArgs.add("--domain-network-policy=" + flutterApplicationInfo.domainNetworkPolicy);
309 }
310 if (settings.getLogTag() != null) {
311 shellArgs.add("--log-tag=" + settings.getLogTag());
312 }
313
314 ApplicationInfo applicationInfo =
315 applicationContext
316 .getPackageManager()
317 .getApplicationInfo(
318 applicationContext.getPackageName(), PackageManager.GET_META_DATA);
319 Bundle metaData = applicationInfo.metaData;
320 int oldGenHeapSizeMegaBytes =
321 metaData != null ? metaData.getInt(OLD_GEN_HEAP_SIZE_META_DATA_KEY) : 0;
322 if (oldGenHeapSizeMegaBytes == 0) {
323 // default to half of total memory.
324 ActivityManager activityManager =
325 (ActivityManager) applicationContext.getSystemService(Context.ACTIVITY_SERVICE);
326 ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
327 activityManager.getMemoryInfo(memInfo);
328 oldGenHeapSizeMegaBytes = (int) (memInfo.totalMem / 1e6 / 2);
329 }
330 shellArgs.add("--old-gen-heap-size=" + oldGenHeapSizeMegaBytes);
331
332 DisplayMetrics displayMetrics = applicationContext.getResources().getDisplayMetrics();
333 int screenWidth = displayMetrics.widthPixels;
334 int screenHeight = displayMetrics.heightPixels;
335 // This is the formula Android uses.
336 // https://android.googlesource.com/platform/frameworks/base/+/39ae5bac216757bc201490f4c7b8c0f63006c6cd/libs/hwui/renderthread/CacheManager.cpp#45
337 int resourceCacheMaxBytesThreshold = screenWidth * screenHeight * 12 * 4;
338 shellArgs.add("--resource-cache-max-bytes-threshold=" + resourceCacheMaxBytesThreshold);
339
340 shellArgs.add("--prefetched-default-font-manager");
341
342 if (metaData != null) {
343 if (metaData.getBoolean(ENABLE_IMPELLER_META_DATA_KEY, false)) {
344 shellArgs.add("--enable-impeller");
345 }
346 if (metaData.getBoolean(ENABLE_VULKAN_VALIDATION_META_DATA_KEY, false)) {
347 shellArgs.add("--enable-vulkan-validation");
348 }
349 if (metaData.getBoolean(IMPELLER_OPENGL_GPU_TRACING_DATA_KEY, false)) {
350 shellArgs.add("--enable-opengl-gpu-tracing");
351 }
352 if (metaData.getBoolean(IMPELLER_VULKAN_GPU_TRACING_DATA_KEY, false)) {
353 shellArgs.add("--enable-vulkan-gpu-tracing");
354 }
355 String backend = metaData.getString(IMPELLER_BACKEND_META_DATA_KEY);
356 if (backend != null) {
357 shellArgs.add("--impeller-backend=" + backend);
358 }
359 }
360
361 final String leakVM = isLeakVM(metaData) ? "true" : "false";
362 shellArgs.add("--leak-vm=" + leakVM);
363
364 long initTimeMillis = SystemClock.uptimeMillis() - initStartTimestampMillis;
365
366 flutterJNI.init(
367 applicationContext,
368 shellArgs.toArray(new String[0]),
369 kernelPath,
370 result.appStoragePath,
371 result.engineCachesPath,
372 initTimeMillis);
373
374 initialized = true;
375 } catch (Exception e) {
376 Log.e(TAG, "Flutter initialization failed.", e);
377 throw new RuntimeException(e);
378 }
379 }
const char * backend
Type::kYUV Type::kRGBA() int(0.7 *637)
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)
void init( @NonNull Context context, @NonNull String[] args, @Nullable String bundlePath, @NonNull String appStoragePath, @NonNull String engineCachesPath, long initTimeMillis)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
void Log(const char *format,...) SK_PRINTF_LIKE(1

◆ ensureInitializationCompleteAsync()

void io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationCompleteAsync ( @NonNull Context  applicationContext,
@Nullable String[]  args,
@NonNull Handler  callbackHandler,
@NonNull Runnable  callback 
)
inline

Same as ensureInitializationComplete(Context, String[]) but waiting on a background thread, then invoking callback on the callbackHandler.

Definition at line 393 of file FlutterLoader.java.

397 {
398 if (Looper.myLooper() != Looper.getMainLooper()) {
399 throw new IllegalStateException(
400 "ensureInitializationComplete must be called on the main thread");
401 }
402 if (settings == null) {
403 throw new IllegalStateException(
404 "ensureInitializationComplete must be called after startInitialization");
405 }
406 if (initialized) {
407 callbackHandler.post(callback);
408 return;
409 }
410 executorService.execute(
411 () -> {
412 InitResult result;
413 try {
414 result = initResultFuture.get();
415 } catch (Exception e) {
416 Log.e(TAG, "Flutter initialization failed.", e);
417 throw new RuntimeException(e);
418 }
419 HandlerCompat.createAsyncHandler(Looper.getMainLooper())
420 .post(
421 () -> {
422 ensureInitializationComplete(applicationContext.getApplicationContext(), args);
423 callbackHandler.post(callback);
424 });
425 });
426 }
void ensureInitializationComplete( @NonNull Context applicationContext, @Nullable String[] args)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

◆ findAppBundlePath()

String io.flutter.embedding.engine.loader.FlutterLoader.findAppBundlePath ( )
inline

Definition at line 457 of file FlutterLoader.java.

457 {
458 return flutterApplicationInfo.flutterAssetsDir;
459 }

◆ getLookupKeyForAsset() [1/2]

String io.flutter.embedding.engine.loader.FlutterLoader.getLookupKeyForAsset ( @NonNull String  asset)
inline

Returns the file name for the given asset. The returned file name can be used to access the asset in the APK through the android.content.res.AssetManager API.

Parameters
assetthe name of the asset. The name can be hierarchical
Returns
the filename to be used with android.content.res.AssetManager

Definition at line 469 of file FlutterLoader.java.

469 {
470 return fullAssetPathFrom(asset);
471 }

◆ getLookupKeyForAsset() [2/2]

String io.flutter.embedding.engine.loader.FlutterLoader.getLookupKeyForAsset ( @NonNull String  asset,
@NonNull String  packageName 
)
inline

Returns the file name for the given asset which originates from the specified packageName. The returned file name can be used to access the asset in the APK through the android.content.res.AssetManager API.

Parameters
assetthe name of the asset. The name can be hierarchical
packageNamethe name of the package from which the asset originates
Returns
the file name to be used with android.content.res.AssetManager

Definition at line 483 of file FlutterLoader.java.

483 {
484 return getLookupKeyForAsset("packages" + File.separator + packageName + File.separator + asset);
485 }
String getLookupKeyForAsset(@NonNull String asset)

◆ initialized()

boolean io.flutter.embedding.engine.loader.FlutterLoader.initialized ( )
inline

Returns whether the FlutterLoader has finished loading the native library.

Definition at line 429 of file FlutterLoader.java.

429 {
430 return initialized;
431 }

◆ startInitialization() [1/2]

void io.flutter.embedding.engine.loader.FlutterLoader.startInitialization ( @NonNull Context  applicationContext)
inline

Starts initialization of the native system.

Parameters
applicationContextThe Android application context.

Definition at line 136 of file FlutterLoader.java.

136 {
137 startInitialization(applicationContext, new Settings());
138 }
void startInitialization(@NonNull Context applicationContext)

◆ startInitialization() [2/2]

void io.flutter.embedding.engine.loader.FlutterLoader.startInitialization ( @NonNull Context  applicationContext,
@NonNull Settings  settings 
)
inline

Starts initialization of the native system.

This loads the Flutter engine's native library to enable subsequent JNI calls. This also starts locating and unpacking Dart resources packaged in the app's APK.

Calling this method multiple times has no effect.

Parameters
applicationContextThe Android application context.
settingsConfiguration settings.

Definition at line 151 of file FlutterLoader.java.

151 {
152 // Do not run startInitialization more than once.
153 if (this.settings != null) {
154 return;
155 }
156 if (Looper.myLooper() != Looper.getMainLooper()) {
157 throw new IllegalStateException("startInitialization must be called on the main thread");
158 }
159
160 try (TraceSection e = TraceSection.scoped("FlutterLoader#startInitialization")) {
161 // Ensure that the context is actually the application context.
162 final Context appContext = applicationContext.getApplicationContext();
163
164 this.settings = settings;
165
166 initStartTimestampMillis = SystemClock.uptimeMillis();
167 flutterApplicationInfo = ApplicationInfoLoader.load(appContext);
168
169 final DisplayManager dm =
170 (DisplayManager) appContext.getSystemService(Context.DISPLAY_SERVICE);
171 VsyncWaiter waiter = VsyncWaiter.getInstance(dm, flutterJNI);
172 waiter.init();
173
174 // Use a background thread for initialization tasks that require disk access.
175 Callable<InitResult> initTask =
176 new Callable<InitResult>() {
177 @Override
178 public InitResult call() {
179 try (TraceSection e = TraceSection.scoped("FlutterLoader initTask")) {
180 ResourceExtractor resourceExtractor = initResources(appContext);
181
182 try {
183 flutterJNI.loadLibrary();
184 } catch (UnsatisfiedLinkError unsatisfiedLinkError) {
185 String couldntFindVersion = "couldn't find \"libflutter.so\"";
186 String notFoundVersion = "dlopen failed: library \"libflutter.so\" not found";
187
188 if (unsatisfiedLinkError.toString().contains(couldntFindVersion)
189 || unsatisfiedLinkError.toString().contains(notFoundVersion)) {
190 // To gather more information for
191 // https://github.com/flutter/flutter/issues/144291,
192 // log the contents of the native libraries directory as well as the
193 // cpu architecture.
194
195 String cpuArch = System.getProperty("os.arch");
196 File nativeLibsDir = new File(flutterApplicationInfo.nativeLibraryDir);
197 String[] nativeLibsContents = nativeLibsDir.list();
198
199 throw new UnsupportedOperationException(
200 "Could not load libflutter.so this is possibly because the application"
201 + " is running on an architecture that Flutter Android does not support (e.g. x86)"
202 + " see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures"
203 + " for more detail.\n"
204 + "App is using cpu architecture: "
205 + cpuArch
206 + ", and the native libraries directory (with path "
207 + nativeLibsDir.getAbsolutePath()
208 + ") contains the following files: "
209 + Arrays.toString(nativeLibsContents),
210 unsatisfiedLinkError);
211 }
212
213 throw unsatisfiedLinkError;
214 }
215
216 flutterJNI.updateRefreshRate();
217
218 // Prefetch the default font manager as soon as possible on a background thread.
219 // It helps to reduce time cost of engine setup that blocks the platform thread.
220 executorService.execute(() -> flutterJNI.prefetchDefaultFontManager());
221
222 if (resourceExtractor != null) {
223 resourceExtractor.waitForCompletion();
224 }
225
226 return new InitResult(
227 PathUtils.getFilesDir(appContext),
228 PathUtils.getCacheDirectory(appContext),
229 PathUtils.getDataDirectory(appContext));
230 }
231 }
232 };
233 initResultFuture = executorService.submit(initTask);
234 }
235 }
call(args)
Definition dom.py:159

Member Data Documentation

◆ AOT_SHARED_LIBRARY_NAME

final String io.flutter.embedding.engine.loader.FlutterLoader.AOT_SHARED_LIBRARY_NAME = "aot-shared-library-name"
staticpackage

Definition at line 65 of file FlutterLoader.java.

◆ AOT_VMSERVICE_SHARED_LIBRARY_NAME

final String io.flutter.embedding.engine.loader.FlutterLoader.AOT_VMSERVICE_SHARED_LIBRARY_NAME = "aot-vmservice-shared-library-name"
staticpackage

Definition at line 66 of file FlutterLoader.java.

◆ AUTOMATICALLY_REGISTER_PLUGINS_KEY

final String io.flutter.embedding.engine.loader.FlutterLoader.AUTOMATICALLY_REGISTER_PLUGINS_KEY = "automatically-register-plugins"
staticpackage

Definition at line 71 of file FlutterLoader.java.

◆ FLUTTER_ASSETS_DIR_KEY

final String io.flutter.embedding.engine.loader.FlutterLoader.FLUTTER_ASSETS_DIR_KEY = "flutter-assets-dir"
staticpackage

Definition at line 70 of file FlutterLoader.java.

◆ initResultFuture

Future<InitResult> io.flutter.embedding.engine.loader.FlutterLoader.initResultFuture
package

Definition at line 129 of file FlutterLoader.java.

◆ ISOLATE_SNAPSHOT_DATA_KEY

final String io.flutter.embedding.engine.loader.FlutterLoader.ISOLATE_SNAPSHOT_DATA_KEY = "isolate-snapshot-data"
staticpackage

Definition at line 69 of file FlutterLoader.java.

◆ SNAPSHOT_ASSET_PATH_KEY

final String io.flutter.embedding.engine.loader.FlutterLoader.SNAPSHOT_ASSET_PATH_KEY = "snapshot-asset-path"
staticpackage

Definition at line 67 of file FlutterLoader.java.

◆ VM_SNAPSHOT_DATA_KEY

final String io.flutter.embedding.engine.loader.FlutterLoader.VM_SNAPSHOT_DATA_KEY = "vm-snapshot-data"
staticpackage

Definition at line 68 of file FlutterLoader.java.


The documentation for this class was generated from the following file: