Flutter源码系列之《一》源码系列之《一》Flutter的热更新探索的热更新探索(下下)
转载请注明出处:https://blog.csdn.net/llew2011/article/details/104075883
在上篇文章Flutter源码系列之Flutter的热更新探索(上)我们分析了Flutter的加载流程,找到了实现热更新的方法,接下来我们开始实现热更新功能。考虑到Google可能会在后续版本中
对FlutterLoader类做修改,因此我们先定义一个适配版本,代码如下:
public enum FlutterVersion {
/**
* Flutter Version: 1.14.0
*/
VERSION_011400
}
VERSION_011400表示1.14.0版本,这代表着我们热更新适配从1.14.0版本开始,定义完版本后我们还要定义与之相对应的加载器FlutterLoaderV011400,然后在定义一个
FlutterManager,它是替代FlutterMain的,代码如下:
public class FlutterManager {
private static final String TAG = "FlutterManager";
public static void startInitialization(Context context) {
startInitialization(context, null, FlutterVersion.VERSION_011400);
}
public static void startInitialization(Context context, File aotFile, FlutterVersion version) {
startInitialization(context, aotFile, version, new FlutterMain.Settings());
}
public static void startInitialization(Context context, File aotFile, FlutterVersion version, FlutterMain.Settings settings) {
ensureInitializeOnMainThread();
FlutterCallback flutterCallback = generateFlutterCallback(version);
if (null != flutterCallback) {
flutterCallback.startInitialization(context, aotFile, getFlutterLoaderSettings(settings));
} else {
FlutterLogger.w(TAG, "Flutter Version not supported: " + version);
FlutterMain.startInitialization(context);
}
}
private static void ensureInitializeOnMainThread() {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("startInitialization must be called on the main thread");
}
}
private static FlutterLoader.Settings getFlutterLoaderSettings(FlutterMain.Settings settings) {
FlutterLoader.Settings setting = new FlutterLoader.Settings();
if (null != settings) {
setting.setLogTag(settings.getLogTag());
}
return setting;
}
private static FlutterCallback generateFlutterCallback(FlutterVersion version) {
if (FlutterVersion.VERSION_011400 == version) {
return FlutterLoaderV011400.getInstance();
}
return null;
}
public interface FlutterCallback {
void startInitialization(Context context, File aotFile, FlutterLoader.Settings settings);
}
}
FlutterManager的职责是代替FlutterMain进行Flutter引擎的初始化等操作,它提供了一系列startInitialization()方法,这些方法最终执行的是含有4个参数的startInitialization()方法,它
的执行流程首先检验时候运行在主线程,如果不是则抛异常,然后调用generateFlutterCallback()方法获取一个FlutterCallback实例,FlutterCallback为了适配后续Flutter版本变更添加
的一个接口,如果获取到FlutterCallback就执行其startInitialization()方法否则执行FlutterMain的默认初始化流程,由于我们目前仅支持1.14.0版本,所以返回了一个
FlutterLoaderV011400实例,FlutterLoaderV011400是和VERSION_011400对应的加载类,其源码如下:
/**
* Flutter Version: 1.14.0
*/
public class FlutterLoaderV011400 extends FlutterLoader implements FlutterManager.FlutterCallback {
private static final String TAG = "FlutterLoader";
// Must match values in flutter::switches
private static final String AOT_SHARED_LIBRARY_NAME = "aot-shared-library-name";
private static final String SNAPSHOT_ASSET_PATH_KEY = "snapshot-asset-path";
private static final String VM_SNAPSHOT_DATA_KEY = "vm-snapshot-data";
private static final String ISOLATE_SNAPSHOT_DATA_KEY = "isolate-snapshot-data";
private static final String FLUTTER_ASSETS_DIR_KEY = "flutter-assets-dir";
// XML Attribute keys supported in AndroidManifest.xml
private static final String PUBLIC_AOT_SHARED_LIBRARY_NAME =
FlutterLoader.class.getName() + '.' + AOT_SHARED_LIBRARY_NAME;
private static final String PUBLIC_VM_SNAPSHOT_DATA_KEY =
FlutterLoader.class.getName() + '.' + VM_SNAPSHOT_DATA_KEY;
private static final String PUBLIC_ISOLATE_SNAPSHOT_DATA_KEY =
FlutterLoader.class.getName() + '.' + ISOLATE_SNAPSHOT_DATA_KEY;
private static final String PUBLIC_FLUTTER_ASSETS_DIR_KEY =
FlutterLoader.class.getName() + '.' + FLUTTER_ASSETS_DIR_KEY;
// Resource names used for components of the precompiled snapshot.
private static final String DEFAULT_AOT_SHARED_LIBRARY_NAME = "libapp.so";
private static final String DEFAULT_VM_SNAPSHOT_DATA = "vm_snapshot_data";
private static final String DEFAULT_ISOLATE_SNAPSHOT_DATA = "isolate_snapshot_data";
private static final String DEFAULT_LIBRARY = "libflutter.so";
private static final String DEFAULT_KERNEL_BLOB = "kernel_blob.bin";
private static final String DEFAULT_FLUTTER_ASSETS_DIR = "flutter_assets";