diff --git a/build_flutter_app.dart b/build_flutter_app.dart index 3c5c342..78f2b34 100644 --- a/build_flutter_app.dart +++ b/build_flutter_app.dart @@ -1,10 +1,8 @@ import 'dart:io'; import 'dart:convert'; -import 'package:flutter/foundation.dart'; - void main(List arguments) async { - debugPrint('🚀 开始构建Flutter Android应用...'); + print('🚀 开始构建Flutter Android应用...'); // 默认构建 ARM64 + ARM32(推荐) final targetPlatform = arguments.contains('--arm64-only') @@ -21,28 +19,28 @@ void main(List arguments) async { // 生成version.json文件 await generateVersionJson(apkPath, targetPlatform); - debugPrint('✅ 构建完成!'); - debugPrint('📦 APK位置: $apkPath'); + print('✅ 构建完成!'); + print('📦 APK位置: $apkPath'); } catch (e) { - debugPrint('❌ 构建过程中出现错误: $e'); + print('❌ 构建过程中出现错误: $e'); exit(1); } } Future buildFlutterApp(String targetPlatform) async { - debugPrint('📱 正在构建Flutter Android应用...'); - debugPrint('🎯 目标平台: $targetPlatform'); + print('📱 正在构建Flutter Android应用...'); + print('🎯 目标平台: $targetPlatform'); // 清理构建缓存 - debugPrint('🧹 清理构建缓存...'); + print('🧹 清理构建缓存...'); await runFlutterCommand(['clean']); // 获取pub依赖 - debugPrint('📦 获取依赖...'); + print('📦 获取依赖...'); await runFlutterCommand(['pub', 'get']); // 构建APK(指定目标平台) - debugPrint('🔨 构建Release APK...'); + print('🔨 构建Release APK...'); await runFlutterCommand([ 'build', 'apk', @@ -50,7 +48,7 @@ Future buildFlutterApp(String targetPlatform) async { '--target-platform=$targetPlatform', ]); - debugPrint('✅ Flutter应用构建成功!'); + print('✅ Flutter应用构建成功!'); // 根据目标平台生成对应的APK文件名 String apkName; @@ -73,7 +71,7 @@ Future runFlutterCommand(List args) async { } Future generateVersionJson(String apkPath, String targetPlatform) async { - debugPrint('📄 正在生成version.json文件...'); + print('📄 正在生成version.json文件...'); final apkFile = File(apkPath); String fileSize = '未知'; @@ -81,7 +79,7 @@ Future generateVersionJson(String apkPath, String targetPlatform) async { if (await apkFile.exists()) { final apkSize = await apkFile.length(); fileSize = '${(apkSize / (1024 * 1024)).toStringAsFixed(1)}MB'; - debugPrint('📦 APK文件大小: $fileSize'); + print('📦 APK文件大小: $fileSize'); } final now = DateTime.now(); @@ -104,7 +102,7 @@ Future generateVersionJson(String apkPath, String targetPlatform) async { const JsonEncoder.withIndent(' ').convert(versionInfo), ); - debugPrint('✅ version.json文件生成成功!'); + print('✅ version.json文件生成成功!'); } String getBuildNumber() { @@ -117,5 +115,5 @@ Future checkFlutterEnvironment() async { if (result.exitCode != 0) { throw Exception('Flutter环境检查失败'); } - debugPrint('✅ Flutter环境正常'); + print('✅ Flutter环境正常'); } diff --git a/build_flutter_app_v2.dart b/build_flutter_app_v2.dart new file mode 100644 index 0000000..09a57d0 --- /dev/null +++ b/build_flutter_app_v2.dart @@ -0,0 +1,149 @@ +import 'dart:io'; +import 'dart:convert'; +import 'package:yaml/yaml.dart'; // 用于解析 pubspec.yaml +import 'package:args/args.dart'; // 用于解析命令行参数 + +// --- 配置区 --- +const String pubspecPath = 'pubspec.yaml'; +const String baseApkPath = 'build/app/outputs/flutter-apk/'; +const String sourceApkName = 'app-release.apk'; +// --- + +Future main(List rawArgs) async { + // 使用 args 包来专业地解析命令行参数 + final parser = ArgParser() + ..addFlag('arm64-only', negatable: false, help: '只构建 ARM64 架构的 APK。') + ..addOption( + 'description', + abbr: 'd', + help: '版本更新的描述。', + defaultsTo: '常规更新。', + ); + final args = parser.parse(rawArgs); + + final isArm64Only = args['arm64-only'] as bool; + final description = args['description'] as String; + + print('🚀 开始构建Flutter Android应用...'); + + final targetPlatform = isArm64Only + ? 'android-arm64' + : 'android-arm64,android-arm'; + final finalApkName = isArm64Only ? 'app-arm64-release.apk' : sourceApkName; + final finalApkPath = '$baseApkPath$finalApkName'; + + try { + await checkFlutterEnvironment(); + await buildFlutterApp(targetPlatform, finalApkPath); + await generateVersionJson(finalApkPath, targetPlatform, description); + + print('✅ 构建全部完成!'); + print('📦 APK位置: $finalApkPath'); + } catch (e) { + print('❌ 构建过程中出现错误: $e'); + exit(1); + } +} + +Future buildFlutterApp(String targetPlatform, String finalApkPath) async { + print('📱 正在构建Flutter Android应用...'); + print('🎯 目标平台: $targetPlatform'); + + await runProcess('flutter', ['clean']); + await runProcess('flutter', ['pub', 'get']); + + print('🔨 构建Release APK...'); + await runProcess('flutter', [ + 'build', + 'apk', + '--release', + '--target-platform=$targetPlatform', + ]); + + final sourceApkFile = File('$baseApkPath$sourceApkName'); + if (await sourceApkFile.exists()) { + print('✍️ 正在重命名APK为: ${finalApkPath.split('/').last}'); + await sourceApkFile.rename(finalApkPath); + } else { + throw Exception('构建产物 $sourceApkName 未找到!'); + } + + print('✅ Flutter应用构建成功!'); +} + +Future generateVersionJson( + String apkPath, + String targetPlatform, + String description, +) async { + print('📄 正在生成version.json文件...'); + + final apkFile = File(apkPath); + String fileSize = '未知'; + + if (await apkFile.exists()) { + final apkSize = await apkFile.length(); + fileSize = '${(apkSize / (1024 * 1024)).toStringAsFixed(1)}MB'; + print('📦 APK文件大小: $fileSize'); + } + + final pubspecInfo = getPubspecInfo(); + final buildTime = getFormattedDateTime(); + + final versionInfo = { + "version": pubspecInfo['version_name'], + "build_number": pubspecInfo['build_number'], + "build_time": buildTime, + "target_platform": targetPlatform, + "file_size": fileSize, + // [已修正] 从 apkPath 中提取文件名 + "url": "http://xhota.anxincloud.cn/problem/${apkPath.split('/').last}", + "description": description, + "platform": "android", + }; + + final jsonFile = File('${baseApkPath}version.json'); + await jsonFile.writeAsString( + const JsonEncoder.withIndent(' ').convert(versionInfo), + ); + + print('✅ version.json文件生成成功!'); +} + +// --- 辅助函数 --- + +Future runProcess(String executable, List arguments) async { + print('⚙️ 执行命令: $executable ${arguments.join(' ')}'); + final result = await Process.run(executable, arguments, runInShell: true); + if (result.exitCode != 0) { + print(result.stdout); + print(result.stderr); + throw Exception('命令执行失败: $executable ${arguments.join(' ')}'); + } +} + +Future checkFlutterEnvironment() async { + try { + await runProcess('flutter', ['--version']); + print('✅ Flutter环境正常'); + } catch (e) { + throw Exception('Flutter环境检查失败'); + } +} + +Map getPubspecInfo() { + final file = File(pubspecPath); + final content = file.readAsStringSync(); + final doc = loadYaml(content); + final versionString = doc['version'] as String; + final parts = versionString.split('+'); + return { + 'version_name': parts[0], + 'build_number': parts.length > 1 ? parts[1] : '1', + }; +} + +String getFormattedDateTime() { + final now = DateTime.now(); + return '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')} ${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}'; +}