新建一个plugin工程,包含平台platform的部分
新project的目录解读
插件的部分写好了,接下来看下使用侧,分别是宿主activity和main,dart
在刚刚新建的工程里,有一个example的文件夹,里面是一个标准的flutter工程,用于对插件内容的测试,没问题了,可以发布或者提交了
安卓的activity实现
flutter的实现
另外在example的工程里,android目录下还有一个自动生成的类GeneratedPluginRegistrant,负责利用registerWith方法把插件装载在flutterEngine中
事实上,上述说的所有类都是插件模式下自动生成的。
过程中遇到一些问题引发的思考:
native->flutter\flutter->native怎么实现数据传输,因为自动生成的plugin实现了MethodChannelPlugin : FlutterPlugin, MethodCallHandler这俩接口,
class BlMethodChannelPlugin : FlutterPlugin, MethodCallHandler {
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "bl_method_channel")
channel?.setMethodCallHandler(this)
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (mb != null) {
// Log.e("testrpy",msg)
mb.handleMsg(call, result)
} else {
result.notImplemented()m
result.error("-1", "error", "");
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}}
1、针对flutter->native问题:因为这里实现了onMethodCall接口,但是业务的实现只能耦合在plugin类中,如果有依赖三方sdk或者和ui的操作,就不太方便,另一方面这个只是想作为一个公共类的话,接受消息,对业务项目的通用处理做一个统一封装,具体的流转抛给宿主,那么就需要改造。所以增加了setMessageBridge的set方法。
1、针对native->flutter问题,需要获取到methodChannel对象,那么plugin的onAttachedToEngine方法对channel进行了初始化,channel是私有的,那么先由plugin提供一个get方法,另外就是如何获取plugin对象了,就需要了解plugin初始化的时机。
flutterActivity中执行configureFlutterEngine方法,(oncreate执行,configureFlutterEngine方法也会一定执行)
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
if (delegate.isFlutterEngineFromHost()) {
// If the FlutterEngine was explicitly built and injected into this FlutterActivity, the
// builder should explicitly decide whether to automatically register plugins via the
// FlutterEngine's construction parameter or via the AndroidManifest metadata.
return;
}
GeneratedPluginRegister.registerGeneratedPlugins(flutterEngine);
}
主要就是register这个方法,看下他的内容
public static void registerGeneratedPlugins(@NonNull FlutterEngine flutterEngine) {
try {
Class<?> generatedPluginRegistrant =
Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
Method registrationMethod =
generatedPluginRegistrant.getDeclaredMethod("registerWith", FlutterEngine.class);
registrationMethod.invoke(null, flutterEngine);
} catch (Exception e) {
Log.e(
TAG,
"Tried to automatically register plugins with FlutterEngine ("
+ flutterEngine
+ ") but could not find or invoke the GeneratedPluginRegistrant.");
Log.e(TAG, "Received exception while registering", e);
}
}
正好调用了GeneratedPluginRegistrant的注册方法,把自动生成的plugin增加到PluginRegistry中,
@Keep
public final class GeneratedPlmuginRegistrant {
private static final String TAG = "GeneratedPluginRegistrant";
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
try {
flutterEngine.getPlugins().add(new com.example.bl_method_channel.BlMethodChannelPlugin());
} catch(Exception e) {
Log.e(TAG, "Error registering plugin bl_method_channel, com.example.bl_method_channel.BlMethodChannelPlugin", e);
}
}
}
所以由此推断,在activity覆盖configureFlutterEngine方法,利用flutterEngine参数拿到plugins的结合,获取目标plugin,强制转换类型。得到我们plugin,获取channel对象
(BlMethodChannelPlugin)flutterEngine?.getPlugins().get(BlMethodChannelPlugin::class.java)