前言:在某些情况下(尤其是用到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,酌情判断。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