diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java index ca2fa1ab51..eb696a6ddc 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterMain.java @@ -4,17 +4,19 @@ package io.flutter.view; +import android.app.AlarmManager; +import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.AssetManager; -import android.app.AlarmManager; -import android.app.PendingIntent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.SystemClock; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.Log; import io.flutter.util.PathUtils; @@ -61,7 +63,8 @@ public class FlutterMain { private static final String DEFAULT_KERNEL_BLOB = "kernel_blob.bin"; private static final String DEFAULT_FLUTTER_ASSETS_DIR = "flutter_assets"; - private static String fromFlutterAssets(String filePath) { + @NonNull + private static String fromFlutterAssets(@NonNull String filePath) { return sFlutterAssetsDir + File.separator + filePath; } @@ -74,10 +77,16 @@ public class FlutterMain { private static String sFlutterAssetsDir = DEFAULT_FLUTTER_ASSETS_DIR; private static boolean sInitialized = false; + private static boolean sIsPrecompiledAsBlobs = false; + private static boolean sIsPrecompiledAsSharedLibrary = false; + + @Nullable private static ResourceExtractor sResourceExtractor; - private static boolean sIsPrecompiledAsBlobs; - private static boolean sIsPrecompiledAsSharedLibrary; + @Nullable private static Settings sSettings; + @NonNull + private static String sSnapshotPath; + private static final class ImmutableSetBuilder { static ImmutableSetBuilder newInstance() { @@ -88,19 +97,22 @@ public class FlutterMain { private ImmutableSetBuilder() {} - ImmutableSetBuilder add(T element) { + @NonNull + ImmutableSetBuilder add(@NonNull T element) { set.add(element); return this; } @SafeVarargs - final ImmutableSetBuilder add(T... elements) { + @NonNull + final ImmutableSetBuilder add(@NonNull T... elements) { for (T element : elements) { set.add(element); } return this; } + @NonNull Set build() { return Collections.unmodifiableSet(set); } @@ -109,6 +121,7 @@ public class FlutterMain { public static class Settings { private String logTag; + @Nullable public String getLogTag() { return logTag; } @@ -126,7 +139,7 @@ public class FlutterMain { * Starts initialization of the native system. * @param applicationContext The Android application context. */ - public static void startInitialization(Context applicationContext) { + public static void startInitialization(@NonNull Context applicationContext) { startInitialization(applicationContext, new Settings()); } @@ -135,7 +148,7 @@ public class FlutterMain { * @param applicationContext The Android application context. * @param settings Configuration settings. */ - public static void startInitialization(Context applicationContext, Settings settings) { + public static void startInitialization(@NonNull Context applicationContext, @NonNull Settings settings) { if (Looper.myLooper() != Looper.getMainLooper()) { throw new IllegalStateException("startInitialization must be called on the main thread"); } @@ -167,7 +180,7 @@ public class FlutterMain { * @param applicationContext The Android application context. * @param args Flags sent to the Flutter runtime. */ - public static void ensureInitializationComplete(Context applicationContext, String[] args) { + public static void ensureInitializationComplete(@NonNull Context applicationContext, @Nullable String[] args) { if (Looper.myLooper() != Looper.getMainLooper()) { throw new IllegalStateException("ensureInitializationComplete must be called on the main thread"); } @@ -178,13 +191,16 @@ public class FlutterMain { return; } try { - sResourceExtractor.waitForCompletion(); + // There are resources to extract. For example, the AOT blobs from the `assets` directory. + // `sResourceExtractor` is `null` if there isn't any AOT blob to extract. + if (sResourceExtractor != null) { + sResourceExtractor.waitForCompletion(); + } List shellArgs = new ArrayList<>(); - shellArgs.add("--icu-symbol-prefix=_binary_icudtl_dat"); - ApplicationInfo applicationInfo = applicationContext.getPackageManager().getApplicationInfo( - applicationContext.getPackageName(), PackageManager.GET_META_DATA); + + ApplicationInfo applicationInfo = getApplicationInfo(applicationContext); shellArgs.add("--icu-native-lib-path=" + applicationInfo.nativeLibraryDir + File.separator + DEFAULT_LIBRARY); if (args != null) { @@ -192,17 +208,13 @@ public class FlutterMain { } if (sIsPrecompiledAsSharedLibrary) { shellArgs.add("--" + AOT_SHARED_LIBRARY_PATH + "=" + - new File(PathUtils.getDataDirectory(applicationContext), sAotSharedLibraryPath)); + new File(sSnapshotPath, sAotSharedLibraryPath)); } else { if (sIsPrecompiledAsBlobs) { - shellArgs.add("--" + AOT_SNAPSHOT_PATH_KEY + "=" + - PathUtils.getDataDirectory(applicationContext)); + shellArgs.add("--" + AOT_SNAPSHOT_PATH_KEY + "=" + sSnapshotPath); } else { - shellArgs.add("--cache-dir-path=" + - PathUtils.getCacheDirectory(applicationContext)); - - shellArgs.add("--" + AOT_SNAPSHOT_PATH_KEY + "=" + - PathUtils.getDataDirectory(applicationContext) + "/" + sFlutterAssetsDir); + shellArgs.add("--cache-dir-path=" + PathUtils.getCacheDirectory(applicationContext)); + shellArgs.add("--" + AOT_SNAPSHOT_PATH_KEY + "=" + PathUtils.getDataDirectory(applicationContext) + "/" + sFlutterAssetsDir); } shellArgs.add("--" + AOT_VM_SNAPSHOT_DATA_KEY + "=" + sAotVmSnapshotData); shellArgs.add("--" + AOT_VM_SNAPSHOT_INSTR_KEY + "=" + sAotVmSnapshotInstr); @@ -232,10 +244,10 @@ public class FlutterMain { * thread, then invoking {@code callback} on the {@code callbackHandler}. */ public static void ensureInitializationCompleteAsync( - Context applicationContext, - String[] args, - Handler callbackHandler, - Runnable callback + @NonNull Context applicationContext, + @Nullable String[] args, + @NonNull Handler callbackHandler, + @NonNull Runnable callback ) { if (Looper.myLooper() != Looper.getMainLooper()) { throw new IllegalStateException("ensureInitializationComplete must be called on the main thread"); @@ -246,11 +258,12 @@ public class FlutterMain { if (sInitialized) { return; } - new Thread(new Runnable() { @Override public void run() { - sResourceExtractor.waitForCompletion(); + if (sResourceExtractor != null) { + sResourceExtractor.waitForCompletion(); + } new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { @@ -265,44 +278,57 @@ public class FlutterMain { private static native void nativeInit(Context context, String[] args, String bundlePath, String appStoragePath, String engineCachesPath); private static native void nativeRecordStartTimestamp(long initTimeMillis); - /** - * Initialize our Flutter config values by obtaining them from the - * manifest XML file, falling back to default values. - */ - private static void initConfig(Context applicationContext) { + @NonNull + private static ApplicationInfo getApplicationInfo(@NonNull Context applicationContext) { try { - Bundle metadata = applicationContext.getPackageManager().getApplicationInfo( - applicationContext.getPackageName(), PackageManager.GET_META_DATA).metaData; - if (metadata != null) { - sAotSharedLibraryPath = metadata.getString(PUBLIC_AOT_AOT_SHARED_LIBRARY_PATH, DEFAULT_AOT_SHARED_LIBRARY_PATH); - sAotVmSnapshotData = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_DATA_KEY, DEFAULT_AOT_VM_SNAPSHOT_DATA); - sAotVmSnapshotInstr = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_INSTR_KEY, DEFAULT_AOT_VM_SNAPSHOT_INSTR); - sAotIsolateSnapshotData = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_DATA_KEY, DEFAULT_AOT_ISOLATE_SNAPSHOT_DATA); - sAotIsolateSnapshotInstr = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_INSTR_KEY, DEFAULT_AOT_ISOLATE_SNAPSHOT_INSTR); - sFlutterAssetsDir = metadata.getString(PUBLIC_FLUTTER_ASSETS_DIR_KEY, DEFAULT_FLUTTER_ASSETS_DIR); - } + return applicationContext + .getPackageManager() + .getApplicationInfo(applicationContext.getPackageName(), PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(e); } } - private static void initResources(Context applicationContext) { - Context context = applicationContext; - new ResourceCleaner(context).start(); + /** + * Initialize our Flutter config values by obtaining them from the + * manifest XML file, falling back to default values. + */ + private static void initConfig(@NonNull Context applicationContext) { + Bundle metadata = getApplicationInfo(applicationContext).metaData; - Bundle metaData = null; - try { - metaData = context.getPackageManager().getApplicationInfo( - context.getPackageName(), PackageManager.GET_META_DATA).metaData; - - } catch (PackageManager.NameNotFoundException e) { - Log.e(TAG, "Unable to read application info", e); + // There isn't a `` tag as a direct child of `` in + // `AndroidManifest.xml`. + if (metadata == null) { + return; } - final String dataDirPath = PathUtils.getDataDirectory(context); - final String packageName = context.getPackageName(); - final PackageManager packageManager = context.getPackageManager(); - final AssetManager assetManager = context.getResources().getAssets(); + sAotSharedLibraryPath = metadata.getString(PUBLIC_AOT_AOT_SHARED_LIBRARY_PATH, DEFAULT_AOT_SHARED_LIBRARY_PATH); + sFlutterAssetsDir = metadata.getString(PUBLIC_FLUTTER_ASSETS_DIR_KEY, DEFAULT_FLUTTER_ASSETS_DIR); + + sAotVmSnapshotData = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_DATA_KEY, DEFAULT_AOT_VM_SNAPSHOT_DATA); + sAotVmSnapshotInstr = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_INSTR_KEY, DEFAULT_AOT_VM_SNAPSHOT_INSTR); + sAotIsolateSnapshotData = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_DATA_KEY, DEFAULT_AOT_ISOLATE_SNAPSHOT_DATA); + sAotIsolateSnapshotInstr = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_INSTR_KEY, DEFAULT_AOT_ISOLATE_SNAPSHOT_INSTR); + } + + /** + * Extract the AOT blobs from the app's asset directory. + * This is required by the Dart runtime, so it can read the blobs. + */ + private static void initResources(@NonNull Context applicationContext) { + // When the AOT blobs are contained in the native library directory, + // we don't need to extract them manually because they are + // extracted by the Android Package Manager automatically. + if (!sSnapshotPath.equals(PathUtils.getDataDirectory(applicationContext))) { + return; + } + + new ResourceCleaner(applicationContext).start(); + + final String dataDirPath = PathUtils.getDataDirectory(applicationContext); + final String packageName = applicationContext.getPackageName(); + final PackageManager packageManager = applicationContext.getPackageManager(); + final AssetManager assetManager = applicationContext.getResources().getAssets(); sResourceExtractor = new ResourceExtractor(dataDirPath, packageName, packageManager, assetManager); sResourceExtractor @@ -313,17 +339,15 @@ public class FlutterMain { .addResource(fromFlutterAssets(DEFAULT_KERNEL_BLOB)); if (sIsPrecompiledAsSharedLibrary) { - sResourceExtractor - .addResource(sAotSharedLibraryPath); - + sResourceExtractor + .addResource(sAotSharedLibraryPath); } else { - sResourceExtractor - .addResource(sAotVmSnapshotData) - .addResource(sAotVmSnapshotInstr) - .addResource(sAotIsolateSnapshotData) - .addResource(sAotIsolateSnapshotInstr); + sResourceExtractor + .addResource(sAotVmSnapshotData) + .addResource(sAotVmSnapshotInstr) + .addResource(sAotIsolateSnapshotData) + .addResource(sAotIsolateSnapshotInstr); } - sResourceExtractor.start(); } @@ -331,7 +355,8 @@ public class FlutterMain { * Returns a list of the file names at the root of the application's asset * path. */ - private static Set listAssets(Context applicationContext, String path) { + @NonNull + private static Set listAssets(@NonNull Context applicationContext, @NonNull String path) { AssetManager manager = applicationContext.getResources().getAssets(); try { return ImmutableSetBuilder.newInstance() @@ -343,18 +368,96 @@ public class FlutterMain { } } - private static void initAot(Context applicationContext) { + /** + * Returns a list of the file names at the root of the application's + * native library directory. + */ + @NonNull + private static Set listLibs(@NonNull Context applicationContext) { + ApplicationInfo applicationInfo = getApplicationInfo(applicationContext); + File[] files = new File(applicationInfo.nativeLibraryDir).listFiles(); + + ImmutableSetBuilder builder = ImmutableSetBuilder.newInstance(); + for (File file : files) { + builder.add(file.getName()); + } + return builder.build(); + } + + /** + * Determines if the APK contains a shared library or AOT snapshots, + * the file name of the snapshots and the directory where they are contained. + * + *

The snapshots can be contained in the app's assets or in the native library + * directory. The default names are: + * + *

    + *
  • `vm_snapshot_data`
  • + *
  • `vm_snapshot_instr`
  • + *
  • `isolate_snapshot_data`
  • + *
  • `isolate_snapshot_instr`
  • + *
  • Shared library: `app.so`
  • + *
+ * + *

When the blobs are contained in the native library directory, + * the format `lib_%s.so` is applied to the file name. + * + *

Note: The name of the files can be customized in the app's metadata, but the + * format is preserved. + * + *

The AOT snapshots and the shared library cannot exist at the same time in the APK. + */ + private static void initAot(@NonNull Context applicationContext) { Set assets = listAssets(applicationContext, ""); - sIsPrecompiledAsBlobs = assets.containsAll(Arrays.asList( - sAotVmSnapshotData, - sAotVmSnapshotInstr, - sAotIsolateSnapshotData, - sAotIsolateSnapshotInstr - )); - sIsPrecompiledAsSharedLibrary = assets.contains(sAotSharedLibraryPath); + Set libs = listLibs(applicationContext); + + String aotVmSnapshotDataLib = "lib_" + sAotVmSnapshotData + ".so"; + String aotVmSnapshotInstrLib = "lib_" + sAotVmSnapshotInstr + ".so"; + String aotIsolateSnapshotDataLib = "lib_" + sAotIsolateSnapshotData + ".so"; + String aotIsolateSnapshotInstrLib = "lib_" + sAotIsolateSnapshotInstr + ".so"; + String aotSharedLibraryLib = "lib_" + sAotSharedLibraryPath + ".so"; + + boolean isPrecompiledBlobInLib = libs + .containsAll(Arrays.asList( + aotVmSnapshotDataLib, + aotVmSnapshotInstrLib, + aotIsolateSnapshotDataLib, + aotIsolateSnapshotInstrLib + )); + + if (isPrecompiledBlobInLib) { + sIsPrecompiledAsBlobs = true; + sAotVmSnapshotData = aotVmSnapshotDataLib; + sAotVmSnapshotInstr = aotVmSnapshotInstrLib; + sAotIsolateSnapshotData = aotIsolateSnapshotDataLib; + sAotIsolateSnapshotInstr = aotIsolateSnapshotInstrLib; + } else { + sIsPrecompiledAsBlobs = assets.containsAll(Arrays.asList( + sAotVmSnapshotData, + sAotVmSnapshotInstr, + sAotIsolateSnapshotData, + sAotIsolateSnapshotInstr + )); + } + boolean isSharedLibraryInLib = libs.contains(aotSharedLibraryLib); + boolean isSharedLibraryInAssets = assets.contains(sAotSharedLibraryPath); + + if (isSharedLibraryInLib) { + sAotSharedLibraryPath = aotSharedLibraryLib; + sIsPrecompiledAsSharedLibrary = true; + } else if (isSharedLibraryInAssets) { + sIsPrecompiledAsSharedLibrary = true; + } + + if (isSharedLibraryInLib || isPrecompiledBlobInLib) { + sSnapshotPath = getApplicationInfo(applicationContext).nativeLibraryDir; + } else { + sSnapshotPath = PathUtils.getDataDirectory(applicationContext); + } + if (sIsPrecompiledAsBlobs && sIsPrecompiledAsSharedLibrary) { - throw new RuntimeException( - "Found precompiled app as shared library and as Dart VM snapshots."); + throw new RuntimeException( + "Found precompiled app as shared library and as Dart VM snapshots."); } } @@ -362,7 +465,8 @@ public class FlutterMain { return sIsPrecompiledAsBlobs || sIsPrecompiledAsSharedLibrary; } - public static String findAppBundlePath(Context applicationContext) { + @Nullable + public static String findAppBundlePath(@NonNull Context applicationContext) { String dataDirectory = PathUtils.getDataDirectory(applicationContext); File appBundle = new File(dataDirectory, sFlutterAssetsDir); return appBundle.exists() ? appBundle.getPath() : null; @@ -376,7 +480,8 @@ public class FlutterMain { * @param asset the name of the asset. The name can be hierarchical * @return the filename to be used with {@link android.content.res.AssetManager} */ - public static String getLookupKeyForAsset(String asset) { + @NonNull + public static String getLookupKeyForAsset(@NonNull String asset) { return fromFlutterAssets(asset); } @@ -389,7 +494,8 @@ public class FlutterMain { * @param packageName the name of the package from which the asset originates * @return the file name to be used with {@link android.content.res.AssetManager} */ - public static String getLookupKeyForAsset(String asset, String packageName) { + @NonNull + public static String getLookupKeyForAsset(@NonNull String asset, @NonNull String packageName) { return getLookupKeyForAsset( "packages" + File.separator + packageName + File.separator + asset); }