Flutter_Boost v0.1.5升级 Android版本

Posted by lizubing1992 on 09-16,2019

1.新特性

  1. 支持Flutter 1.5
  2. 从老版的复用FlutterView,到新版的创建新的实例
  3. 新版创建新的的实例Flutter,可以避免白屏,闪屏,黑屏等现象
  4. 推荐使用ContainerLifeCycle observer来监听APP的生命周期
  5. 移除了xservice通信插件

2.主要的API变化

IPlatform

移除一下代码

Activity getMainActivity();
boolean startActivity(Context context,String url,int requestCode);
Map getSettings();

新增加

void registerPlugins(PluginRegistry registry) 方法
void openContainer(Context context,String url,Map<String,Object> urlParams,int requestCode,Map<String,Object> exts);
void closeContainer(IContainerRecord record, Map<String,Object> result, Map<String,Object> exts);
IFlutterEngineProvider engineProvider();
int whenEngineStart();

这个Api变化主要是在Application初始化的时候的修改

以上是Flutter_boost github上面的说明,具体可以查看

3.接入指南

接入的方式主要是在V0.0.420版本上面做的修改,V0.0.420的接入方式可以参考我之前的文章

旧版本 Application类的修改

public class MyApplication extends Application {

  @Override
  public void onCreate() {
    super.onCreate();
    FlutterBoostPlugin.init(new IPlatform() {
      @Override
      public Application getApplication() {
        return MyApplication.this;
      }

      /**
       * get the main activity, this activity should always at the bottom of task stack.
       */
      @Override
      public Activity getMainActivity() {
        return MainActivity.sRef.get();
      }

      @Override
      public boolean isDebug() {
        return false;
      }

      /**
       * start a new activity from flutter page, you may need a activity router.
       */
      @Override
      public boolean startActivity(Context context, String url, int requestCode) {
        return PageRouter.openPageByUrl(context,url,requestCode);
      }

      @Override
      public Map getSettings() {
        return null;
      }
    });
  }
}

可以继承FlutterApplication,直接使用,不继承FlutterApplication 的话需要在使用Flutter的Activity中onCreate加入 FlutterMain.startInitialization(this);
如下所示

@CallSuper
  public void onCreate() {
   FlutterMain.startInitialization(this);
    super.onCreate();
  }

其实对应的FlutterApplication是很简单的,主要还是在onCreate中

public class FlutterApplication extends Application {
  private Activity mCurrentActivity = null;

  public FlutterApplication() {
  }

  @CallSuper
  public void onCreate() {
    super.onCreate();
    FlutterMain.startInitialization(this);
  }

  public Activity getCurrentActivity() {
    return this.mCurrentActivity;
  }

  public void setCurrentActivity(Activity mCurrentActivity) {
    this.mCurrentActivity = mCurrentActivity;
  }
}

新版本 Application类的修改

public class MyApplication extends FlutterApplication {
    @Override
    public void onCreate() {
        super.onCreate();

        FlutterBoost.init(new Platform() {

            @Override
            public Application getApplication() {
                return MyApplication.this;
            }

            @Override
            public boolean isDebug() {
                return true;
            }

            @Override
            public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
                PageRouter.openPageByUrl(context,url,urlParams,requestCode);
            }

            @Override
            public IFlutterEngineProvider engineProvider() {
                return new BoostEngineProvider(){
                    @Override
                    public BoostFlutterEngine createEngine(Context context) {
                        return new BoostFlutterEngine(context, new DartExecutor.DartEntrypoint(
                                context.getResources().getAssets(),
                                FlutterMain.findAppBundlePath(context),
                                "main"),"/");
                    }
                };
            }

            @Override
            public int whenEngineStart() {
                return IMMEDIATELY;
            }
        });
    }
}

移除和新增代码,前面讲了,这里主要讲一下whenEngineStart()函数,其返回值有两个

 int IMMEDIATELY = 0;          //立即启动引擎
 int ANY_ACTIVITY_CREATED = 1; //当有任何Activity创建时,启动引擎

官方demo给的默认值是ANY_ACTIVITY_CREATED 创建Activity 都会启动,主要是控制引擎的初始化时机

engineProvider()返回一个Flutter引擎提供者,BoostEngineProvider,主要看其内部的createEngine()方法

 @Override
public BoostFlutterEngine createEngine(Context context) {
    return new BoostFlutterEngine(context, new DartExecutor.DartEntrypoint(
            context.getResources().getAssets(),
            FlutterMain.findAppBundlePath(context),
            "main"),"/");
}

这里大致讲一下其中的main字符,这个主要是在BoostFlutterEngine内部创建引擎的时候,设置对应的Flutter 执行的main函数也就是Flutter代码的执行的入口,斜杠主要是初始化路由用的

4.Flutter混合库函数升级Flutter_boost

修改版本信息v0.1.52,最新版

移除

FlutterBoost.handleOnStartPage();

打开页面不带参数

FlutterBoost.singleton.openPage("second", {}, animated: true);
<!--修改成-->
FlutterBoost.singleton.open("second")

打开页面带参数

  FlutterBoost.singleton.openPage("sample://nativePage", {
                  "query": {"aaa": "bbb"}
                }),
<!--修改成-->
  FlutterBoost.singleton.open("sample://nativePage", urlParams:{
                  "query": {"aaa": "bbb"}
                }),

关闭页面

FlutterBoost.singleton.closePageForContext(context);
<!--修改成-->
BoostContainerSettings settings = BoostContainer.of(context).settings;
FlutterBoost.singleton.close(settings.uniqueId,result: {"result":"data from second"});

5.iOS相关的升级

写好flutter插件相关的之后,再iOS端接入就比较简单了

再命令行执行

pod install

就会升级完flutter_boost的插件,主要再API层面的改动和Android差不多,就是open/close两个方法,具体如下

升级前
- (void)openPage:(NSString *)name params:(NSString *)json animated:(BOOL)animated completion:(void (^)(BOOL))completion

- (void)closePage:(NSString *)uid
         animated:(BOOL)animated
           params:(NSDictionary *)params
       completion:(void (^)(BOOL finished))completion;

升级后
#pragma mark - Boost 1.5
- (void)open:(NSString *)name
   urlParams:(NSDictionary *)params
        exts:(NSDictionary *)exts
  completion:(void (^)(BOOL))completion

- (void)close:(NSString *)uid
   result:(NSDictionary *)result
     exts:(NSDictionary *)exts
completion:(void (^)(BOOL finished))completion;

总结

  1. 目前发现在一些低端的Android机器,入14年15年的手机,还是存在页面跳转的Frame Render字样的出现,高端机器经过测试没这现象,这个查看flutter_boost的源码发现可以修改对应的方法可以避免
//继承BoostFlutterActivity修改
BoostFlutterActivity#createRenderingProgressCover()方法可以避免

2.同样的低端机器,再native界面回退到Flutter页面会存在白屏闪一下的现象,应该是在恢复Flutter相关的绘制时候导致的,期待后续的优化


0评论