Flutter 安卓平台启动及初始化

image.png

flutter:跨平台(Android/iOS),图形引擎,Dart语言 这些都有什么关系呢
我们先看下在安卓平台下的初始化:
入口:manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flutter_app">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="flutter_app"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

我们看到没有 Application,只有一个 MainActivity,还有一堆 meta-data
接下来看下 MainActivity 做了什么

class MainActivity: FlutterActivity() {}

发现只是继承了 io.flutter 包下的FlutterActivity

public class FlutterActivity extends Activity implements Host, LifecycleOwner {

继承了 android.app.Activity 实现了LifecycleOwner 通过 LifecycleRegistry(this); 实现了一套 Lifecycler ,还有个 Host 接口 来扩展 Flutter 相关的功能,那么Flutter 引擎如何初始化的呢?发现 FlutterActivity 的 onCreate 执行了一些相关逻辑

protected void onCreate(@Nullable Bundle savedInstanceState) {
        this.switchLaunchThemeForNormalTheme();
        super.onCreate(savedInstanceState);
        this.lifecycle.handleLifecycleEvent(Event.ON_CREATE);
        this.delegate = new FlutterActivityAndFragmentDelegate(this);
        this.delegate.onAttach(this);
        this.delegate.onActivityCreated(savedInstanceState);
        this.configureWindowForTransparency();
        this.setContentView(this.createFlutterView());
        this.configureStatusBarForFullscreenFlutterExperience();
    }

1: 创建了 FlutterActivityAndFragmentDelegate 并 onAttach
2: 创建 FlutterView 通过 setContentView 添加到 Window
先看下第一步

void onAttach(@NonNull Context context) {
        this.ensureAlive();
        if (this.flutterEngine == null) {
            this.setupFlutterEngine();
        }
        this.platformPlugin = this.host.providePlatformPlugin(this.host.getActivity(), this.flutterEngine);
        if (this.host.shouldAttachEngineToActivity()) {
            Log.v("FlutterActivityAndFragmentDelegate", "Attaching FlutterEngine to the Activity that owns this Fragment.");
            this.flutterEngine.getActivityControlSurface().attachToActivity(this.host.getActivity(), this.host.getLifecycle());
        }
        this.host.configureFlutterEngine(this.flutterEngine);
    }

void setupFlutterEngine() {
        Log.v("FlutterActivityAndFragmentDelegate", "Setting up FlutterEngine.");
        String cachedEngineId = this.host.getCachedEngineId();
        if (cachedEngineId != null) {
            this.flutterEngine = FlutterEngineCache.getInstance().get(cachedEngineId);
            this.isFlutterEngineFromHost = true;
            if (this.flutterEngine == null) {
                throw new IllegalStateException("The requested cached FlutterEngine did not exist in the FlutterEngineCache: '" + cachedEngineId + "'");
            }
        } else {
            this.flutterEngine = this.host.provideFlutterEngine(this.host.getContext());
            if (this.flutterEngine != null) {
                this.isFlutterEngineFromHost = true;
            } else {
                Log.v("FlutterActivityAndFragmentDelegate", "No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.");
                this.flutterEngine = new FlutterEngine(this.host.getContext(), this.host.getFlutterShellArgs().toArray(), false, this.host.shouldRestoreAndSaveState());
                this.isFlutterEngineFromHost = false;
            }
        }
    }

setupFlutterEngin 方法去创建了 FlutterEngine 对象,这个对象是做什么到呢,我们看它到构造

public FlutterEngine(@NonNull Context context, @NonNull FlutterLoader flutterLoader, @NonNull FlutterJNI flutterJNI, @NonNull PlatformViewsController platformViewsController, @Nullable String[] dartVmArgs, boolean automaticallyRegisterPlugins, boolean waitForRestorationData) {
        this.engineLifecycleListeners = new HashSet();
        this.engineLifecycleListener = new FlutterEngine.EngineLifecycleListener() {
            public void onPreEngineRestart() {
                Log.v("FlutterEngine", "onPreEngineRestart()");
                Iterator var1 = FlutterEngine.this.engineLifecycleListeners.iterator();

                while(var1.hasNext()) {
                    FlutterEngine.EngineLifecycleListener lifecycleListener = (FlutterEngine.EngineLifecycleListener)var1.next();
                    lifecycleListener.onPreEngineRestart();
                }

                FlutterEngine.this.platformViewsController.onPreEngineRestart();
                FlutterEngine.this.restorationChannel.clearData();
            }
        };
        this.dartExecutor = new DartExecutor(flutterJNI, context.getAssets());
        this.dartExecutor.onAttachedToJNI();
        this.accessibilityChannel = new AccessibilityChannel(this.dartExecutor, flutterJNI);
        this.keyEventChannel = new KeyEventChannel(this.dartExecutor);
        this.lifecycleChannel = new LifecycleChannel(this.dartExecutor);
        this.localizationChannel = new LocalizationChannel(this.dartExecutor);
        this.mouseCursorChannel = new MouseCursorChannel(this.dartExecutor);
        this.navigationChannel = new NavigationChannel(this.dartExecutor);
        this.platformChannel = new PlatformChannel(this.dartExecutor);
        this.restorationChannel = new RestorationChannel(this.dartExecutor, waitForRestorationData);
        this.settingsChannel = new SettingsChannel(this.dartExecutor);
        this.systemChannel = new SystemChannel(this.dartExecutor);
        this.textInputChannel = new TextInputChannel(this.dartExecutor);
        this.localizationPlugin = new LocalizationPlugin(context, this.localizationChannel);
        this.flutterJNI = flutterJNI;
        flutterLoader.startInitialization(context.getApplicationContext());
        flutterLoader.ensureInitializationComplete(context, dartVmArgs);
        flutterJNI.addEngineLifecycleListener(this.engineLifecycleListener);
        flutterJNI.setPlatformViewsController(platformViewsController);
        flutterJNI.setLocalizationPlugin(this.localizationPlugin);
        this.attachToJni();
        this.renderer = new FlutterRenderer(flutterJNI);
        this.platformViewsController = platformViewsController;
        this.platformViewsController.onAttachedToJNI();
        this.pluginRegistry = new FlutterEnginePluginRegistry(context.getApplicationContext(), this, flutterLoader);
        if (automaticallyRegisterPlugins) {
            this.registerPlugins();
        }
    }

监听了引擎到变化,初始化了一些系统基础 Channel(Dart通道) , FlutterLoader 对象 start ,flutterJNI 对象初始化,FlutterRender及FlutterView初始化,registerPlugins(插件初始化)
先看下 FlutterLoader 的 startInitialization 方法吧 ensureInitializationComplete,这个是整个 Flutter引擎 初始化的基础:
startInitialization 方法

public void startInitialization(@NonNull Context applicationContext, @NonNull FlutterLoader.Settings settings) {
        if (this.settings == null) {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                throw new IllegalStateException("startInitialization must be called on the main thread");
            } else {
                final Context appContext = applicationContext.getApplicationContext();
                this.settings = settings;
                this.initStartTimestampMillis = SystemClock.uptimeMillis();
                this.initConfig(appContext);
                VsyncWaiter.getInstance((WindowManager)appContext.getSystemService("window")).init();
                Callable<FlutterLoader.InitResult> initTask = new Callable<FlutterLoader.InitResult>() {
                    public FlutterLoader.InitResult call() {
                        ResourceExtractor resourceExtractor = FlutterLoader.this.initResources(appContext);
                        System.loadLibrary("flutter");
                        Executors.newSingleThreadExecutor().execute(new Runnable() {
                            public void run() {
                                FlutterJNI.nativePrefetchDefaultFontManager();
                            }
                        });
                        if (resourceExtractor != null) {
                            resourceExtractor.waitForCompletion();
                        }

                        return new FlutterLoader.InitResult(PathUtils.getFilesDir(appContext), PathUtils.getCacheDirectory(appContext), PathUtils.getDataDirectory(appContext));
                    }
                };
                this.initResultFuture = Executors.newSingleThreadExecutor().submit(initTask);
            }
        }
    }

