Flutter 模块动态化初探
简介
Flutter : Flutter allows you to build beautiful native apps on iOS and Android from a single codebase. 具有跨平台, 高性能的优势.
Flutter 产物
产物流向:
通过分析我们可以发现
不变的产物有 flutter.jar ,libfluter.so,icudtl.dat, vm_snapshot_data , vm_snapshot_instr, 不跟业务代码相关, 只跟 flutter engine 的版本有关.
变化的产物: flutter_assets , isolate_snapshot_data , isolate_snapshot_instr 主要是业务的代码和资源.
LICENSE 没用, 可以删除.
总结:
我们通过将不变的产物集成到APK 中.将变化组成一个资源包,通过配置下发下来.
动态化改造
Flutter SDK 改造
修改 Flutter.createView()
方法.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22@NonNull
public static FlutterView createView(@NonNull final Context activity, @NonNull final Lifecycle lifecycle,
final String initialRoute, String bundlePath) {
Context context = activity;
if (!TextUtils.isEmpty(bundlePath)) {
final AssetManager bundleAsset = AssetManagerUtils.newAssetManager(bundlePath); // Flutter 组件资源包位置
context = new ContextWrapper(activity) {
@Override
public Resources getResources() {
return new Resources(bundleAsset,
super.getResources().getDisplayMetrics(), super.getResources().getConfiguration()) {
};
}
};
}
FlutterMain.startInitialization(context); // 使用自定义的 context
FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), null);
final FlutterNativeView nativeView = new FlutterNativeView(context);// 使用自定义的 context
...
return flutterView;
}
这里: 通过反射 AssetManger . 同时将 bundlePath 添加进去.
bundlePath 的作用:
- 用于查找 flutter_assets, bundleAsset 会在
FlutterNativeView.nativeRunBundleAndSnapshotFromLibrary()
传递给 Flutter 用于查找 flutter_assets 资源. - 用于 copy isolate_snapshot_data , isolate_snapshot_instr 资源.
Copy 资源是由 ResourceExtractor 完成的.
我们需要修改 ResourceExtractor.ExtractTask1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75private void extractResources() {
final File dataDir = new File(PathUtils.getDataDirectory(mContext));
final AssetManager bundleManger = mContext.getResources().getAssets();
//获取 Apk 自身的 AssetsManger
final AssetManager shareManager = mContext.getApplicationContext().getResources().getAssets();
final String fluterBundleVersion = getFluterBundleVersion(bundleManger);
if (fluterBundleVersion != null) {
Log.i(TAG, "delete cache fluterBundleVersion " + fluterBundleVersion);
deleteFiles();
}
AssetManager manager;
byte[] buffer = null;
for (String asset : mResources) {
try {
final File output = new File(dataDir, asset);
if (output.exists()) {
continue;
}
if (output.getParentFile() != null) {
output.getParentFile().mkdirs();
}
manager = bundleManger;
if (asset.startsWith("flutter_shared")
|| asset.equals("vm_snapshot_data")
|| asset.equals("vm_snapshot_instr")) {
manager = shareManager;
}
...
}
} catch (FileNotFoundException fnfe) {
continue;
} catch (IOException ioe) {
Log.w(TAG, "Exception unpacking resources: " + ioe.getMessage());
deleteFiles();
return;
}
}
if (fluterBundleVersion != null) {
try {
new File(dataDir, fluterBundleVersion).createNewFile();
} catch (IOException e) {
Log.w(TAG, "Failed to write resource timestamp");
}
}
}
private String getFluterBundleVersion(AssetManager bundleAssets) {
final File dataDir = new File(PathUtils.getDataDirectory(mContext));
String expectedTimestamp = null;
try {
InputStream in = bundleAssets.open("flutter_bundle_version");
expectedTimestamp = VERSION_PREFIX + IoUtils.toString(in);
} catch (IOException e) {
e.printStackTrace();
}
if (expectedTimestamp == null) {
return null;
}
final String[] existingTimestamps = getVersionStamps(dataDir);
if (existingTimestamps == null) {
return null;
}
if (existingTimestamps.length != 1
|| !expectedTimestamp.equals(existingTimestamps[0])) {
return expectedTimestamp;
}
return null;
}
主要的修改:
- 共享的资源由 APK 的 AssetsManger 中获取. 其他由 bundleManger 中获取
- 修改 ResourceExtractor 更新资源逻辑, 由本来的 res_timestamp- {versionCode} -{packageInfo.lastUpdateTime} 不同就更新改为 flutter_bundle_version 不同就更新.
打包插件修改:
组件资源插件
需要做的事情:
- 需要组合成一个APK, 因为需要被 AssetManger 识别.
- 不需要包含代码,
- android 资源尽可能少, 但是需要 AndroidManifest.xml
- flutter_bundle_version. 每次编译都应不同, 用于 ResourceExtractor 更新版本逻辑.
- flutter_engine_version 获取 flutter sdk 版本, 相同版本才能升级, 防止因为版本实现不同导致的 bug.
- flutter_bridge_version 是一个 md5. 是对所有自定义的
MethodChannel.MethodCallHandler
进行 ABI 格式化. 即 APK 具有一样的扩展能力.相同版本才能升级. - 保留 isolate_snapshot_data, isolate_snapshot_instr 文件.
lib 插件
Flutter 模块 一般情况我们是以 aar 的形式依赖. 需要修改上传到 maven 的AAR 如下. - 保留 vm_snapshot_data, vm_snapshot_instr 文件.
- 保留 flutter_bundle_version ,flutter_engine_version,flutter_bridge_version 作用如上.
flutter.gradle 修改
- copy {flutter sdk}\flutter\packages\flutter_tools\gradle\flutter.gradle
- 修改 release 依赖为自定义的 flutter jar.
尾巴
更详情的分析留在后面。