簡(jiǎn)介
Flutter : Flutter allows you to build beautiful native apps on iOS and Android from a single codebase. 具有跨平臺(tái), 高性能的優(yōu)勢(shì).
Flutter 產(chǎn)物
Flutter 產(chǎn)物
產(chǎn)物流向:
產(chǎn)物流向
通過分析我們可以發(fā)現(xiàn)
不變的產(chǎn)物有 flutter.jar ,libfluter.so,icudtl.dat, vm_snapshot_data , vm_snapshot_instr, 不跟業(yè)務(wù)代碼相關(guān), 只跟 flutter engine 的版本有關(guān).
變化的產(chǎn)物: flutter_assets , isolate_snapshot_data , isolate_snapshot_instr 主要是業(yè)務(wù)的代碼和資源.
LICENSE 沒用, 可以刪除.
總結(jié):
我們通過將不變的產(chǎn)物集成到APK 中.將變化組成一個(gè)資源包,通過配置下發(fā)下來.
動(dòng)態(tài)化改造
Flutter SDK 改造
修改 Flutter.createView()
方法.
@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 . 同時(shí)將 bundlePath 添加進(jìn)去.
bundlePath 的作用:
- 用于查找 flutter_assets, bundleAsset 會(huì)在
FlutterNativeView.nativeRunBundleAndSnapshotFromLibrary()
傳遞給 Flutter 用于查找 flutter_assets 資源. - 用于 copy isolate_snapshot_data , isolate_snapshot_instr 資源.
Copy 資源是由 ResourceExtractor 完成的.
我們需要修改 ResourceExtractor.ExtractTask
private 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 = getFlutterBundleVersion(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 getFlutterBundleVersion(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 不同就更新.
打包插件修改:
組件資源插件
image.png
需要做的事情:
- 需要組合成一個(gè)APK, 因?yàn)樾枰?AssetManger 識(shí)別.
- 不需要包含代碼,
- android 資源盡可能少, 但是需要 AndroidManifest.xml
- flutter_bundle_version. 每次編譯都應(yīng)不同, 用于 ResourceExtractor 更新版本邏輯.
- flutter_engine_version 獲取 flutter sdk 版本, 相同版本才能升級(jí), 防止因?yàn)榘姹緦?shí)現(xiàn)不同導(dǎo)致的 bug.
- flutter_bridge_version 是一個(gè) md5. 是對(duì)所有自定義的
MethodChannel.MethodCallHandler
進(jìn)行 ABI 格式化. 即 APK 具有一樣的擴(kuò)展能力.相同版本才能升級(jí). - 保留 isolate_snapshot_data, isolate_snapshot_instr 文件.
lib 插件
Flutter 模塊 一般情況我們是以 aar 的形式依賴. 需要修改上傳到 maven 的AAR 如下.
image.png
- 保留 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.