初始化了 flutter 的 c 库,并将 安卓系统的 vsync 同步到 flutter

public void init() {
        FlutterJNI.setAsyncWaitForVsyncDelegate(this.asyncWaitForVsyncDelegate);
        float fps = this.windowManager.getDefaultDisplay().getRefreshRate();
        FlutterJNI.setRefreshRateFPS(fps);
    }

private final AsyncWaitForVsyncDelegate asyncWaitForVsyncDelegate = new AsyncWaitForVsyncDelegate() {
        public void asyncWaitForVsync(final long cookie) {
            Choreographer.getInstance().postFrameCallback(new FrameCallback() {
                public void doFrame(long frameTimeNanos) {
                    float fps = VsyncWaiter.this.windowManager.getDefaultDisplay().getRefreshRate();
                    long refreshPeriodNanos = (long)(1.0E9D / (double)fps);
                    FlutterJNI.nativeOnVsync(frameTimeNanos, frameTimeNanos + refreshPeriodNanos, cookie);
                }
            });
        }
    };

ensureInitializationComplete方法

public void ensureInitializationComplete(@NonNull Context applicationContext, @Nullable String[] args) {
        if (!this.initialized) {
            if (Looper.myLooper() != Looper.getMainLooper()) {
                throw new IllegalStateException("ensureInitializationComplete must be called on the main thread");
            } else if (this.settings == null) {
                throw new IllegalStateException("ensureInitializationComplete must be called after startInitialization");
            } else {
                try {
                    FlutterLoader.InitResult result = (FlutterLoader.InitResult)this.initResultFuture.get();
                    List<String> shellArgs = new ArrayList();
                    shellArgs.add("--icu-symbol-prefix=_binary_icudtl_dat");
                    ApplicationInfo applicationInfo = this.getApplicationInfo(applicationContext);
                    shellArgs.add("--icu-native-lib-path=" + applicationInfo.nativeLibraryDir + File.separator + "libflutter.so");
                    if (args != null) {
                        Collections.addAll(shellArgs, args);
                    }

                    String kernelPath = null;
                    String snapshotAssetPath = result.dataDirPath + File.separator + this.flutterAssetsDir;
                    kernelPath = snapshotAssetPath + File.separator + "kernel_blob.bin";
                    shellArgs.add("--snapshot-asset-path=" + snapshotAssetPath);
                    shellArgs.add("--vm-snapshot-data=" + this.vmSnapshotData);
                    shellArgs.add("--isolate-snapshot-data=" + this.isolateSnapshotData);
                    shellArgs.add("--cache-dir-path=" + result.engineCachesPath);
                    if (this.settings.getLogTag() != null) {
                        shellArgs.add("--log-tag=" + this.settings.getLogTag());
                    }

                    long initTimeMillis = SystemClock.uptimeMillis() - this.initStartTimestampMillis;
                    Bundle bundle = applicationInfo.metaData;
                    if (bundle != null) {
                        boolean use_embedded_view = bundle.getBoolean("io.flutter.embedded_views_preview");
                        if (use_embedded_view) {
                            shellArgs.add("--use-embedded-view");
                        }
                    }

                    FlutterJNI.nativeInit(applicationContext, (String[])shellArgs.toArray(new String[0]), kernelPath, result.appStoragePath, result.engineCachesPath, initTimeMillis);
                    this.initialized = true;
                } catch (Exception var11) {
                    Log.e("FlutterLoader", "Flutter initialization failed.", var11);
                    throw new RuntimeException(var11);
                }
            }
        }
    }

