Flutter 熱更新-1.12.13+hotfix.5啟動流程分析

這里分析flutter作為module情況下Android端啟動流程。1.12.13版本通過下述方法啟動Flutter頁面。

startActivity(
   FlutterActivity.createDefaultIntent(this)
)

createDefaultIntent()

public static Intent createDefaultIntent(@NonNull Context launchContext) {
  return withNewEngine().build(launchContext);
}

public Intent build(@NonNull Context context) {
  return new Intent(context, activityClass)
      .putExtra(EXTRA_INITIAL_ROUTE, initialRoute)
      .putExtra(EXTRA_BACKGROUND_MODE, backgroundMode)
      .putExtra(EXTRA_DESTROY_ENGINE_WITH_ACTIVITY, true);
}
 
//activityClass聲明處Class<? extends FlutterActivity>
//activityClass是FlutterActivity的子類型,接下來看FlutterActivity.onCreate()
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  switchLaunchThemeForNormalTheme();
 
  super.onCreate(savedInstanceState);
 
  lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
 
  delegate = new FlutterActivityAndFragmentDelegate(this);
  delegate.onAttach(this);
  delegate.onActivityCreated(savedInstanceState);
 
  configureWindowForTransparency();
  setContentView(createFlutterView());
  configureStatusBarForFullscreenFlutterExperience();
}
  • delegate.onAttach(this)
  • delegate.onActivityCreated(savedInstanceState)

FlutterActivityAndFragmentDelegate.onActivityCreated()

void onActivityCreated(@Nullable Bundle bundle) {
  Log.v(TAG, "onActivityCreated. Giving plugins an opportunity to restore state.");
  ensureAlive();
 
  if (host.shouldAttachEngineToActivity()) {
    flutterEngine.getActivityControlSurface().onRestoreInstanceState(bundle);
  }
}

flutterEngine.getActivityControlSurface().onRestoreInstanceState最終調(diào)用到FlutterEnginePluginRegistry.onRestoreInstanceState()

public void onRestoreInstanceState(@Nullable Bundle bundle) {
  if (isAttachedToActivity()) {
    activityPluginBinding.onRestoreInstanceState(bundle);
  } else {
    Log.e(TAG, "Attempted to notify ActivityAware plugins of onRestoreInstanceState, but no Activity was attached.");
  }
}

activityPluginBinding.onRestoreInstanceState(bundle),activityPluginBinding是FlutterEnginePluginRegistry的靜態(tài)內(nèi)部類FlutterEngineActivityPluginBinding;FlutterEngineActivityPluginBinding.onRestoreInstanceState()遍歷OnSaveInstanceStateListener集合回調(diào)onRestoreInstanceState()方法。

void onRestoreInstanceState(@Nullable Bundle bundle) {
  for (OnSaveInstanceStateListener listener : onSaveInstanceStateListeners) {
    listener.onRestoreInstanceState(bundle);
  }
}

onSaveInstanceStateListeners是FlutterEngineActivityPluginBinding成員變量

private final Set<OnSaveInstanceStateListener> onSaveInstanceStateListeners = new HashSet<>();

看看它在哪里被賦值,FlutterEngineActivityPluginBinding

@Override
public void addOnSaveStateListener(@NonNull OnSaveInstanceStateListener listener) {
  onSaveInstanceStateListeners.add(listener);
}

addOnSaveStateListener是ActivityPluginBinding接口方法,F(xiàn)lutterEngine通過FlutterEnginePluginRegistry注冊一些PluginBinding同步對應(yīng)組件的信息,ActivityPluginBinding中就有我們很熟悉的一些Activity回調(diào),類似的FlutterEnginePluginRegistry中FlutterEngineServicePluginBinding實(shí)現(xiàn)了ServicePluginBinding接口。

public interface ActivityPluginBinding {
  @NonNull
  Activity getActivity();

  @NonNull
  Object getLifecycle();

  void addRequestPermissionsResultListener(@NonNull PluginRegistry.RequestPermissionsResultListener listener);

  void removeRequestPermissionsResultListener(@NonNull PluginRegistry.RequestPermissionsResultListener listener);

  void addActivityResultListener(@NonNull PluginRegistry.ActivityResultListener listener);

  void removeActivityResultListener(@NonNull PluginRegistry.ActivityResultListener listener);

  void addOnNewIntentListener(@NonNull PluginRegistry.NewIntentListener listener);

  void removeOnNewIntentListener(@NonNull PluginRegistry.NewIntentListener listener);

  void addOnUserLeaveHintListener(@NonNull PluginRegistry.UserLeaveHintListener listener);

  void removeOnUserLeaveHintListener(@NonNull PluginRegistry.UserLeaveHintListener listener);

  void addOnSaveStateListener(@NonNull OnSaveInstanceStateListener listener);

  void removeOnSaveStateListener(@NonNull OnSaveInstanceStateListener listener);

  interface OnSaveInstanceStateListener {
    void onSaveInstanceState(@NonNull Bundle bundle);

