From 84e518820e45f89407e4024eccf1f38d13950218 Mon Sep 17 00:00:00 2001 From: "DESKTOP-I3JPKHK\\wy" <1111> Date: Thu, 27 Nov 2025 14:47:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/app_upgrade_plugin_method_channel.dart | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/app_upgrade_plugin_method_channel.dart b/lib/app_upgrade_plugin_method_channel.dart index b18266c..813f5cf 100644 --- a/lib/app_upgrade_plugin_method_channel.dart +++ b/lib/app_upgrade_plugin_method_channel.dart @@ -284,6 +284,9 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform { } debugPrint('下载路径: $savePath'); + // 下载前清理历史APK文件 + await _clearHistoryApkFiles(savePath); + // 创建文件名 final fileName = 'app_${DateTime.now().millisecondsSinceEpoch}.apk'; final filePath = '$savePath/$fileName'; @@ -349,6 +352,38 @@ class MethodChannelAppUpgradePlugin extends AppUpgradePluginPlatform { } } + /// 清理下载目录中的历史APK文件 + /// [downloadPath] 下载目录路径 + Future _clearHistoryApkFiles(String downloadPath) async { + try { + final dir = Directory(downloadPath); + if (!await dir.exists()) { + debugPrint('下载目录不存在,跳过清理: $downloadPath'); + return; + } + + final files = await dir.list().toList(); + int deletedCount = 0; + for (final file in files) { + if (file is File && file.path.endsWith('.apk')) { + try { + await file.delete(); + deletedCount++; + debugPrint('已删除历史APK文件: ${file.path}'); + } catch (e) { + debugPrint('删除历史APK文件失败: ${file.path}, 错误: $e'); + } + } + } + if (deletedCount > 0) { + debugPrint('清理完成,共删除 $deletedCount 个历史APK文件'); + } + } catch (e) { + debugPrint('清理历史APK文件失败: $e'); + // 清理失败不影响下载流程,继续执行 + } + } + @override Future installApk(String filePath) async { if (!Platform.isAndroid) {