8 changed files with 131 additions and 185 deletions
@ -0,0 +1,119 @@ |
|||||||
|
import 'dart:io'; |
||||||
|
import 'dart:convert'; |
||||||
|
|
||||||
|
void main(List<String> arguments) async { |
||||||
|
print('🚀 开始构建Flutter Android应用...'); |
||||||
|
|
||||||
|
// 默认构建 ARM64 + ARM32(推荐) |
||||||
|
final targetPlatform = arguments.contains('--arm64-only') |
||||||
|
? 'android-arm64' |
||||||
|
: 'android-arm64,android-arm'; |
||||||
|
|
||||||
|
try { |
||||||
|
// 检查Flutter环境 |
||||||
|
await checkFlutterEnvironment(); |
||||||
|
|
||||||
|
// 构建Flutter Android应用 |
||||||
|
final apkPath = await buildFlutterApp(targetPlatform); |
||||||
|
|
||||||
|
// 生成version.json文件 |
||||||
|
await generateVersionJson(apkPath, targetPlatform); |
||||||
|
|
||||||
|
print('✅ 构建完成!'); |
||||||
|
print('📦 APK位置: $apkPath'); |
||||||
|
} catch (e) { |
||||||
|
print('❌ 构建过程中出现错误: $e'); |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Future<String> buildFlutterApp(String targetPlatform) async { |
||||||
|
print('📱 正在构建Flutter Android应用...'); |
||||||
|
print('🎯 目标平台: $targetPlatform'); |
||||||
|
|
||||||
|
// 清理构建缓存 |
||||||
|
print('🧹 清理构建缓存...'); |
||||||
|
await runFlutterCommand(['clean']); |
||||||
|
|
||||||
|
// 获取pub依赖 |
||||||
|
print('📦 获取依赖...'); |
||||||
|
await runFlutterCommand(['pub', 'get']); |
||||||
|
|
||||||
|
// 构建APK(指定目标平台) |
||||||
|
print('🔨 构建Release APK...'); |
||||||
|
await runFlutterCommand([ |
||||||
|
'build', |
||||||
|
'apk', |
||||||
|
'--release', |
||||||
|
'--target-platform=$targetPlatform', |
||||||
|
]); |
||||||
|
|
||||||
|
print('✅ Flutter应用构建成功!'); |
||||||
|
|
||||||
|
// 根据目标平台生成对应的APK文件名 |
||||||
|
String apkName; |
||||||
|
if (targetPlatform == 'android-arm64') { |
||||||
|
apkName = 'app-arm64-release.apk'; |
||||||
|
} else if (targetPlatform == 'android-arm') { |
||||||
|
apkName = 'app-arm-release.apk'; |
||||||
|
} else { |
||||||
|
apkName = 'app-release.apk'; |
||||||
|
} |
||||||
|
|
||||||
|
return 'build/app/outputs/flutter-apk/$apkName'; |
||||||
|
} |
||||||
|
|
||||||
|
Future<void> runFlutterCommand(List<String> args) async { |
||||||
|
final result = await Process.run('flutter', args, runInShell: true); |
||||||
|
if (result.exitCode != 0) { |
||||||
|
throw Exception('Flutter命令执行失败: ${result.stderr}'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Future<void> generateVersionJson(String apkPath, String targetPlatform) 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 now = DateTime.now(); |
||||||
|
final buildTime = |
||||||
|
'${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')}'; |
||||||
|
|
||||||
|
final versionInfo = { |
||||||
|
"version": "1.0.1", |
||||||
|
"build_number": getBuildNumber(), |
||||||
|
"build_time": buildTime, |
||||||
|
"target_platform": targetPlatform, |
||||||
|
"file_size": fileSize, |
||||||
|
"url": "http://xhota.anxincloud.cn/problem/app-release-problem.apk", |
||||||
|
"description": "1. 修复了xxx bug。\n2. 新增了xxx功能。\n3. 优化了用户体验。", |
||||||
|
"platform": "android", |
||||||
|
}; |
||||||
|
|
||||||
|
final jsonFile = File('build/app/outputs/flutter-apk/version.json'); |
||||||
|
await jsonFile.writeAsString( |
||||||
|
const JsonEncoder.withIndent(' ').convert(versionInfo), |
||||||
|
); |
||||||
|
|
||||||
|
print('✅ version.json文件生成成功!'); |
||||||
|
} |
||||||
|
|
||||||
|
String getBuildNumber() { |
||||||
|
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')}'; |
||||||
|
} |
||||||
|
|
||||||
|
Future<void> checkFlutterEnvironment() async { |
||||||
|
final result = await Process.run('flutter', ['--version'], runInShell: true); |
||||||
|
if (result.exitCode != 0) { |
||||||
|
throw Exception('Flutter环境检查失败'); |
||||||
|
} |
||||||
|
print('✅ Flutter环境正常'); |
||||||
|
} |
Loading…
Reference in new issue