不論是創(chuàng)建完全的 Flutter項(xiàng)目,還是 Native以 Moudle的方式集成 Flutter,亦或是 Native以 aar方式集成 Flutter,最終 Flutter在 Andorid端的 App 都是以 Native項(xiàng)目+ Flutter 的UI產(chǎn)物存在的。所以在這里拆開一個(gè) Flutter在 release模式下編譯后生成 aar包來做分析:
我們關(guān)注重點(diǎn)在 assets,jni,libs 這 3 個(gè)目錄中,其他的文件都是 Nactive層殼工程的產(chǎn)物。
jni :該目錄下存在文件 libflutter.so,該文件為 Flutter Engine (引擎) 層的 C++實(shí)現(xiàn),提供skia(繪制引擎),Dart,Text(紋理繪制)等支持。
libs:該目錄下存在文件為 flutter.jar,該文件為 Flutter embedding (嵌入) 層的 Java實(shí)現(xiàn),該層提供給 Flutter 許多Native層平臺(tái)系統(tǒng)功能的支持,比如創(chuàng)建線程。
assets:該目錄下分為兩部分:
代碼探究
在我們的 Native 項(xiàng)目中,會(huì)在 FlutterMainActivity 中,通過調(diào)用 Flutter 這個(gè)類來創(chuàng)建 View:
flutterView = Flutter.createView(this, getLifecycle(), route);
layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
addContentView(flutterView, layoutParams);
查看 Flutter 類代碼,發(fā)現(xiàn) Flutter 類主要做了幾件事:
public static FlutterView createView(@NonNull final Activity activity, @NonNull Lifecycle lifecycle, String initialRoute) {
FlutterMain.startInitialization(activity.getApplicationContext());
FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), (String[])null);
FlutterNativeView nativeView = new FlutterNativeView(activity);
所以,真正初始化的相關(guān)代碼是在 FlutterMian 中:
所以,真正初始化的相關(guān)代碼是在 FlutterMian 中:
public static void startInitialization(Context applicationContext, FlutterMain.Settings settings) {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("startInitialization must be called on the main thread");
} else if (sSettings == null) {
sSettings = settings;
long initStartTimestampMillis = SystemClock.uptimeMillis();
initConfig(applicationContext);
initAot(applicationContext);
initResources(applicationContext);
System.loadLibrary("flutter");
long initTimeMillis = SystemClock.uptimeMillis() - initStartTimestampMillis;
nativeRecordStartTimestamp(initTimeMillis);
}
}
在 startInitialization 中,主要執(zhí)行了三個(gè)初始化方法 initConfig(applicationContext),initAot(applicationContext),initResources(applicationContext),最后記錄了執(zhí)行時(shí)間。
在 initConfig 中:
private static void initConfig(Context applicationContext) {
try {
Bundle metadata = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 128).metaData;
if (metadata != null) {
sAotSharedLibraryPath = metadata.getString(PUBLIC_AOT_AOT_SHARED_LIBRARY_PATH, "app.so");
sAotVmSnapshotData = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_DATA_KEY, "vm_snapshot_data");
sAotVmSnapshotInstr = metadata.getString(PUBLIC_AOT_VM_SNAPSHOT_INSTR_KEY, "vm_snapshot_instr");
sAotIsolateSnapshotData = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_DATA_KEY, "isolate_snapshot_data");
sAotIsolateSnapshotInstr = metadata.getString(PUBLIC_AOT_ISOLATE_SNAPSHOT_INSTR_KEY, "isolate_snapshot_instr");
sFlx = metadata.getString(PUBLIC_FLX_KEY, "app.flx");
sFlutterAssetsDir = metadata.getString(PUBLIC_FLUTTER_ASSETS_DIR_KEY, "flutter_assets");
}
} catch (NameNotFoundException var2) {
throw new RuntimeException(var2);
}
}
在 initResources 中:
sResourceExtractor = new ResourceExtractor(applicationContext);
sResourceExtractor.addResource(fromFlutterAssets(sFlx)).addResource(fromFlutterAssets(sAotVmSnapshotData)).addResource(fromFlutterAssets(sAotVmSnapshotInstr)).addResource(fromFlutterAssets(sAotIsolateSnapshotData)).addResource(fromFlutterAssets(sAotIsolateSnapshotInstr)).addResource(fromFlutterAssets("kernel_blob.bin"));
if (sIsPrecompiledAsSharedLibrary) {
sResourceExtractor.addResource(sAotSharedLibraryPath);
} else {
sResourceExtractor.addResource(sAotVmSnapshotData).addResource(sAotVmSnapshotInstr).addResource(sAotIsolateSnapshotData).addResource(sAotIsolateSnapshotInstr);
}
sResourceExtractor.start();
在 ResourceExtractor 類中,通過名字就能知道這個(gè)類是做資源提取的。把 add 的 Flutter 相關(guān)文件從 assets 目錄中取出來,該類中 ExtractTask 的 doInBackground 方法中:
File dataDir = new File(PathUtils.getDataDirectory(ResourceExtractor.this.mContext))
這句話指定了資源提取的目的地,即 data/data/包名/app_flutter,如下:
image
如圖,可以看到該目錄是的訪問權(quán)限是可讀可寫,所以理論上,我們只要把自己的 Flutter 產(chǎn)物下載后,從內(nèi)存 copy 到這里,便能夠?qū)崿F(xiàn)代碼的動(dòng)態(tài)更新。
代碼實(shí)現(xiàn)
public class FlutterUtils {
private static String TAG = "FlutterUtils.class";
private static String flutterZipName = "flutter-code.zip";
private static String fileSuffix = ".zip";
private static String zipPath = Environment.getExternalStorageDirectory().getPath() + "/k12/" + flutterZipName;
private static String targetDirPath = zipPath.replace(fileSuffix, "");
private static String targetDirDataPath = zipPath.replace(fileSuffix, "/data");
/**
* Flutter 代碼熱更新第一步: 解壓 Flutter 的壓縮文件
*/
public static void unZipFlutterFile() {
Log.i(TAG, "unZipFile: Start");
try {
unZipFile(zipPath, targetDirPath);
Log.i(TAG, "unZipFile: Finish");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Flutter 代碼熱更新第二步: 將 Flutter 的相關(guān)文件移動(dòng)到 AppData 的相關(guān)目錄,APP啟動(dòng)時(shí)調(diào)用
*
* @param mContext 獲取 AppData 目錄需要
*/
public static void copyDataToFlutterAssets(Context mContext) {
String appDataDirPath = PathUtils.getDataDirectory(mContext.getApplicationContext()) + File.separator;
Log.d(TAG, "copyDataToFlutterAssets-filesDirPath:" + targetDirDataPath);
Log.d(TAG, "copyDataToFlutterAssets-appDataDirPath:" + appDataDirPath);
File appDataDirFile = new File(appDataDirPath);
File filesDirFile = new File(targetDirDataPath);
File[] files = filesDirFile.listFiles();
for (File srcFile : files) {
if (srcFile.getPath().contains("isolate_snapshot_data")
|| srcFile.getPath().contains("isolate_snapshot_instr")
|| srcFile.getPath().contains("vm_snapshot_data")
|| srcFile.getPath().contains("vm_snapshot_instr")) {
File targetFile = new File(appDataDirFile + "/" + srcFile.getName());
FileUtil.copyFileByFileChannels(srcFile, targetFile);
Log.i(TAG, "copyDataToFlutterAssets-copyFile:" + srcFile.getPath());
}
}
Log.i(TAG, "copyDataToFlutterAssets: Finish");
}
/**
* 解壓縮文件到指定目錄
*
* @param zipFileString 壓縮文件路徑
* @param outPathString 目標(biāo)路徑
* @throws Exception
*/
private static void unZipFile(String zipFileString, String outPathString) {
try {
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else {
File file = new File(outPathString + File.separator + szName);
if (!file.exists()) {
Log.d(TAG, "Create the file:" + outPathString + File.separator + szName);
file.getParentFile().mkdirs();
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
while ((len = inZip.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
} catch (Exception e) {
Log.i(TAG,e.getMessage());
e.printStackTrace();
}
}
/**
* 使用FileChannels復(fù)制文件。
*
* @param source 原路徑
* @param dest 目標(biāo)路徑
*/
public static void copyFileByFileChannels(File source, File dest) {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
refreshMedia(BaseApplication.getBaseApplication(), dest);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputChannel.close();
outputChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 更新媒體庫
*
* @param cxt
* @param files
*/
public static void refreshMedia(Context cxt, File... files) {
for (File file : files) {
String filePath = file.getAbsolutePath();
refreshMedia(cxt, filePath);
}
}
public static void refreshMedia(Context cxt, String... filePaths) {
MediaScannerConnection.scanFile(cxt.getApplicationContext(),
filePaths, null,
null);
}
}
我們的App安裝到手機(jī)上后,是很難再修改 Assets 目錄下的資源,所以關(guān)于資源的替換,目前的方案是使用 Flutter 的 API :Image.file() 來從存儲(chǔ)卡中讀取圖片。
通常我們的 Flutter 項(xiàng)目中應(yīng)當(dāng)存有關(guān)于 App 的圖片,盡量保證在熱更新的時(shí)候使用已經(jīng)存在的圖片。
其次,我們可以使用 Image.network() 來加載網(wǎng)絡(luò)資源的圖片,如果還不能滿足需求,兜底的方案就是使用 Image.file(),將資源圖片放到Zip目錄下一起下發(fā),并在Flutter代碼中使用 Image.file() 來加載。
new File(dataDir + 'hotupdate_test.png').existsSync()? Image.file(new File(dataDir + 'hotupdate_test.png')): Image.asset("images/net_error.png"),
在 Flutter 代碼產(chǎn)物替換中,因?yàn)樘鎿Q的 4 個(gè)文件皆為直接加載到內(nèi)存中的引擎代碼,所以這部分優(yōu)化空間有限。但在資源的熱更新中,資源是從Assets取得,所以這里應(yīng)該有更優(yōu)的方案。
Flutter 的熱更新意味著可以實(shí)在App的一個(gè)入口里,像 H5 一樣無窮的嵌入頁面,但又有和原生媲美的流暢體驗(yàn)。
未來 Flutter 熱更新技術(shù)如果成熟,應(yīng)用開發(fā)可能只需要 Android端和 IOS端實(shí)現(xiàn)本地業(yè)務(wù)功能模塊的封裝,業(yè)務(wù)和UI的代碼都放在 Flutter 中,便能夠真正的實(shí)現(xiàn)移動(dòng)兩端一份業(yè)務(wù)代碼,并且賦予產(chǎn)品在不影響用戶體驗(yàn)的情況下,擁有動(dòng)態(tài)部署APP內(nèi)容的能力。