flutter aar中我用到了package_info、shared_preferences等須要和原生交互的插件,打包aar以後集成到安卓項目中發現這些插件無法工做,報錯了;java
錯誤信息大體意思是沒有註冊method channel之類的;markdown
找了一大圈,終於找到怎麼解決了,可能大多數在安卓中集成flutter以後是使用flutter開發的新頁面也就是 FlutterActivity,這樣的話不會有任何問題,由於在新版本中的FlutterActivity已經自動幫忙初始化第三方flutter插件了,可是flutterFragment裏面並無,須要本身去處理;this
個人項目中只是viewpager中有3個fragment,其中第三個是flutterFragment,而後承載這些fragment的activity繼承我本身的baseActivity,沒辦法改成FlutterActivity。spa
個人解決辦法是,在本身寫的Application裏面作第三方flutter插件的初始化,初始化插件須要傳入flutterEngine實例,
由於我作了預加載 flutterEngine,因此直接傳進去就好了:插件
flutterEngine = new FlutterEngine(this);
// Configure an initial route.
flutterEngine.getNavigationChannel().setInitialRoute("your/route/here");
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartEntrypoint.createDefault()
);
// 建立預加載的flutterEngine以後,在這裏爲證flutterEngine初始化用到的第三方flutter插件。
GeneratedPluginRegistrant.registerWith(flutterEngine);
// Cache the FlutterEngine to be used by FlutterActivity or FlutterFragment.
FlutterEngineCache
.getInstance()
.put("my_engine_id", flutterEngine);
複製代碼
預加載flutterEngine好處是 flutter的頁面加載會快不少。 這些東西官方文檔不會告訴你的,flutter的issues中也都是告訴你用最新版本的flutter插件,毛用都沒有,
我也搞不懂官方文檔爲啥很少寫點,難道他們只用flutterActivity,不會單獨使用flutterFragment?code