[TOC]

preface

As you can see from the Diagram of the Flutter architecture, the Flutter Platform SDK sits on top of the entire Flutter framework, connecting Java and Dart code. So as the “top” it, in the end play what role, and how to play these roles? Google engineers used a package called “Show me the answer” to flutter. Jar.

Platform SDK role-playing

According to the source code of Platform SDK, it can be roughly divided into three roles: Creator, Transmitter and Registrant.

Creator

The creator

FlutterMain
Sequence diagram

FlutterMain initializes a flutter after it is invoked by Application’s onCreate

  • initConfig

    The required profile name and path

    • Files generated by the command line flutter build aot

      The name of the content
      isolate_snapshot_instr Application instruction segment
      isolate_snapshot_data Application data segment
      vm_snapshot_instr VM VM instruction segment
      vm_snapshot_data VM VM data segment

    Note: Refer to the official description for details

    • Path information

      Path information of files such as flutter_assets

  • initAot

    1. Initialization bit: How the snapshot file is integrated.
    2. SIsPrecompiledAsSharedLibrary represents all the snapshot files bundled into a dynamic library (similar to an ios integrated way).
    3. Do not use two integration methods at the same time.
  • initResource

    1. Creates a ResourceExtractor object that is the porter of the Resource file
    2. Initialize the files to be moved through the addResource method of the ResourceExtractor object
    3. The ResourceExtractor starts an asynchronous task to move the files under asset to the flutter directory of the DataDir

FlutterActivityDelegate

WhatDelegate
//FlutterActivity.java.private final FlutterActivityEvents eventDelegate;
private final Provider viewProvider;
private final PluginRegistry pluginRegistry;   
private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this.this); .public FlutterActivity(a) {
        this.eventDelegate = this.delegate;
        this.viewProvider = this.delegate;
        this.pluginRegistry = this.delegate; }...Copy the code

We can see from the FlutterActivity constructor above what responsibilities the Delegate delegates Delegate

  • FlutterActivityEvents: Pass the specific processing logic for the Activity lifecycle to the FlutterView

  • Provider: FlutterView created in onCreate, exposed through the interface that implements the Provider

  • PluginRegistry: Registration and query of plug-in information is passed to the FlutterView

    From the above, the object that FlutterActivityDelegate directly interacts with is Actually FlutterView. Through such design, the cohesion of each class is improved

FlutterView

FlutterNativeView can be simply regarded as responsible for display at first, while the following FlutterNativeView is responsible for communication.

  • Decouple system control

    First of all, we can see that FlutterView is derived from the SurfaceView. When we think of SurfaceView, we can’t help but think of words like “dig” and “double buffer”. Because of these features, FlutterView can transfer UI rendering to Flutter (which does the drawing via Dart -> LayerTree->Skia).

  • Pass status messages to the Dart UI

    These messages can be roughly divided into seven modules, two broad categories

    1. Data communication is performed by reflecting native system apis

      Multilingual modules, system information and user Settings

    2. The data is packaged into a special format to interact with flutter controls written using Dart in the form of messages

      Lifecycle, keystroke messages, routing navigation, and platform plug-ins

FlutterNativeView

We can simply consider the FlutterNativeView as responsible for communication. It implements the BinaryMessenger interface at the system level [this is detailed in Transmitter below].

Transmitter

The relay

News channels

  • Flutter encapsulates three classes of channels for different application scenarios

    1. MethodChannel: Calls methods
    2. BasicMessageChannel: Customizes structure information
    3. EventChannel: Indicates an event notification
  • The three classes of channels have similar structure types

    1. Messenger BinaryMessenger: our self-created channel is typically the FlutterNativeView
    2. Channel name: the key value of a channel, which cannot be repeated
    3. MethodCodec /MessageCodec decoder: Decodes binary reply data for different channles

Message codec

Standard platform channels use a standard message codec. Efficient binary serialization of simple JSON class values (such as Boolean values), numbers, strings, byte buffers, and these lists and mappings. (See StandardMessageCodec for details). Serialization and deserialization of these values occur automatically when a message sends and receives values. (The corresponding data conversion relationship is as follows)

Transfer process

