Flutter安卓release包无法登录、按钮失效且报shared_preferences MissingPluginException错误
问题背景
我开发了一款类似Udemy的Flutter应用,iOS版本已发布到App Store且运行正常,但安卓端出现两处异常问题:
问题1:Release版本首页WhatsApp按钮无响应
- 调试模式(Android Studio连接模拟器/真机)所有功能正常,执行
flutter run --release运行、打包apk/aab后,首页带WhatsApp图标的RaisedButton点击无响应 - 首页截图:

问题2:登录页抛出MissingPluginException错误
用户尝试登录时抛出如下错误:
MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
注:代码中使用errrrrrrrrrrrrrooooor标记了错误输出位置。
查询资料得知该错误通常在shared_preferences无历史存储值时触发,但应用首次启动登录前并未调用shared_preferences,已在安卓manifest中添加网络权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
补充说明
执行flutter run --release命令时终端输出两条报错:
E/flutter (18074): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
E/flutter (18074):
E/flutter (18074): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method checkPermissionStatus on channel flutter.baseflow.com/permissions/methods)
已尝试执行flutter clean、flutter pub get等操作,问题未解决。
最新进展
将gradle升级到最新版本后,大部分设备运行正常,但仍有少量用户反馈出现相同错误。
彻底解决方案
1. 修复R8/ProGuard代码混淆规则
安卓Release打包默认开启代码混淆,插件原生方法会被误删,是少部分设备报错的核心诱因:
- 在
android/app/proguard-rules.pro文件中添加如下规则,保留相关插件的类和方法:
# 保留shared_preferences相关类 -keep class io.flutter.plugins.sharedpreferences.** { *; } # 保留permission_handler相关类 -keep class com.baseflow.permissionhandler.** { *; } # 保留url_launcher相关类(解决WhatsApp按钮无响应问题) -keep class io.flutter.plugins.urllauncher.** { *; } # 保留所有Flutter插件原生通道基础实现 -keep class io.flutter.plugin.** { *; } -keep class io.flutter.util.** { *; } -keep class io.flutter.view.** { *; } -keep class io.flutter.** { *; }
- 确认
android/app/build.gradle中release构建配置已正确引用混淆规则:
buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
2. 显式注册插件适配旧设备
如果使用的Flutter版本低于2.10,部分低版本安卓设备会出现插件注册延迟问题,在安卓项目的MainActivity.kt中显式初始化所有插件:
import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugins.GeneratedPluginRegistrant class MainActivity: FlutterActivity() { override fun configureFlutterEngine(flutterEngine: FlutterEngine) { GeneratedPluginRegistrant.registerWith(flutterEngine) } }
3. 升级所有依赖到稳定兼容版
将pubspec.yaml中相关依赖升级到最新稳定版,避免跨版本兼容问题:
dependencies: shared_preferences: ^2.2.2 permission_handler: ^10.2.0 url_launcher: ^6.1.14 device_info_plus: ^9.0.2
操作完成后重新执行flutter clean、flutter pub get,再打包Release版本即可覆盖所有设备场景。
内容的提问来源于stack exchange,提问作者slackgate

