国产亚洲欧美人成在线,免费视频爱爱太爽了无码,日本免费一区二区三区高清视频 ,国产真实伦对白精彩视频

歡迎您光臨深圳塔燈網(wǎng)絡(luò)科技有限公司!
電話圖標 余先生:13699882642

網(wǎng)站百科

為您解碼網(wǎng)站建設(shè)的點點滴滴

深入解析 Flutter 初始化流程

發(fā)表日期:2019-01 文章編輯:小燈 瀏覽次數(shù):3220

在調(diào)研 Flutter 動態(tài)化方案的時候,需要了解 Flutter 加載 dart 產(chǎn)物的流程,于是梳理了一遍 FLutter 的初始化流程

flutter的源碼下載地址在 github 上可以找到,具體地址: github-flutter/engine

FLutterMain的初始化

先從 Android 的入口開始看

FlutterAppliationonCreate 中調(diào)用了

FlutterMain.startInitialization(this); 

跟進去我們會看到調(diào)用了 startInitialization 方法,最后會順序調(diào)用這幾個方法

initConfig(applicationContext); initAot(applicationContext); initResources(applicationContext); 

我們查看 initResources 方法如圖

這里我們可以看到實際加載了assets里面的flutter資源。并且會把資源 copy 到本地的?路徑。這里不做深究。 FlutterMan 的初始化基本包括了

  • 初始化配置
  • 初始化 AOT 編譯
  • 初始化資源

3 個部分

繼續(xù)看 ? FlutterView 的初始化:

FLutterView的初始化

FlutterActivity 為例,在 onCreate 中會調(diào)用到 FlutterActivityDelegate 的對應(yīng)方法,最終調(diào)用 FlutterViewrunFromBundle 方法

public void runFromBundle(FlutterRunArguments args) { this.assertAttached(); this.preRun(); this.mNativeView.runFromBundle(args); this.postRun(); } 

跟蹤這段代碼,會調(diào)用 FlutterNativeViewnativeRunBundleAndSnapshotFromLibrary 方法。

這里會繼續(xù)進行 ? jni 層的調(diào)用,?查看 platform_view_android_jni.cc

{ .name = "nativeRunBundleAndSnapshotFromLibrary", .signature = "(J[Ljava/lang/String; Ljava/lang/String;" "Ljava/lang/String;Landroid/content/res/AssetManager;)V", .fnPtr = reinterpret_cast<void*>(shell::RunBundleAndSnapshotFromLibrary), }, 

查看 RunBundleAndSnapshotFromLibrary ,這里刪除了一些我們不關(guān)心的邏輯

static void RunBundleAndSnapshotFromLibrary(JNIEnv* env, jobject jcaller, jlong shell_holder, jobjectArray jbundlepaths, jstring jEntrypoint, jstring jLibraryUrl, jobject jAssetManager) { auto asset_manager = std::make_shared<blink::AssetManager>();for (const auto& bundlepath :fml::jni::StringArrayToVector(env, jbundlepaths)) { const auto file_ext_index = bundlepath.rfind("."); if (bundlepath.substr(file_ext_index) == ".zip") { asset_manager->PushBack( std::make_unique<blink::ZipAssetStore>(bundlepath)); } else { asset_manager->PushBack( std::make_unique<blink::DirectoryAssetBundle>(fml::OpenDirectory( bundlepath.c_str(), false, fml::FilePermission::kRead))); const auto last_slash_index = bundlepath.rfind("/", bundlepath.size()); if (last_slash_index != std::string::npos) { auto apk_asset_dir = bundlepath.substr( last_slash_index + 1, bundlepath.size() - last_slash_index);asset_manager->PushBack(std::make_unique<blink::APKAssetProvider>( env, // jni environment jAssetManager, // asset manager std::move(apk_asset_dir))// apk asset dir ); } } } auto isolate_configuration = CreateIsolateConfiguration(*asset_manager); RunConfiguration config(std::move(isolate_configuration), std::move(asset_manager)); ANDROID_SHELL_HOLDER->Launch(std::move(config)); 

首先會對資源路徑進行處理 會?分為 zip 包或者文件夾進行分別處理。最終會調(diào)用常量 ANDROID_SHELL_HOLDERLaunch 函數(shù).

最終走到 engineRun 函數(shù)。

這里有 2 個函數(shù)比較重要,先是 IsolateConfiguration::PrepareIsolate , 然后是 RunFromLibrary 或者 Run 函數(shù)

跟到 PrepareAndLaunchIsolate 函數(shù),查看源碼

bool IsolateConfiguration::PrepareIsolate(blink::DartIsolate& isolate) { if (isolate.GetPhase() != blink::DartIsolate::Phase::LibrariesSetup) { FML_DLOG(ERROR) << "Isolate was in incorrect phase to be prepared for running."; return false; }return DoPrepareIsolate(isolate); } 

而有 DoPrepareIsolate 函數(shù)的類 Configuration 類有3個

  • AppSnapshotIsolateConfiguration
  • KernelIsolateConfiguration
  • KernelListIsolateConfiguration

他們分別會調(diào)用 DartIsolate

  • PrepareForRunningFromPrecompiledCode
  • PrepareForRunningFromKernel

這2個方法的一個,可以?看到這里的 prepare 操作分成了 預先加載的代碼從內(nèi)核獲取 2種

至于 RunFromLibrary 函數(shù)和 Run 函數(shù)

我們能看到?他們最終都會調(diào)用 dart:isolate_startMainIsolate 的邏輯:

Dart_Handle isolate_lib = Dart_LookupLibrary(tonic::ToDart("dart:isolate")); if (tonic::LogIfError(Dart_Invoke( isolate_lib, tonic::ToDart("_startMainIsolate"), sizeof(isolate_args) / sizeof(isolate_args[0]), isolate_args))) { return false; } 

這里說明我們正在執(zhí)行調(diào)用 Dart 的入口方法。而 RunRunFromLibrary 的區(qū)別,則是如果我們傳入了 entrypoint 參數(shù)去進行 Flutter 的 bundle 初始化的時候,則會去加載我們制定的 library。

小結(jié)

到這里, Flutter 的初始化流程就就簡單的分析了一遍。大致可以總結(jié)成三個部分

  1. 初始化 FlutterMain
  2. 初始化 FlutterView,開始加載 bundle
  3. 初始化Flutter Bundle,這里獲取了 Flutter 的入口方法、Flutter 的 library, 以及對 Flutter 入口方法的調(diào)用。

初始化的邏輯比較復雜,對后續(xù)一些初始化相關(guān)的性能優(yōu)化應(yīng)該也會有不小的啟發(fā)。 FlutterMain 中對資源的處理和寫入本地的邏輯也給 Android 端研究 Flutter 動態(tài)化提供了基礎(chǔ)。

有需要Android進階全面系統(tǒng)視頻資料的可以加入Android進階交流群;701740775。免費獲取加群請備注 領(lǐng)取進階資料


本頁內(nèi)容由塔燈網(wǎng)絡(luò)科技有限公司通過網(wǎng)絡(luò)收集編輯所得,所有資料僅供用戶學習參考,本站不擁有所有權(quán),如您認為本網(wǎng)頁中由涉嫌抄襲的內(nèi)容,請及時與我們聯(lián)系,并提供相關(guān)證據(jù),工作人員會在5工作日內(nèi)聯(lián)系您,一經(jīng)查實,本站立刻刪除侵權(quán)內(nèi)容。本文鏈接:http://jstctz.cn/18047.html
相關(guān)APP開發(fā)