flutter在release模式下解决Dart_LookupLibrary问题

前言:在某些情况下(尤其是用到flutter顶级函数时),debug模式正常,release模式发布却不正常。这个现象看上去很奇怪,很容易是代码混淆问题,实际上另有玄机。

环境:flutterSdk:3.3.0;android开发

正文:

一、先上报错内容:

Dart Error:Dart_LookupLibrary:library 'package:***.dart' not found
Could not resolve main entrypoint function.
Could not run the run main Dart entrypoint.
Could not create root isolate.

二、复现场景:

在使用PluginUtilities.getCallbackHandle回调时,我们需要传入一个顶级函数,如:

//文件1中代码段:
class ***Utils {
  static const _channel = const MethodChannel("***.***.channel");
  static void registerCallback(Function topCallback) {
    final callback = PluginUtilities.getCallbackHandle(topCallback);
    _channel.invokeMethod("registerCallback", callback?.toRawHandle());
  }
}

//文件1中代码段:(顶级函数,即dart函数不包括在类中,像main()函数那样,不是表面意思写在最上面)
void callbackTopMethod() {
  print('这是一个顶级函数');
} 


//文件2中代码段:
***Utils.registerCallback(callbackTopMethod); //这里传入顶级函数

这段代码,在debug模式下能够打印“这是一个顶级函数”,在release模式下无法打印“这是一个顶级函数”。且两种模式,运行都不崩溃。

三、解决办法:

解决办法有3种(推荐第三种):

①这是最新flutterSdk的机制,可以降低flutterSdk版本,如:3.0.5或者更低版本;

②使用profile(介于debug和release模式之间)模式打包:flutter run --profile --no-sound-null-safety

③在顶级函数上面加个注解:@pragma('vm:entry-point'),如:

@pragma('vm:entry-point')

void callbackTopMethod() {
  print('这是一个顶级函数');
} 

注解作用:在AOT编译(非debug,debug使用的JIT编译)中,如果没有被引用到的代码会丢弃掉,而AOP(切片)代码是不会被引用,需要使用该方式告诉编译器不要丢弃代码。

后记:当然,文中报错,其他场景也会遇到类似,比如使用了过旧或不兼容的三方sdk,酌情判断。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值