温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Flutter初始化流程是怎样的

发布时间:2022-01-11 16:51:00 来源:亿速云 阅读:120 作者:iii 栏目:开发技术

本篇内容介绍了“Flutter初始化流程是怎样的”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Flutter初始化时序

Flutter初始化主要分四部分,FlutterMain初始化、FlutterNativeView的初始化、FlutterView初始化和Flutter Bundle初始化。

  • PlatformViewAndroid::PlatformViewAndroid()
        : PlatformView(std::make_unique<NullRasterizer>()),
          android_surface_(InitializePlatformSurface()) {}
    void PlatformViewAndroid::Attach() {
      CreateEngine();
      // Eagerly setup the IO thread context. We have already setup the surface.
      SetupResourceContextOnIOThread();
      UpdateThreadPriorities();
  • 其中:
    1. PlatformViewAndroid的构造函数主要是调用了InitializePlatformSurface方法,这个方法主要是初始化了Surface,其中Surface有Vulkan、OpenGL和Software三种类型的区别。
    2. PlatformViewAndroid::Attach方法这里主要调用三个方法:CreateEngine、SetupResourceContextOnIOThread和UpdateThreadPriorities。
    2.1 CreateEngine比较好理解,创建Engine,这里会重新创建一个Engine对象。
    2.2 SetupResourceContextOnIOThread是在IO线程去准备资源的上下文逻辑。
    2.3 UpdateThreadPriorities是设置线程优先级,这设置GPU线程优先级为-2,UI线程优先级为-1。

    FlutterView初始化

    FlutterView的初始化就是纯粹的Android层啦,所以相对比较简单。分析FlutterView.java的构造函数就会发现,整个FlutterView的初始化在确保FlutterNativeView的创建成功和一些必要的view设置之外,主要做了两件事:
    1. 注册SurfaceHolder监听,其中surfaceCreated回调会作为Flutter的第一帧回调使用。
    2. 初始化了Flutter系统需要用到的一系列桥接方法。例如:localization、navigation、keyevent、system、settings、platform、textinput。
    FlutterView初始化流程主要如下图所示:

    Flutter初始化流程是怎样的


    Flutter Bundle初始化


    Flutter Bundle的初始化是由调用FlutterActivityDelegate.runFlutterBundle开始的

    我们再从源码角度较为深入了解下:

    FlutterActivity的onCreate方法在执行完FlutterActivityDelegate的onCreate方法之后会调用它的runFlutterBundle方法。runFlutterBundle代码如下:

    public void runFlutterBundle(){
        // other codes ...
        String appBundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
        if (appBundlePath != null) {
            flutterView.runFromBundle(appBundlePath, null, "main", reuseIsolate);
        }
    }

    很明显,这个runFlutterBundle并没有做太多事情,而且直接调用了FlutterView.runFromBundle方法。而后兜兜转转最后会调用到PlatformViewAndroid::RunBundleAndSnapshot方法。

    void PlatformViewAndroid::RunBundleAndSnapshot(JNIEnv* env, std::string bundle_path,
                                                   std::string snapshot_override,
                                                   std::string entrypoint,
                                                   bool reuse_runtime_controller,
                                                   jobject assetManager) {
      // other codes ...
      blink::Threads::UI()->PostTask(
          [engine = engine_->GetWeakPtr(),
           asset_provider = std::move(asset_provider),
           bundle_path = std::move(bundle_path), entrypoint = std::move(entrypoint),
           reuse_runtime_controller = reuse_runtime_controller] {
            if (engine)
              engine->RunBundleWithAssets(
                  std::move(asset_provider), std::move(bundle_path),
                  std::move(entrypoint), reuse_runtime_controller);
          });
    }

    PlatformViewAndroid::RunBundleAndSnapshot在UI线程中调用Engine::RunBundleWithAssets,最终调用Engine::DoRunBundle。
    DoRunBundle方法最后只会调用RunFromPrecompiledSnapshot、RunFromKernel和RunFromScriptSnapshot三个方法中的一个。而这三个方法最终都会调用SendStartMessage方法。

    bool DartController::SendStartMessage(Dart_Handle root_library,
                                          const std::string& entrypoint) {
      // other codes ...
      // Get the closure of main().
      Dart_Handle main_closure = Dart_GetClosure(
          root_library, Dart_NewStringFromCString(entrypoint.c_str()));
      // other codes ...
      // Grab the 'dart:isolate' library.
      Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
      DART_CHECK_VALID(isolate_lib);
      // Send the start message containing the entry point by calling
      // _startMainIsolate in dart:isolate.
      const intptr_t kNumIsolateArgs = 2;
      Dart_Handle isolate_args[kNumIsolateArgs];
      isolate_args[0] = main_closure;
      isolate_args[1] = Dart_Null();
      Dart_Handle result = Dart_Invoke(isolate_lib, ToDart("_startMainIsolate"),
                                       kNumIsolateArgs, isolate_args);
      return LogIfError(result);
    }

    而SendStartMessage方法主要做了三件事:
    1. 获取Flutter入口方法(例如main方法)的closure。
    2. 获取FlutterLibrary。
    3. 发送消息来调用Flutter的入口方法。

“Flutter初始化流程是怎样的”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI