}
//步骤5、常规的构造器模式。
public static final class Builder {
private FlutterLoader flutterLoader;
private DeferredComponentManager deferredComponentManager;
//…
private void fillDefaults() {
if (flutterLoader == null) {
//步骤6、实例化一个FlutterLoader。
flutterLoader = new FlutterLoader();
}
// DeferredComponentManager’s intended default is null.
}
public FlutterInjector build() {
fillDefaults();
return new FlutterInjector(flutterLoader, deferredComponentManager);
}
}
}
可以看到,FlutterInjector 看起来只是一个全局单例的注入管理类角色,其核心还在于 FlutterInjector 和 DeferredComponentManager,由于我们不是海外市场,所以暂时不分析 DeferredComponentManager 特性,着重关注通用的 FlutterInjector 机制。
FlutterLoader 相关分析
正如其注释说的,这个类的职责是在应用 APK 中查找 Flutter 资源并加载 Flutter 原生库。源码如下:
public class FlutterLoader {
//…
//步骤7、创建实例,传递FlutterJNI实例,下一小节分析FlutterJNI。
public FlutterLoader() {
this(new FlutterJNI());
}
public FlutterLoader(@NonNull FlutterJNI flutterJNI) {
this.flutterJNI = flutterJNI;
}
//…
//步骤8、初始化结果结构体。
private static class InitResult {
final String appStoragePath;
final String engineCachesPath;
final String dataDirPath;
//…
}
//步骤9、FlutterEngine实例化中调用,重要开始入口。
public void startInitialization(@NonNull Context applicationContext) {
startInitialization(applicationContext, new Settings());
}
//步骤10、加载Flutter Engine native so库,定位Dart resources在apk中的路径。
//必须在主线程调用此方法。
public void startInitialization(@NonNull Context applicationContext, @NonNull Settings settings) {
//…
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException(“startInitialization must be called on the main thread”);
}
final Context appContext = applicationContext.getApplicationContext();
this.settings = settings;
initStartTimestampMillis = SystemClock.uptimeMillis();
//步骤11、通过ApplicationInfoLoader的load获取FlutterApplicationInfo信息。
//里面主要都是flutterAssetsDir、nativeLibraryDir、aotSharedLibraryName等信息,下一小节专门