    void onRestoreInstanceState(@Nullable Bundle bundle);
  }
}

FlutterActivityAndFragmentDelegate.onAttach()

void onAttach(@NonNull Context context) {
  ensureAlive();
 
  if (flutterEngine == null) {
    setupFlutterEngine();
  }
 
  platformPlugin = host.providePlatformPlugin(host.getActivity(), flutterEngine);
 
  if (host.shouldAttachEngineToActivity()) {
    //FlutterEnginePluginRegistry.attachToActivity()
    flutterEngine.getActivityControlSurface().attachToActivity(
        host.getActivity(),
        host.getLifecycle()
    );
  }
 
  host.configureFlutterEngine(flutterEngine);
}

ensureAlive()

private void ensureAlive() {
    //host是前面delegate = new FlutterActivityAndFragmentDelegate(this)傳入的this即FlutterActivity
    if (host == null) {
      throw new IllegalStateException("Cannot execute method on a destroyed FlutterActivityAndFragmentDelegate.");
    }
  }

setupFlutterEngine()

void setupFlutterEngine() {
  String cachedEngineId = host.getCachedEngineId();
  if (cachedEngineId != null) {
    //flutterEngine緩存池
    flutterEngine = FlutterEngineCache.getInstance().get(cachedEngineId);
    isFlutterEngineFromHost = true;
    if (flutterEngine == null) {
      throw new IllegalStateException("The requested cached FlutterEngine did not exist in the FlutterEngineCache: '" + cachedEngineId + "'");
    }
    return;
  }
 
  flutterEngine = host.provideFlutterEngine(host.getContext());
  if (flutterEngine != null) {
    isFlutterEngineFromHost = true;
    return;
  }
 
  //初始化FlutterEngine
  flutterEngine = new FlutterEngine(host.getContext(), host.getFlutterShellArgs().toArray());
  isFlutterEngineFromHost = false;
}

FlutterEngine()

public FlutterEngine(@NonNull Context context, @Nullable String[] dartVmArgs) {
  this(context, FlutterLoader.getInstance(), new FlutterJNI(), dartVmArgs, true);
}

public FlutterEngine(
    @NonNull Context context,
    @NonNull FlutterLoader flutterLoader,
    @NonNull FlutterJNI flutterJNI,
    @Nullable String[] dartVmArgs,
    boolean automaticallyRegisterPlugins
) {
  this.flutterJNI = flutterJNI;
  flutterLoader.startInitialization(context);
  flutterLoader.ensureInitializationComplete(context, dartVmArgs);
 
  flutterJNI.addEngineLifecycleListener(engineLifecycleListener);
  attachToJni();
 
  this.dartExecutor = new DartExecutor(flutterJNI, context.getAssets());
  this.dartExecutor.onAttachedToJNI();
 
  this.renderer = new FlutterRenderer(flutterJNI);
 
  accessibilityChannel = new AccessibilityChannel(dartExecutor, flutterJNI);
  keyEventChannel = new KeyEventChannel(dartExecutor);
  lifecycleChannel = new LifecycleChannel(dartExecutor);
  localizationChannel = new LocalizationChannel(dartExecutor);
  navigationChannel = new NavigationChannel(dartExecutor);
  platformChannel = new PlatformChannel(dartExecutor);
  settingsChannel = new SettingsChannel(dartExecutor);
  systemChannel = new SystemChannel(dartExecutor);
  textInputChannel = new TextInputChannel(dartExecutor);
 
  platformViewsController = new PlatformViewsController();
 
  this.pluginRegistry = new FlutterEnginePluginRegistry(
    context.getApplicationContext(),
    this,
    flutterLoader
  );
 
  if (automaticallyRegisterPlugins) {
    registerPlugins();
  }
}
  • flutterLoader.startInitialization(context)
  • flutterLoader.ensureInitializationComplete(context, dartVmArgs)

FlutterLoader.startInitialization()

public void startInitialization(@NonNull Context applicationContext) {
        startInitialization(applicationContext, new Settings());
}
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");
        }

        this.settings = settings;

        long initStartTimestampMillis = SystemClock.uptimeMillis();
        initConfig(applicationContext);
        initResources(applicationContext);

        System.loadLibrary("flutter");

        VsyncWaiter
            .getInstance((WindowManager) applicationContext.getSystemService(Context.WINDOW_SERVICE))
            .init();

        long initTimeMillis = SystemClock.uptimeMillis() - initStartTimestampMillis;
        FlutterJNI.nativeRecordStartTimestamp(initTimeMillis);
}

