Flutter Release模式报错:找不到home_widget_callback_dispatcher.dart库
解决home_widget在Release模式下Dart_LookupLibrary错误的问题
问题描述
使用home_widget包(版本^0.1.6)开发桌面组件,flutter run --profile模式可正常运行,但执行flutter run --release时出现Dart_LookupLibrary错误,提示找不到package:home_widget/home_widget_callback_dispatcher.dart库,无法发布到Play商店。
错误日志
E/flutter (24125): [ERROR:flutter/shell/common/shell.cc(93)] Dart Error: Dart_LookupLibrary: library 'package:home_widget/home_widget_callback_dispatcher.dart' not found. E/flutter (24125): [ERROR:flutter/runtime/dart_isolate.cc(668)] Could not resolve main entrypoint function. E/flutter (24125): [ERROR:flutter/runtime/dart_isolate.cc(167)] Could not run the run main Dart entrypoint. E/flutter (24125): [ERROR:flutter/runtime/runtime_controller.cc(385)] Could not create root isolate. E/flutter (24125): [ERROR:flutter/shell/common/shell.cc(604)] Could not launch engine with configuration.
相关配置
AndroidManifest.xml
<!-- Your Background receiver and service goes here --> <receiver android:name="AppWidgetProvider" android:exported="true"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver> <receiver android:name="es.antonborri.home_widget.HomeWidgetBackgroundReceiver" android:exported="true"> <intent-filter> <action android:name="es.antonborri.home_widget.action.BACKGROUND" /> </intent-filter> </receiver> <service android:name="es.antonborri.home_widget.HomeWidgetBackgroundService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
pubspec.yaml依赖
home_widget: ^0.1.6 workmanager: ^0.5.1
flutter doctor信息
[√] Flutter (Channel stable, 3.3.9, on Microsoft Windows [Version 10.0.22623.1020], locale tr-TR) [√] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [√] Chrome - develop for the web [!] Visual Studio - develop for Windows (Visual Studio Enterprise 2022 17.0.4) X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components: MSVC v142 - VS 2019 C++ x64/x86 build tools - If there are multiple build tool versions available, install the latest C++ CMake tools for Windows Windows 10 SDK [√] Android Studio (version 2021.2) [√] Connected device (4 available) [√] HTTP Host Availability
解决办法
确认回调分发文件存在
检查项目根目录(与main.dart同级)是否存在home_widget_callback_dispatcher.dart文件,内容示例:import 'package:flutter/services.dart'; import 'package:home_widget/home_widget.dart'; void callbackDispatcher() { WidgetsFlutterBinding.ensureInitialized(); HomeWidget.registerBackgroundCallback(backgroundCallback); } void backgroundCallback(Uri? uri) { // 这里添加桌面组件的回调处理逻辑 if (uri?.host == 'updateWidget') { // 执行组件更新操作 } }Release模式下R8混淆会移除未被引用的文件,必须确保该文件被正确注册和引用。
添加混淆规则
在android/app/proguard-rules.pro文件中添加以下规则,防止home_widget相关代码被混淆移除:-keep class es.antonborri.home_widget.** { *; } -keepclassmembers class ** { @es.antonborri.home_widget.HomeWidgetActionHandler <methods>; } -keep class io.flutter.app.FlutterApplication { *; } -keep class io.flutter.plugin.common.MethodChannel { *; }修正AppWidgetProvider配置
不要直接使用系统的AppWidgetProvider,需自定义实现该类,修改AndroidManifest.xml中的配置:<receiver android:name=".MyAppWidgetProvider" android:exported="true"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver>在自定义的
MyAppWidgetProvider中正确调用home_widget的初始化和更新方法。清理缓存并重新构建
执行以下命令彻底清理项目缓存后重新构建Release版本:flutter clean flutter pub get flutter run --release升级包版本
考虑将home_widget升级到最新版本,旧版本可能存在Release模式下的兼容性问题,新版本通常会修复此类问题。
内容的提问来源于stack exchange,提问作者Ramazan HABER
相关产品推荐
相关产品推荐

