Flutter之Android層面原始碼分析(一)

kimi_he發表於2018-03-17

學習Flutter過程中,先擼了一遍Flutter,寫了個仿boss直聘的demo, github地址:flutter_boss. 寫完之後其實比較迷茫,android裡到底幹了啥,於是稍微看了一下原始碼,有種恍然大悟的感覺。

在建立完Flutter工程後,自動為我們生成了一個FlutterApplication和一個kotlin的Activity。 在FlutterApplication裡其實就做了一件事,通過呼叫FlutterMain裡的startInitialization方法進行初始化。 在生成的主的Activity裡我們可以看見以下內容。

class MainActivity(): FlutterActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
  }
}
複製程式碼

可以看到,這個MainActivity就是啟動Activity,只不過是繼承的FlutterActivity,於是進入FlutterActivity,發現是繼承自Activity,而FlutterActivity裡的生命週期是委託給另一個FlutterActivityDelegate管理的,還有一個類名字叫FlutterFragmentActivity共用了該類。 看原始碼先抓重點,當然先是看FlutterActivityDelegate的onCreate裡做了啥

String[] args = getArgsFromIntent(this.activity.getIntent());        

FlutterMain.ensureInitializationComplete(this.activity.getApplicationContext(), args);
        this.flutterView = this.viewFactory.createFlutterView(this.activity);
        if(this.flutterView == null) {
            FlutterNativeView nativeView = this.viewFactory.createFlutterNativeView();
            this.flutterView = new FlutterView(this.activity, (AttributeSet)null, nativeView);
            this.flutterView.setLayoutParams(matchParent);
            this.activity.setContentView(this.flutterView);
            this.launchView = this.createLaunchView();
            if(this.launchView != null) {
                this.addLaunchView();
            }
        }
複製程式碼

前2行是看方法意思是關於確保了Flutter環境初始化完成,如果初始化失敗,則會提示"Flutter initialization failed."並丟擲RuntimeException,這塊不用暫時不用太關心,Flutter工程IDE為我們建立好了,一般不會在這裡出問題。

然後接下去看是怎麼初始化的, 實際專案裡,我們是通過Dart來編寫Flutter介面的,那麼我們肯定最關心Flutter和activity裡的介面是什麼關係,怎麼承載的。通過初始化我們可以看到flutterView是通過viewFactory介面裡的2個方法createFlutterView和createFlutterNativeView裡去建立,預設是直接返回null,這麼寫的目的是可以通過override由自己傳入。接下去看,由於預設的flutterView是null,所以就通過new FlutterView建立。其實FlutterView繼承自SurfaceView,這時候,Android自定義View的知識派上用處了。 最後通過最熟悉的activity.setContentView(this.flutterView);設定完成。所以我們可以得出一個結論,Flutter開發出來的應用不管裡面有多少個介面,都是一個繼承自SurfaceView的FlutterView,既不是activity也不是fragment,只是一個view,必要時,我們可以重寫FlutterActivityDelegate裡的onCreate實現我們自己的需求。 注意後面的createLaunchView方法,我們可以建立app的啟動畫面。

public FlutterView(Context context, AttributeSet attrs, FlutterNativeView nativeView) {
        super(context, attrs);
        ...
        Activity activity = (Activity)this.getContext();
        if(nativeView == null) {
            this.mNativeView = new FlutterNativeView(activity.getApplicationContext());
        } else {
            this.mNativeView = nativeView;
        }

        this.mNativeView.attachViewAndActivity(this, activity);

        ...

        this.mAccessibilityManager = (AccessibilityManager)this.getContext().getSystemService("accessibility");
        this.mActivityLifecycleListeners = new ArrayList();
        this.mFirstFrameListeners = new ArrayList();
        this.mFlutterLocalizationChannel = new MethodChannel(this, "flutter/localization", JSONMethodCodec.INSTANCE);
        this.mFlutterNavigationChannel = new MethodChannel(this, "flutter/navigation", JSONMethodCodec.INSTANCE);
        this.mFlutterKeyEventChannel = new BasicMessageChannel(this, "flutter/keyevent", JSONMessageCodec.INSTANCE);
        this.mFlutterLifecycleChannel = new BasicMessageChannel(this, "flutter/lifecycle", StringCodec.INSTANCE);
        this.mFlutterSystemChannel = new BasicMessageChannel(this, "flutter/system", JSONMessageCodec.INSTANCE);
        this.mFlutterSettingsChannel = new BasicMessageChannel(this, "flutter/settings", JSONMessageCodec.INSTANCE);
        PlatformPlugin platformPlugin = new PlatformPlugin(activity);
        MethodChannel flutterPlatformChannel = new MethodChannel(this, "flutter/platform", JSONMethodCodec.INSTANCE);
        flutterPlatformChannel.setMethodCallHandler(platformPlugin);
        this.addActivityLifecycleListener(platformPlugin);
        this.mTextInputPlugin = new TextInputPlugin(this);
        this.setLocale(this.getResources().getConfiguration().locale);
        this.setUserSettings();
        if((context.getApplicationInfo().flags & 2) != 0) {
            this.mDiscoveryReceiver = new FlutterView.DiscoveryReceiver(null);
            context.registerReceiver(this.mDiscoveryReceiver, new IntentFilter("io.flutter.view.DISCOVER"));
        } else {
            this.mDiscoveryReceiver = null;
        }

    }

複製程式碼

程式碼太長,有的地方省略了,我認為比較重要的是做了以下幾點:

  1. 真正建立了FlutterNativeView,裡面真正工作的是Framework層的dart
  2. 載入系統預設的MethodChannel,至於MethodChannel是幹什麼的,官方有一張圖可以說明。
    Flutter之Android層面原始碼分析(一)
    我們也可以在Activity裡做一些擴張,自定義自己的MethodChannel
       new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodCallHandler() {
                @Override
                public void onMethodCall(MethodCall call, Result result) {
                    // TODO
                }
            });
複製程式碼

其他的activity裡的幾個生命週期方法,沒什麼好說的,簡單說下onDestroy,

    public void destroy() {
        if(this.isAttached()) {
            if(this.mDiscoveryReceiver != null) {
                this.getContext().unregisterReceiver(this.mDiscoveryReceiver);
            }

            this.getHolder().removeCallback(this.mSurfaceCallback);
            this.mNativeView.destroy();
            this.mNativeView = null;
        }
    }
複製程式碼

就是把FlutterNativeView清除。

時間關係,先了解個大概吧,後面有新的收穫再來分享。

相關文章