Dart and Android can communicate with each other through data conversion by the Flutter Engine

Registrant

registrant

Situation transformation

Registrar: Registrar, registrar and registrant To translate them into real life scenes is actually a process of “the registrant updates the registration information through the registrar, and the registrar sends the information to the registry office for preservation”. Let’s translate this process back into code:

First we create a new plugin as the object of the description:

/ / implementation PluginRegistry ActivityResultListener
public class FlutterMusicPlugin implements MethodCallHandler.PluginRegistry.ActivityResultListener {...public static void registerWith(Registrar registrar) {
        / / to the Activity
        final FlutterMusicPlugin plugin = newFlutterMusicPlugin(registrar.activity()); .// Register the ActivityResult callback
        registrar.addActivityResultListener(plugin);
    }
    
    @Override
    public void onMethodCall(MethodCall call, Result result) {... }@Override
    public boolean onActivityResult(int requestCode, int resultCode, Intent data) {...return false; }}Copy the code

At the same time in GeneratedPluginRegistrant class will be automatically generated

public final class GeneratedPluginRegistrant {
  public static void registerWith(PluginRegistry registry) {
    if (alreadyRegisteredWith(registry)) {
      return;
    }
 FlutterMusicPlugin.registerWith(registry.registrarFor("com.plugin.FlutterMusicPlugin"));
  }

  private static boolean alreadyRegisteredWith(PluginRegistry registry) {
    final String key = GeneratedPluginRegistrant.class.getCanonicalName();
    if (registry.hasPlugin(key)) {
      return true;
    }
    registry.registrarFor(key);
    return false; }}Copy the code

registrant

It is the corresponding GeneratedPluginRegistrant class in your code

  • A registered

    GeneratedPluginRegistrant in the class

     FlutterMusicPlugin.registerWith(registry.registrarFor("com.plugin.FlutterMusicPlugin"));// That is to initiate the registration point
    Copy the code
  • Renew registration through a registrar

    FlutterMusicPlugin in the class

    registrar.addActivityResultListener(plugin);// Update the information that requires the Activity callback through the registrar
    Copy the code
  • Synchronize to the registry

    FlutterRegistrar class in the SDK

    FlutterPluginRegistry.this.mActivityResultListeners.add(listener);// To synchronize the information to the registry
    Copy the code

registrar

This corresponds to the FlutterPluginRegistry internal class FlutterRegistrar in your code.

SurfaceTexture (surfaceTexture), surfaceTexture (surfaceTexture), surfaceTexture (surfaceTexture)

      
        Activity activity(a);// Return the Host app Activity
        Context context(a);// Return Application Context.
        Context activeContext(a);// Return the active Context
        BinaryMessenger is used to register Platform Channels
        BinaryMessenger messenger(a);
        // Return TextureRegistry to retrieve SurfaceTexture
        TextureRegistry textures(a);
        // Returns the FlutterView created by the current Host app
        FlutterView view(a);
        // Return the file path corresponding to Asset
        String lookupKeyForAsset(String var1);
        // Return the file path corresponding to Asset
        String lookupKeyForAsset(String var1, String var2);
        // A "value" to be published by the plug-in
        PluginRegistry.Registrar publish(Object var1);
        // Register permission related callback
        PluginRegistry.Registrar addRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener var1);
        // Register the ActivityResult callback
        PluginRegistry.Registrar addActivityResultListener(PluginRegistry.ActivityResultListener var1);
        // Register the NewIntent callback
        PluginRegistry.Registrar addNewIntentListener(PluginRegistry.NewIntentListener var1);
        // Register the UserLeaveHint callback
        PluginRegistry.Registrar addUserLeaveHintListener(PluginRegistry.UserLeaveHintListener var1);
        // Register the View destruction callback
        PluginRegistry.Registrar addViewDestroyListener(PluginRegistry.ViewDestroyListener var1);
Copy the code

registry

This corresponds to the FlutterPluginRegistry in your code

As can be seen from the member variables, FlutterPluginRegistry mainly maintains two types of information:

  1. Summary of unique identity information for registered plug-ins
  2. FlutterRegistrar open by those features (such as: addActivityResultListener) information summary