You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter 3.29升级后Android应用无法启动编译报错求助

Flutter 3.29升级后flutter_barcode_scanner构建失败解决办法

问题原因

flutter_barcode_scanner 2.0.0版本依赖的PluginRegistry.Registrar类在Flutter 3.29中已被移除,该类属于旧版Flutter Android嵌入API,新版本已采用FlutterPluginBinding替代,导致编译时找不到符号,触发3处相关错误最终导致Gradle构建失败。

解决步骤

优先方案:升级插件至兼容版本

这是最可靠的解决方式:

  • 打开项目根目录的pubspec.yaml,找到flutter_barcode_scanner: ^2.0.0,将版本号替换为最新兼容版本(可通过flutter pub outdated命令查看可用更新)
  • 执行以下命令更新依赖:
    flutter pub get
    
  • 清理构建缓存后重新运行:
    flutter clean
    flutter run
    

应急方案:手动修改插件源码

若暂时无法升级插件,可修改插件代码适配新API:

  1. 定位到插件源码文件:
    /Users/macbook/.pub-cache/hosted/pub.dev/flutter_barcode_scanner-2.0.0/android/src/main/java/com/amolg/flutterbarcodescanner/FlutterBarcodeScannerPlugin.java
  2. 修改类实现:
    • 引入新的API依赖:
      import io.flutter.embedding.engine.plugins.FlutterPlugin;
      import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding;
      
    • 让类实现FlutterPlugin接口,替换原有的registerWith方法及相关构造逻辑:
      public class FlutterBarcodeScannerPlugin implements FlutterPlugin, MethodCallHandler {
          private MethodChannel channel;
          private Activity activity;
      
          @Override
          public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
              channel = new MethodChannel(binding.getBinaryMessenger(), "flutter_barcode_scanner");
              channel.setMethodCallHandler(this);
              this.activity = binding.getActivity();
          }
      
          @Override
          public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
              channel.setMethodCallHandler(null);
              channel = null;
              activity = null;
          }
      
          // 保留原有业务逻辑方法(如onMethodCall),移除所有涉及PluginRegistry.Registrar的代码
      }
      
  3. 删除所有包含PluginRegistry.Registrar的参数、构造方法及相关调用,确保代码适配新的嵌入逻辑。
  4. 执行flutter clean后重新构建项目。

备选方案:降级Flutter版本(不推荐)

若上述方案均无法实施,可临时降级Flutter至兼容插件的版本(如3.10及以下),但此方案会错过新版本特性,仅作为临时应急手段。

内容的提问来源于stack exchange,提问作者Moustapha Mbouombouo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:42:22