fix: 修复 release 包 SharedPreferences 失效(R8 混淆改名 kotlin.coroutines.Continuation)

This commit is contained in:
venti1112 committed 2026-10-18 14:16:14 +08:00
1 parent 21925929e1
commit 80848f351d
2 files changed
+35 -4

No files matched your search

+16
View File
@@ -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.** { *; }
+19 -4
View File
@@ -128,8 +128,7 @@ class _SettingsPageState extends State<SettingsPage> {
if (!mounted) return; if (!mounted) return;
// 通过 Provider 落盘并通知,背景层随之交叉淡入 // 通过 Provider 落盘并通知,背景层随之交叉淡入
await context.read<WallpaperProvider>().setCustomWallpaper(path, type: 'image'); if (await _applyWallpaper(path, 'image')) _refreshUI();
_refreshUI();
} }
Future<void> _pickVideo() async { Future<void> _pickVideo() async {
@@ -143,8 +142,24 @@ class _SettingsPageState extends State<SettingsPage> {
if (path == null) return; if (path == null) return;
if (!mounted) return; if (!mounted) return;
await context.read<WallpaperProvider>().setCustomWallpaper(path, type: 'video'); if (await _applyWallpaper(path, 'video')) _refreshUI();
_refreshUI(); }
/// 落盘壁纸设置。失败时给出可见提示,避免「点了没反应」式的静默失败
/// (例如 release 包把插件方法混淆掉、SharedPreferences 整体不可用时)。
Future<bool> _applyWallpaper(String path, String type) async {
try {
await context.read<WallpaperProvider>().setCustomWallpaper(path, type: type);
return true;
} catch (e) {
debugPrint('保存壁纸设置失败: $e');
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('壁纸设置保存失败,请重试')),
);
}
return false;
}
} }
Future<String?> _saveFileToAssets(String sourcePath, String type) async { Future<String?> _saveFileToAssets(String sourcePath, String type) async {