From 80848f351d449a7c1f02336b75fd5cc587a88ada Mon Sep 17 00:00:00 2001 From: venti1112 <3788365896@qq.com> Date: Sun, 18 Oct 2026 14:16:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20release=20=E5=8C=85?= =?UTF-8?q?=20SharedPreferences=20=E5=A4=B1=E6=95=88=EF=BC=88R8=20?= =?UTF-8?q?=E6=B7=B7=E6=B7=86=E6=94=B9=E5=90=8D=20kotlin.coroutines.Contin?= =?UTF-8?q?uation=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/proguard-rules.pro | 16 ++++++++++++++++ lib/features/settings/settings_page.dart | 23 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 android/app/proguard-rules.pro diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro new file mode 100644 index 0000000..a08a582 --- /dev/null +++ b/android/app/proguard-rules.pro @@ -0,0 +1,16 @@ +# Flutter 的 release 构建默认开启 R8 混淆(本文件存在时会自动加入 proguardFiles)。 +# +# shared_preferences 等插件用 JNIGen 生成 Dart 绑定,Dart 侧按「类名 + 方法名 + +# 描述符」字符串查找 Java 方法,描述符里含 kotlin.coroutines.Continuation 等 +# 类型名。R8 把它们改名(实测 Continuation -> vd)后运行期就抛 NoSuchMethodError, +# SharedPreferences 整体失效:壁纸保存不上,主题、服务器列表也无法持久化。 +# debug 不开 R8,所以只有 release 出问题。 + +# Kotlin 标准库类名会出现在 JNIGen 的方法描述符里 +-keepnames class kotlin.** + +# suspend 方法的末端参数,名字写死在 Dart 绑定里,连成员一起保留最稳妥 +-keep class kotlin.coroutines.Continuation { *; } + +# Dart 侧按全限定类名查找插件类(如 SharedPreferencesApiRegistrar) +-keepnames class io.flutter.plugins.** { *; } diff --git a/lib/features/settings/settings_page.dart b/lib/features/settings/settings_page.dart index 7859dcc..bb01ec7 100644 --- a/lib/features/settings/settings_page.dart +++ b/lib/features/settings/settings_page.dart @@ -128,8 +128,7 @@ class _SettingsPageState extends State { if (!mounted) return; // 通过 Provider 落盘并通知,背景层随之交叉淡入 - await context.read().setCustomWallpaper(path, type: 'image'); - _refreshUI(); + if (await _applyWallpaper(path, 'image')) _refreshUI(); } Future _pickVideo() async { @@ -143,8 +142,24 @@ class _SettingsPageState extends State { if (path == null) return; if (!mounted) return; - await context.read().setCustomWallpaper(path, type: 'video'); - _refreshUI(); + if (await _applyWallpaper(path, 'video')) _refreshUI(); + } + + /// 落盘壁纸设置。失败时给出可见提示,避免「点了没反应」式的静默失败 + /// (例如 release 包把插件方法混淆掉、SharedPreferences 整体不可用时)。 + Future _applyWallpaper(String path, String type) async { + try { + await context.read().setCustomWallpaper(path, type: type); + return true; + } catch (e) { + debugPrint('保存壁纸设置失败: $e'); + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('壁纸设置保存失败,请重试')), + ); + } + return false; + } } Future _saveFileToAssets(String sourcePath, String type) async {