介紹
Flutter應用是由平臺來創建、初始化並啓動的,這裏我們以android爲例,對啓動過程做一個走馬觀花式的瞭解,旨在對平臺端的工作有個大致瞭解。
Android端的啓動流程
啓動流程實際上還涉及了很多native 層的工作,但是宥於篇幅,暫且只看Android端。
FlutterApplication
flutter應用下,原生的啓動流程並沒有什麼變化,我們來看Application的onCreate函數。
@Override
@CallSuper
public void onCreate() {
super.onCreate();
FlutterMain.startInitialization(this);
}
很簡單,繼續往裏走
public static void startInitialization(@NonNull Context applicationContext) {
if (isRunningInRobolectricTest) {
return;
}
FlutterLoader.getInstance().startInitialization(applicationContext);
}
按上面方法的註釋來看,是初始化 native system(即C++)的,最終會調用下面的方法:
我將說明以註釋的形式寫在下面
public void startInitialization(@NonNull Context applicationContext, @NonNull Settings settings) {
if (this.settings != null) {
return;
}
///確保運行在 主線程
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("startInitialization must be called on the main thread");
}
// Ensure that the context is actually the application context.
final Context appContext = applicationContext.getApplicationContext();
this.settings = settings;
initStartTimestampMillis = SystemClock.uptimeMillis();
///配置 aotSharedLibraryName、flutterAssetsDir、
/// vmSnapshotData、isolateSnapshotData
///等參數
initConfig(appContext);
///初始化VsyncWaiter,並設置回調
/// 當vsync信號到來時,就調用我們設置的回調,最終會觸發頁面的刷新
VsyncWaiter.getInstance((WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE))
.init();
// 子線程
///這裏主要是抽取資源文件,
///加載 flutter(框架)代碼
Callable<InitResult> initTask =
new Callable<InitResult>() {
@Override
public InitResult call() {
ResourceExtractor resourceExtractor = initResources(appContext);
System.loadLibrary("flutter");
// Prefetch the default font manager as soon as possible on a background thread.
// It helps to reduce time cost of engine setup that blocks the platform thread.
Executors.newSingleThreadExecutor()
.execute(
new Runnable() {
@Override
public void run() {
FlutterJNI.nativePrefetchDefaultFontManager();
}
});
if (resourceExtractor != null) {
resourceExtractor.waitForCompletion();
}
return new InitResult(
PathUtils.getFilesDir(appContext),
PathUtils.getCacheDirectory(appContext),
PathUtils.getDataDirectory(appContext));
}
};
initResultFuture = Executors.newSingleThreadExecutor().submit(initTask);
}
至此FlutterApplication 的相關流程就走完了。
另外,雖然上面的代碼中使用了子線程,但是最終在這些任務沒有完成前,是不會進入flutter側的,我們接着走FlutterActivity。
FlutterActivity & onCreate
開始的地方依然是 onCreate()方法:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
///切換主題
switchLaunchThemeForNormalTheme();
super.onCreate(savedInstanceState);
///通知生命週期
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
///初始化delete,這個很重要,
///所有的工作都是由它來完成的
delegate = new FlutterActivityAndFragmentDelegate(this);
delegate.onAttach(this);
///是否需要恢復(包括通知插件)一些狀態
delegate.onActivityCreated(savedInstanceState);
///配置窗口
configureWindowForTransparency();
///創建flutterView
setContentView(createFlutterView());
configureStatusBarForFullscreenFlutterExperience();
}
這裏面比較重的代碼是這幾行:
delegate = new FlutterActivityAndFragmentDelegate(this);
delegate.onAttach(this);
...
setContentView(createFlutterView());
我們一步一步來,首先創建了FlutterActivityAndFragmentDelegate 並調用了它的attact(this)方法。
FlutterActivityAndFragmentDelegate
void onAttach(@NonNull Context context) {
ensureAlive();
///初始化engine
if (flutterEngine == null) {
///這裏面會對已有的engine進行復用
setupFlutterEngine();
}
///初始化平臺插件
///本質上,是將engine的 channel回調與平臺的系統服務進行綁定
///如:震動、複製粘貼、聲音播放等...
platformPlugin = host.providePlatformPlugin(host.getActivity(), flutterEngine);
if (host.shouldAttachEngineToActivity()) {
Log.v(TAG, "Attaching FlutterEngine to the Activity that owns this Fragment.");
/// 激活 原生viewController
/// 並通知相關插件
/// PlatformViewsController 這個類你應該很熟悉(如果你接入過原生view的話)
flutterEngine
.getActivityControlSurface()
.attachToActivity(host.getActivity(), host.getLifecycle());
}
///註冊插件
///通過反射調用 “io.flutter.plugins.GeneratedPluginRegistrant”
///的 “registerWith”方法,這個過程走完了,你的插件基本就能用了
host.configureFlutterEngine(flutterEngine);
}
通過上面,我們大致瞭解了,在flutter端使用的平臺功能是什麼時候裝配的了。
我們回到FlutterActivity,繼續重要的第二步:
setContentView(createFlutterView());
@NonNull
private View createFlutterView() {
return delegate.onCreateView(
null /* inflater */, null /* container */, null /* savedInstanceState */);
}
這裏插一句,可以看一下這篇文章:
Flutter&Android 啓動頁(閃屏頁)的加載流程和優化方案
最終會調用 delete的onCreateView :
@NonNull
View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.v(TAG, "Creating FlutterView.");
ensureAlive();
if (host.getRenderMode() == RenderMode.surface) {
///一般flutter應用是 RenderMode.surface,所以會進入到這裏
///創建FlutterSurfaceView
FlutterSurfaceView flutterSurfaceView =
new FlutterSurfaceView(
host.getActivity(), host.getTransparencyMode() == TransparencyMode.transparent);
// Allow our host to customize FlutterSurfaceView, if desired.
host.onFlutterSurfaceViewCreated(flutterSurfaceView);
// flutterView 創建完成後,便會調用addView
//將 flutterSurfaceView 顯示出來,只不過啥都沒有而已
flutterView = new FlutterView(host.getActivity(), flutterSurfaceView);
} else {
...省略代碼...
}
// Add listener to be notified when Flutter renders its first frame.
flutterView.addOnFirstFrameRenderedListener(flutterUiDisplayListener);
flutterSplashView = new FlutterSplashView(host.getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
flutterSplashView.setId(View.generateViewId());
} else {
flutterSplashView.setId(486947586);
}
///這裏顯示閃屏頁 默認是個白屏
///即,AndroidMainfest.xml 的<metadata>所設置
flutterSplashView.displayFlutterViewWithSplash(flutterView, host.provideSplashScreen());
///將flutterview 綁定到 engine上
flutterView.attachToFlutterEngine(flutterEngine);
return flutterSplashView;
}
flutterView 內部持有flutterSurfaceView (一個Surface),並最終通過attachToFlutterEngine綁定到engine上,我們來看一下其內部實現:
public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
...省略部分代碼...
this.flutterEngine = flutterEngine;
///通過engine的 getRenderer,
///可以將flutter的紋理繪製到android 上。
FlutterRenderer flutterRenderer = this.flutterEngine.getRenderer();
isFlutterUiDisplayed = flutterRenderer.isDisplayingFlutterUi();
renderSurface.attachToRenderer(flutterRenderer);
flutterRenderer.addIsDisplayingFlutterUiListener(flutterUiDisplayListener);
...省略部分代碼...
///輸入插件
textInputPlugin =
new TextInputPlugin(
this,
this.flutterEngine.getTextInputChannel(),
this.flutterEngine.getPlatformViewsController());
///國際化插件
localizationPlugin = this.flutterEngine.getLocalizationPlugin();
///與上面的textInputPlugin相關聯
androidKeyProcessor =
new AndroidKeyProcessor(this.flutterEngine.getKeyEventChannel(), textInputPlugin);
/// 觸摸事件的初始化
/// 相關觸摸數據會發送到flutter端
androidTouchProcessor = new AndroidTouchProcessor(this.flutterEngine.getRenderer());
///輔助功能
accessibilityBridge =
new AccessibilityBridge(
this,
flutterEngine.getAccessibilityChannel(),
(AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE),
getContext().getContentResolver(),
this.flutterEngine.getPlatformViewsController());
...省略部分代碼...
///通過上面的初始化,將用戶相關的設置發送到flutter端
sendUserSettingsToFlutter();
localizationPlugin.sendLocalesToFlutter(getResources().getConfiguration());
sendViewportMetricsToFlutter();
///將當前flutter view 綁定到 PlatformViewsController
flutterEngine.getPlatformViewsController().attachToView(this);
...省略部分代碼...
}
相關初始化工作完成,activity的生命週期也從onCreate來到了onStart()
FlutterActivity & onStart()
@Override
protected void onStart() {
super.onStart();
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
///重要入口
delegate.onStart();
}
delegate.onStart()此方法 最終會調用doInitialFlutterViewRun()方法:
private void doInitialFlutterViewRun() {
...省略部分代碼...
// 這裏就是獲取我們打包所得的 libapp.so路徑
// 即,我們所寫的dart代碼,並執行它
DartExecutor.DartEntrypoint entrypoint =
new DartExecutor.DartEntrypoint(
host.getAppBundlePath(), host.getDartEntrypointFunctionName());
flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint);
}
至此整個android端的啓動流程就走完了,這裏再回顧總結一下。
總結
在flutterApplication中:
初始化一些資源路徑,配置相關參數
抽取資源並加載(assets)
加載flutter.so關鍵庫
- 最終走JNI_OnLoad 進入native進行相關工作
- 如綁定flutter jni
初始化vsyncWaiter
在flutterActivity中:
會初始化重要類FlutterActivityAndFragmentDelegate
activity端的生命週期,也會觸發delegate來對應回調
對平臺的系統功能(震動、剪貼板)進行綁定
初始化platformViewController以及系統channel
創建flutterView(內部持有一個surfaceView)
- 會最終進入native進行engine的初始化工作
在onStart生命週期中加載咱們的dart代碼,開始執行
在這整個過程中,會穿插進行native層的工作,並最終通過native層的調用,轉到flutter端的main()函數,由於這裏的內容很多,將會在後面的文章中介紹。
最後,謝謝大家的閱讀,如果有不對的地方,還請指出。