加载 assets 下的资源, vm_snapshot_data 和 isolate_snapshot_data 并调用Nactive 方法 FlutterJNI.nativeInit 交给 Flutter 引擎

再回到 FlutterActivityAndFragmentDelegate 我们看下在 setContentView FlutterView 的创建:

View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.v("FlutterActivityAndFragmentDelegate", "Creating FlutterView.");
        this.ensureAlive();
        if (this.host.getRenderMode() == RenderMode.surface) {
            FlutterSurfaceView flutterSurfaceView = new FlutterSurfaceView(this.host.getActivity(), this.host.getTransparencyMode() == TransparencyMode.transparent);
            this.host.onFlutterSurfaceViewCreated(flutterSurfaceView);
            this.flutterView = new FlutterView(this.host.getActivity(), flutterSurfaceView);
        } else {
            FlutterTextureView flutterTextureView = new FlutterTextureView(this.host.getActivity());
            this.host.onFlutterTextureViewCreated(flutterTextureView);
            this.flutterView = new FlutterView(this.host.getActivity(), flutterTextureView);
        }

        this.flutterView.addOnFirstFrameRenderedListener(this.flutterUiDisplayListener);
        this.flutterSplashView = new FlutterSplashView(this.host.getContext());
        if (VERSION.SDK_INT >= 17) {
            this.flutterSplashView.setId(View.generateViewId());
        } else {
            this.flutterSplashView.setId(486947586);
        }

        this.flutterSplashView.displayFlutterViewWithSplash(this.flutterView, this.host.provideSplashScreen());
        Log.v("FlutterActivityAndFragmentDelegate", "Attaching FlutterEngine to FlutterView.");
        this.flutterView.attachToFlutterEngine(this.flutterEngine);
        return this.flutterSplashView;
    }

