apps\build-all.ps1¶
源码路径:
apps\build-all.ps1· 91 行
# Build all five Android apps (Player / Store / HUB / IOT / online).
#
# Usage:
# powershell -ExecutionPolicy Bypass -File .\apps\build-all.ps1
# powershell -ExecutionPolicy Bypass -File .\apps\build-all.ps1 -Task assembleRelease
#
# ASCII only on purpose: Windows PowerShell 5.1 reads a script file as the
# system ANSI codepage (GBK here), so non-ASCII text inside string literals gets
# mangled into a parse error. Keep every byte below 0x80 in this file.
#
# Why this script exists: Gradle itself is fine, but the **JDK version** is a trap.
# This machine only has JDK 26, and AGP 8.7.3 does not support it; the failure is
# a single line "What went wrong: 26.0.1" with no hint about the JDK. So we pin
# JAVA_HOME to .tools\jdk21 here and explain how to fetch it when missing.
[CmdletBinding()]
param(
[string]$Task = 'assembleDebug',
[string]$Jdk = ''
)
$ErrorActionPreference = 'Stop'
$ws = Split-Path -Parent $PSScriptRoot
$apps = Join-Path $ws 'apps'
# ------------------------------------------------------------------ JDK 21
if (-not $Jdk) {
$cand = Get-ChildItem (Join-Path $ws '.tools\jdk21') -Directory -ErrorAction SilentlyContinue |
Where-Object { Test-Path (Join-Path $_.FullName 'bin\java.exe') } |
Select-Object -First 1
if ($cand) { $Jdk = $cand.FullName }
}
if (-not $Jdk -or -not (Test-Path (Join-Path $Jdk 'bin\java.exe'))) {
# No here-string: its terminator must be at column 0, and this block is
# indented. Single-quoted array entries also cannot contain single quotes.
$msg = @(
"JDK 21 not found. AGP 8.7.3 does not support JDK 26 (the only error is '26.0.1').",
"Fetch one into the workspace (does not touch system dirs):",
"",
" `$u = 'https://mirrors.tuna.tsinghua.edu.cn/Adoptium/21/jdk/x64/windows/OpenJDK21U-jdk_x64_windows_hotspot_21.0.12.1_1.zip'",
" `$z = `"`$env:TEMP\jdk21.zip`"; Invoke-WebRequest `$u -OutFile `$z",
" Expand-Archive `$z `"`$PWD\.tools\jdk21`"",
"",
"Then re-run this script with -Jdk pointing at the extracted jdk-21.x dir."
) -join "`n"
throw $msg
}
$env:JAVA_HOME = $Jdk
$env:Path = "$Jdk\bin;$env:Path"
Write-Host "JAVA_HOME = $Jdk" -ForegroundColor DarkGray
# ------------------------------------------------------------------ SDK
if (-not $env:ANDROID_HOME) {
foreach ($p in @("$env:LOCALAPPDATA\Android\Sdk", 'C:\Android\Sdk')) {
if (Test-Path $p) { $env:ANDROID_HOME = $p; break }
}
}
if (-not $env:ANDROID_HOME) { throw 'Android SDK not found; set ANDROID_HOME.' }
$env:ANDROID_SDK_ROOT = $env:ANDROID_HOME
Write-Host "ANDROID_HOME = $($env:ANDROID_HOME)" -ForegroundColor DarkGray
# ------------------------------------------------------------------ Gradle
# Prefer the cached 8.9 distribution so we do not download another 100 MB.
$gradle = Get-ChildItem "$env:USERPROFILE\.gradle\wrapper\dists\gradle-8.9-bin" -Recurse -Depth 3 -Filter 'gradle.bat' -ErrorAction SilentlyContinue |
Select-Object -First 1
if (-not $gradle) {
throw 'Cached Gradle 8.9 not found. Run any gradle build once, or install Gradle 8.9.'
}
Write-Host "gradle = $($gradle.FullName)" -ForegroundColor DarkGray
# ------------------------------------------------------------------ build
Push-Location $apps
try {
& $gradle.FullName $Task --console=plain
if ($LASTEXITCODE -ne 0) { throw "gradle $Task failed (exit $LASTEXITCODE)" }
} finally { Pop-Location }
# ------------------------------------------------------------------ publish
$sub = if ($Task -like '*Release*') { 'release' } else { 'debug' }
$out = Join-Path $ws 'dist-android-apps'
New-Item -ItemType Directory -Force -Path $out | Out-Null
foreach ($n in @('player', 'store', 'hub', 'iot', 'online')) {
$src = Join-Path $apps "$n\build\outputs\apk\$sub\$n-$sub.apk"
if (-not (Test-Path $src)) { Write-Warning "missing artifact: $src"; continue }
$dst = Join-Path $out "nfarms-$n-0.1.0-$sub.apk"
Copy-Item $src $dst -Force
Write-Host (" {0,-7} {1,7:N2} MB {2}" -f $n, ((Get-Item $dst).Length / 1MB), $dst) -ForegroundColor Green
}
Write-Host ''
Write-Host "Install: adb install -r $out\nfarms-player-0.1.0-$sub.apk (same for the others)"