android_build.ps1
3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#################################################
# 脚本代码中不能含有中文,否则默认PowerShell环境可能会报错
#################################################
# 1. 读取脚本参数
#-------------------------------------------
param(
# app打包环境:dev, prod
[Parameter(Mandatory = $true)]
[ValidateSet("dev", "pro")]
[string]$appenv,
# App版本,例如:1.0.2512181
[Parameter(Mandatory = $true)]
[string]$version,
# 下载地址
[string]$url = "https://bxe.obs.cn-north-4.myhuaweicloud.com/fronts/material/xehybrid/assets/basepkg/base-$appenv.zip",
# 下载保存文件的路径
[string]$downloadPath = "C:\Users\tang-\StudioProjects\appframe\assets\base-$appenv.zip",
# App路径
[string]$appPath = "C:\Users\tang-\StudioProjects\appframe"
)
# 2. 验证目录存在
#-------------------------------------------
if (-not (Test-Path $appPath))
{
Write-Error "appPath not exists: $appPath"
exit 1
}
# 确保下载目录存在
$downloadDir = Split-Path $downloadPath -Parent
if (-not (Test-Path $downloadDir))
{
Write-Error "the path for save file does not exists: $appPath"
exit 1
}
# 3. 下载文件(添加错误处理)
#-------------------------------------------
try
{
Write-Host "downloading from : $url" -ForegroundColor Green
Write-Host "save to : $downloadPath" -ForegroundColor Green
Invoke-WebRequest -Uri $url -OutFile $downloadPath -ErrorAction Stop
if (Test-Path $downloadPath)
{
Write-Host "download success!" -ForegroundColor Green
}
else
{
Write-Error "download failed!"
exit 1
}
}
catch
{
Write-Error "downloading failed: $_"
exit 1
}
# 4. 执行flutter构建
#-------------------------------------------
try
{
Set-Location -Path $appPath -ErrorAction Stop
Write-Host "flutter building ..." -ForegroundColor Green
Write-Host "appenv: $appenv, version: $version" -ForegroundColor Cyan
flutter build apk `
--release `
--split-per-abi `
--target-platform android-arm64 `
--dart-define=env=$appenv `
--dart-define=version=$version
if ($LASTEXITCODE -eq 0)
{
Write-Host "build success!" -ForegroundColor Green
}
else
{
Write-Error "build failed!"
exit $LASTEXITCODE
}
}
catch
{
Write-Error "building failed: $_"
exit 1
}
# 5. 复制生成新命名的APK文件
# "build\app\outputs\flutter-apk\app-arm64-v8a-release.apk"
#-------------------------------------------
try
{
$apkOutputDir = Join-Path $appPath "build\app\outputs\flutter-apk"
$sourceApk = Join-Path $apkOutputDir "app-arm64-v8a-release.apk"
$newApkName = "bxeapp-$appenv-$version.apk"
$destApkPath = Join-Path $apkOutputDir $newApkName
if (Test-Path $sourceApk)
{
Copy-Item -Path $sourceApk -Destination $destApkPath -Force
Write-Host "Copied and renamed: $newApkName" -ForegroundColor Yellow
}
else
{
Write-Warning "Source APK not found: $sourceApk"
}
}
catch
{
Write-Error "Failed to copy and rename APK: $_"
}