//initConfig 初始化資源文件
private void initConfig(@NonNull Context applicationContext) {
        Bundle metadata = getApplicationInfo(applicationContext).metaData;

        if (metadata == null) {
            return;
        }

        //so包名稱
        aotSharedLibraryName = metadata.getString(PUBLIC_AOT_SHARED_LIBRARY_NAME, DEFAULT_AOT_SHARED_LIBRARY_NAME);
        //flutterAsset
        flutterAssetsDir = metadata.getString(PUBLIC_FLUTTER_ASSETS_DIR_KEY, DEFAULT_FLUTTER_ASSETS_DIR);
        //BuildConfig.DEBUG || BuildConfig.JIT_RELEASE使用,AOT Release版本flutter代碼打包成so包。
        //ensureInitializationComplete()中可以驗證
        vmSnapshotData = metadata.getString(PUBLIC_VM_SNAPSHOT_DATA_KEY, DEFAULT_VM_SNAPSHOT_DATA);
        isolateSnapshotData = metadata.getString(PUBLIC_ISOLATE_SNAPSHOT_DATA_KEY, DEFAULT_ISOLATE_SNAPSHOT_DATA);
}
//initResources 初始化資源,方法名自解釋。
private void initResources(@NonNull Context applicationContext) {
        new ResourceCleaner(applicationContext).start();

        if (BuildConfig.DEBUG || BuildConfig.JIT_RELEASE) {
            final String dataDirPath = PathUtils.getDataDirectory(applicationContext);
            final String packageName = applicationContext.getPackageName();
            final PackageManager packageManager = applicationContext.getPackageManager();
            final AssetManager assetManager = applicationContext.getResources().getAssets();
            resourceExtractor = new ResourceExtractor(dataDirPath, packageName, packageManager, assetManager);

            resourceExtractor
                .addResource(fullAssetPathFrom(vmSnapshotData))
                .addResource(fullAssetPathFrom(isolateSnapshotData))
                .addResource(fullAssetPathFrom(DEFAULT_KERNEL_BLOB));

            resourceExtractor.start();
        }
}

ensureInitializationComplete()

public void ensureInitializationComplete(@NonNull Context applicationContext, @Nullable String[] args) {
    if (initialized) {
        return;
    }
    if (Looper.myLooper() != Looper.getMainLooper()) {
      throw new IllegalStateException("ensureInitializationComplete must be called on the main thread");
    }
    if (settings == null) {
      throw new IllegalStateException("ensureInitializationComplete must be called after startInitialization");
    }
    try {
        if (resourceExtractor != null) {
            resourceExtractor.waitForCompletion();
        }
 
        List<String> shellArgs = new ArrayList<>();
        shellArgs.add("--icu-symbol-prefix=_binary_icudtl_dat");
 
        ApplicationInfo applicationInfo = getApplicationInfo(applicationContext);
        shellArgs.add("--icu-native-lib-path=" + applicationInfo.nativeLibraryDir + File.separator + DEFAULT_LIBRARY);
 
        if (args != null) {
            Collections.addAll(shellArgs, args);
        }
 
        String kernelPath = null;
        //DEBUG/JIT_RELEASE加載邏輯
        if (BuildConfig.DEBUG || BuildConfig.JIT_RELEASE) {
            String snapshotAssetPath = PathUtils.getDataDirectory(applicationContext) + File.separator + flutterAssetsDir;
            kernelPath = snapshotAssetPath + File.separator + DEFAULT_KERNEL_BLOB;
            shellArgs.add("--" + SNAPSHOT_ASSET_PATH_KEY + "=" + snapshotAssetPath);
            shellArgs.add("--" + VM_SNAPSHOT_DATA_KEY + "=" + vmSnapshotData);
            shellArgs.add("--" + ISOLATE_SNAPSHOT_DATA_KEY + "=" + isolateSnapshotData);
        } else {//AOT Release加載邏輯
            shellArgs.add("--" + AOT_SHARED_LIBRARY_NAME + "=" + aotSharedLibraryName);

            //熱更新可指定AOT_SHARED_LIBRARY_NAME為hotfix.so地址
            //hotfix.so拷貝到data/data/package/app_libs/目錄下
            shellArgs.add("--" + AOT_SHARED_LIBRARY_NAME + "=" + applicationInfo.nativeLibraryDir + File.separator + aotSharedLibraryName);
        }
 
        shellArgs.add("--cache-dir-path=" + PathUtils.getCacheDirectory(applicationContext));
        if (settings.getLogTag() != null) {
            shellArgs.add("--log-tag=" + settings.getLogTag());
        }
 
        String appStoragePath = PathUtils.getFilesDir(applicationContext);
        String engineCachesPath = PathUtils.getCacheDirectory(applicationContext);
        FlutterJNI.nativeInit(applicationContext, shellArgs.toArray(new String[0]),
            kernelPath, appStoragePath, engineCachesPath);
 
        initialized = true;
    } catch (Exception e) {
        Log.e(TAG, "Flutter initialization failed.", e);
        throw new RuntimeException(e);
    }
}

shellArgs.add("--" + AOT_SHARED_LIBRARY_NAME + "=" + aotSharedLibraryName)

shellArgs是個數(shù)組,包含so包和資源文件,最終調(diào)用FlutterJNI.nativeInit()加載flutter資源。實(shí)現(xiàn)熱更新可以從這個數(shù)組入手。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容