Flutter Android Embedding V1和V2有什么区别
What is the difference between Flutter Android Embedding V1 and V2
我正在为带有背景内容的 Flutter 开发一个插件。
最近,我在 android_alarm_manager 插件中遇到了一些关于 Flutter android 嵌入 的问题。
README
的一部分说:
For the Flutter Android Embedding V1, the background service must be provided a callback to register plugins with the background isolate.
- Flutter android 嵌入 V1 或 V2 究竟是什么?
- 这两者有什么区别?
根据docs:
In order to better support the execution environments of adding Flutter to an existing project, the old Android platform-side wrappers hosting the Flutter runtime at io.flutter.app.FlutterActivity and their associated classes are now deprecated. New wrappers at io.flutter.embedding.android.FlutterActivity and associated classes now replace them.
那些 类 更好地支持真实世界的场景,其中 FlutterActivity 不是应用程序中第一个也是唯一的 Android Activity。
嵌入式 v2 为诸如后台执行之类的事情提供了更好的支持(例如 firebase 消息传递。查看 changeLog)。
如果您正在开发插件,您应该考虑从嵌入式 v2 开始。现有包已经迁移或正在迁移。
Flutter 发布了其 Android 嵌入的新版本。这是负责将 Flutter 集成到 Android 应用程序中的 Android 代码。它包括 类,例如 FlutterActivity
、FlutterFragment
、FlutterView
和 FlutterEngine
。 v2 Android 嵌入包括对标准 Android 生命周期事件的支持以及 Flutter 执行与 Android UI 的分离,这在 v1 Android 嵌入中是缺失的.在 v2 Android 嵌入的开发过程中,很明显现有的 Flutter 插件 API 不足以处理 v2 Android 嵌入的新功能。需要一个新的 Android 插件 API。
在旧的 v1 Android 嵌入中,所有插件都在 Android 应用程序的最开始进行初始化和配置,并且只有一次 Flutter 体验。在 v2 嵌入中,我们不假设插件何时初始化,并且每个 FlutterEngine 必须初始化一次插件。因此,Android 的所有 Flutter 插件现在必须支持实例化而不是静态初始化,并且它们必须支持附加到 FlutterEngine 和从 FlutterEngine 分离。以下代码示例演示了旧版 v1 插件初始化实现和新版 v2 插件初始化过程之间的区别。
旧插件初始化
class MyOldPlugin {
public static void registerWith(PluginRegistrar registrar) {
// Obtain any references that the plugin requires from the
// registrar.
//
// This plugin is now considered "initialized" and "attached"
// to a Flutter experience.
}
}
新插件初始化
class MyNewPlugin implements FlutterPlugin {
public MyNewPlugin() {
// All Android plugin classes must support a no-args
// constructor. A no-arg constructor is provided by
// default without declaring one, but we include it here for
// clarity.
//
// At this point your plugin is instantiated, but it
// isn't attached to any Flutter experience. You should not
// attempt to do any work here that is related to obtaining
// resources or manipulating Flutter.
}
@override
public void onAttachedToFlutterEngine(FlutterPluginBinding binding) {
// Your plugin is now attached to a Flutter experience
// represented by the given FlutterEngine.
//
// You can obtain the associated FlutterEngine with
// binding.getFlutterEngine()
//
// You can obtain a BinaryMessenger with
// binding.getBinaryMessenger()
//
// You can obtain the Application context with
// binding.getApplicationContext()
//
// You cannot access an Activity here because this
// FlutterEngine is not necessarily displayed within an
// Activity. See the ActivityAware interface for more info.
}
@override
public void onDetachedFromFlutterEngine(FlutterPluginBinding binding) {
// Your plugin is no longer attached to a Flutter experience.
// You need to clean up any resources and references that you
// established in onAttachedToFlutterEngine().
}
}
此外,您的插件不得依赖于 onAttachedToFlutterEngine() 中的 Activity 引用。仅仅因为您的插件附加到 Flutter 体验并不意味着 Flutter 体验正在 Activity 中显示。 这是新旧插件之间最显着的区别之一 APIs。在旧的 v1 插件 API 中,插件作者可以依赖 Activity 立即和永久可用。这不再是真的。
有关详细信息,请参阅 https://medium.com/flutter/modern-flutter-plugin-development-4c3ee015cf5a
我正在为带有背景内容的 Flutter 开发一个插件。
最近,我在 android_alarm_manager 插件中遇到了一些关于 Flutter android 嵌入 的问题。
README
的一部分说:
For the Flutter Android Embedding V1, the background service must be provided a callback to register plugins with the background isolate.
- Flutter android 嵌入 V1 或 V2 究竟是什么?
- 这两者有什么区别?
根据docs:
In order to better support the execution environments of adding Flutter to an existing project, the old Android platform-side wrappers hosting the Flutter runtime at io.flutter.app.FlutterActivity and their associated classes are now deprecated. New wrappers at io.flutter.embedding.android.FlutterActivity and associated classes now replace them.
那些 类 更好地支持真实世界的场景,其中 FlutterActivity 不是应用程序中第一个也是唯一的 Android Activity。
嵌入式 v2 为诸如后台执行之类的事情提供了更好的支持(例如 firebase 消息传递。查看 changeLog)。
如果您正在开发插件,您应该考虑从嵌入式 v2 开始。现有包已经迁移或正在迁移。
Flutter 发布了其 Android 嵌入的新版本。这是负责将 Flutter 集成到 Android 应用程序中的 Android 代码。它包括 类,例如
FlutterActivity
、FlutterFragment
、FlutterView
和FlutterEngine
。 v2 Android 嵌入包括对标准 Android 生命周期事件的支持以及 Flutter 执行与 Android UI 的分离,这在 v1 Android 嵌入中是缺失的.在 v2 Android 嵌入的开发过程中,很明显现有的 Flutter 插件 API 不足以处理 v2 Android 嵌入的新功能。需要一个新的 Android 插件 API。在旧的 v1 Android 嵌入中,所有插件都在 Android 应用程序的最开始进行初始化和配置,并且只有一次 Flutter 体验。在 v2 嵌入中,我们不假设插件何时初始化,并且每个 FlutterEngine 必须初始化一次插件。因此,Android 的所有 Flutter 插件现在必须支持实例化而不是静态初始化,并且它们必须支持附加到 FlutterEngine 和从 FlutterEngine 分离。以下代码示例演示了旧版 v1 插件初始化实现和新版 v2 插件初始化过程之间的区别。
旧插件初始化
class MyOldPlugin {
public static void registerWith(PluginRegistrar registrar) {
// Obtain any references that the plugin requires from the
// registrar.
//
// This plugin is now considered "initialized" and "attached"
// to a Flutter experience.
}
}
新插件初始化
class MyNewPlugin implements FlutterPlugin {
public MyNewPlugin() {
// All Android plugin classes must support a no-args
// constructor. A no-arg constructor is provided by
// default without declaring one, but we include it here for
// clarity.
//
// At this point your plugin is instantiated, but it
// isn't attached to any Flutter experience. You should not
// attempt to do any work here that is related to obtaining
// resources or manipulating Flutter.
}
@override
public void onAttachedToFlutterEngine(FlutterPluginBinding binding) {
// Your plugin is now attached to a Flutter experience
// represented by the given FlutterEngine.
//
// You can obtain the associated FlutterEngine with
// binding.getFlutterEngine()
//
// You can obtain a BinaryMessenger with
// binding.getBinaryMessenger()
//
// You can obtain the Application context with
// binding.getApplicationContext()
//
// You cannot access an Activity here because this
// FlutterEngine is not necessarily displayed within an
// Activity. See the ActivityAware interface for more info.
}
@override
public void onDetachedFromFlutterEngine(FlutterPluginBinding binding) {
// Your plugin is no longer attached to a Flutter experience.
// You need to clean up any resources and references that you
// established in onAttachedToFlutterEngine().
}
}
此外,您的插件不得依赖于 onAttachedToFlutterEngine() 中的 Activity 引用。仅仅因为您的插件附加到 Flutter 体验并不意味着 Flutter 体验正在 Activity 中显示。 这是新旧插件之间最显着的区别之一 APIs。在旧的 v1 插件 API 中,插件作者可以依赖 Activity 立即和永久可用。这不再是真的。
有关详细信息,请参阅 https://medium.com/flutter/modern-flutter-plugin-development-4c3ee015cf5a