我们看到 contentView 是 FlutterSplashView 并调用了 displayFlutterViewWithSplash 方法

final class FlutterSplashView extends FrameLayout {
  
  public FlutterSplashView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.flutterEngineAttachmentListener = new FlutterEngineAttachmentListener() {
            public void onFlutterEngineAttachedToFlutterView(@NonNull FlutterEngine engine) {
                FlutterSplashView.this.flutterView.removeFlutterEngineAttachmentListener(this);
                FlutterSplashView.this.displayFlutterViewWithSplash(FlutterSplashView.this.flutterView, FlutterSplashView.this.splashScreen);
            }

            public void onFlutterEngineDetachedFromFlutterView() {
            }
        };
  ......
  public void displayFlutterViewWithSplash(@NonNull FlutterView flutterView, @Nullable SplashScreen splashScreen) {
        if (this.flutterView != null) {
            this.flutterView.removeOnFirstFrameRenderedListener(this.flutterUiDisplayListener);
            this.removeView(this.flutterView);
        }

        if (this.splashScreenView != null) {
            this.removeView(this.splashScreenView);
        }

        this.flutterView = flutterView;
        this.addView(flutterView);
        this.splashScreen = splashScreen;
        if (splashScreen != null) {
            if (this.isSplashScreenNeededNow()) {
                Log.v(TAG, "Showing splash screen UI.");
                this.splashScreenView = splashScreen.createSplashView(this.getContext(), this.splashScreenState);
                this.addView(this.splashScreenView);
                flutterView.addOnFirstFrameRenderedListener(this.flutterUiDisplayListener);
            } else if (this.isSplashScreenTransitionNeededNow()) {
                Log.v(TAG, "Showing an immediate splash transition to Flutter due to previously interrupted transition.");
                this.splashScreenView = splashScreen.createSplashView(this.getContext(), this.splashScreenState);
                this.addView(this.splashScreenView);
                this.transitionToFlutter();
            } else if (!flutterView.isAttachedToFlutterEngine()) {
                Log.v(TAG, "FlutterView is not yet attached to a FlutterEngine. Showing nothing until a FlutterEngine is attached.");
                flutterView.addFlutterEngineAttachmentListener(this.flutterEngineAttachmentListener);
            }
        }

    }

将 flutterView 和 SplashScreen(一个metadata 生成的 DrawableView)先后添加到 frameLayout 里,FlutterView 在 attachToFlutterEngine 时候回调将 SplashScreen 这个 View transitionToFlutter ,也就是 Alpha 设置为 0

 public void transitionToFlutter(@NonNull final Runnable onTransitionComplete) {
        if (this.splashView == null) {
            onTransitionComplete.run();
        } else {
            this.splashView.animate().alpha(0.0F).setDuration(this.crossfadeDurationInMillis).setListener(new AnimatorListener() {
                public void onAnimationStart(Animator animation) {
                }

                public void onAnimationEnd(Animator animation) {
                    onTransitionComplete.run();
                }

                public void onAnimationCancel(Animator animation) {
                    onTransitionComplete.run();
                }

                public void onAnimationRepeat(Animator animation) {
                }
            });
        }
    }

看了上面到源码感觉 flutter 引擎很像 WebView:


image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342