119 lines
3.8 KiB
PowerShell
119 lines
3.8 KiB
PowerShell
#!/usr/bin/env pwsh
|
||
# ============================================
|
||
# 一键重新打包 APK
|
||
# 使用场景:修改服务器地址、语音测评代码后快速打包
|
||
# ============================================
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
Write-Host " 一键重新打包 APK" -ForegroundColor Cyan
|
||
Write-Host "========================================" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
# 配置信息
|
||
$ProjectRoot = $PSScriptRoot
|
||
$SDKRoot = "D:\4_Part\HBuilder-Android\HBuilder-Integrate-AS"
|
||
$AppID = "__UNI__08E0C13"
|
||
|
||
# 步骤 1:检查配置文件
|
||
Write-Host "[ 1/4 ] 检查配置..." -ForegroundColor Yellow
|
||
$ConfigFile = "$ProjectRoot\src\utils\config.js"
|
||
if (Test-Path $ConfigFile) {
|
||
$ConfigContent = Get-Content $ConfigFile -Raw
|
||
if ($ConfigContent -match "DEFAULT_SERVER_HOST\s*=\s*'([^']+)'") {
|
||
$CurrentIP = $Matches[1]
|
||
Write-Host " 当前服务器地址: $CurrentIP" -ForegroundColor Green
|
||
}
|
||
} else {
|
||
Write-Host " 警告:找不到配置文件" -ForegroundColor Red
|
||
}
|
||
|
||
# 步骤 2:编译 uni-app 项目
|
||
Write-Host ""
|
||
Write-Host "[ 2/4 ] 编译 uni-app 项目..." -ForegroundColor Yellow
|
||
Write-Host " 预计时间: 2-3 分钟" -ForegroundColor Gray
|
||
|
||
Push-Location $ProjectRoot
|
||
try {
|
||
npm run build:app
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw "uni-app 编译失败"
|
||
}
|
||
Write-Host " ✓ uni-app 编译成功" -ForegroundColor Green
|
||
} catch {
|
||
Write-Host " ✗ 编译失败: $_" -ForegroundColor Red
|
||
Pop-Location
|
||
exit 1
|
||
} finally {
|
||
Pop-Location
|
||
}
|
||
|
||
# 步骤 3:集成资源到 Android Studio
|
||
Write-Host ""
|
||
Write-Host "[ 3/4 ] 集成资源..." -ForegroundColor Yellow
|
||
|
||
$IntegrateScript = "$ProjectRoot\integrate.ps1"
|
||
if (Test-Path $IntegrateScript) {
|
||
& $IntegrateScript
|
||
if ($LASTEXITCODE -ne 0) {
|
||
Write-Host " ✗ 资源集成失败" -ForegroundColor Red
|
||
exit 1
|
||
}
|
||
Write-Host " ✓ 资源集成成功" -ForegroundColor Green
|
||
} else {
|
||
Write-Host " 警告:找不到集成脚本,跳过此步骤" -ForegroundColor Yellow
|
||
}
|
||
|
||
# 步骤 4:构建 APK
|
||
Write-Host ""
|
||
Write-Host "[ 4/4 ] 构建 APK..." -ForegroundColor Yellow
|
||
Write-Host " 预计时间: 2-3 分钟" -ForegroundColor Gray
|
||
|
||
Push-Location $SDKRoot
|
||
try {
|
||
.\gradlew assembleDebug
|
||
if ($LASTEXITCODE -ne 0) {
|
||
throw "APK 构建失败"
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "========================================" -ForegroundColor Green
|
||
Write-Host " 构建成功!" -ForegroundColor Green
|
||
Write-Host "========================================" -ForegroundColor Green
|
||
|
||
$APKPath = "$SDKRoot\simpleDemo\build\outputs\apk\debug\simpleDemo-debug.apk"
|
||
if (Test-Path $APKPath) {
|
||
$APKInfo = Get-Item $APKPath
|
||
$SizeMB = [math]::Round($APKInfo.Length / 1MB, 2)
|
||
|
||
Write-Host ""
|
||
Write-Host "APK 位置:" -ForegroundColor Cyan
|
||
Write-Host " $APKPath" -ForegroundColor White
|
||
Write-Host ""
|
||
Write-Host "APK 大小: $SizeMB MB" -ForegroundColor Cyan
|
||
Write-Host "构建时间: $($APKInfo.LastWriteTime)" -ForegroundColor Cyan
|
||
Write-Host ""
|
||
|
||
# 询问是否打开文件夹
|
||
$Response = Read-Host "是否打开 APK 所在文件夹? (Y/n)"
|
||
if ($Response -ne 'n' -and $Response -ne 'N') {
|
||
explorer.exe "/select,$APKPath"
|
||
}
|
||
}
|
||
} catch {
|
||
Write-Host ""
|
||
Write-Host " ✗ 构建失败: $_" -ForegroundColor Red
|
||
Pop-Location
|
||
exit 1
|
||
} finally {
|
||
Pop-Location
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "提示:如需修改服务器地址,编辑以下文件:" -ForegroundColor Yellow
|
||
Write-Host " $ProjectRoot\src\utils\config.js" -ForegroundColor Gray
|
||
Write-Host ""
|
||
Write-Host "按任意键退出..."
|
||
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
|