param( [Parameter(Position = 0)] [ValidateSet('assembleDebug', 'installDebug', 'assembleRelease')] [string]$Task = 'assembleDebug' ) $ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $PSScriptRoot $documentsRoot = Split-Path -Parent $projectRoot $wowRoot = Join-Path $documentsRoot 'wow335a' $jdkRoot = Join-Path $wowRoot 'dungeon-export-work\tools\temurin-21' $portableSdkRoot = Join-Path $wowRoot '.android-sdk' $jdkHome = $null $configuredJavaHome = [Environment]::GetEnvironmentVariable('JAVA_HOME') if ($configuredJavaHome -and (Test-Path -LiteralPath (Join-Path $configuredJavaHome 'bin\java.exe'))) { $jdkHome = Get-Item -LiteralPath $configuredJavaHome } else { $jdkHome = Get-ChildItem -LiteralPath $jdkRoot -Directory -ErrorAction SilentlyContinue | Where-Object { Test-Path -LiteralPath (Join-Path $_.FullName 'bin\java.exe') } | Sort-Object Name -Descending | Select-Object -First 1 } if (-not $jdkHome) { throw "Java 21 was not found. Set JAVA_HOME or install the portable JDK under $jdkRoot" } $configuredSdkRoot = [Environment]::GetEnvironmentVariable('ANDROID_SDK_ROOT') if (-not $configuredSdkRoot) { $configuredSdkRoot = [Environment]::GetEnvironmentVariable('ANDROID_HOME') } $sdkRoot = if ($configuredSdkRoot -and (Test-Path -LiteralPath (Join-Path $configuredSdkRoot 'platforms\android-36\android.jar'))) { $configuredSdkRoot } else { $portableSdkRoot } if (-not (Test-Path -LiteralPath (Join-Path $sdkRoot 'platforms\android-36\android.jar'))) { throw "Android SDK Platform 36 was not found. Set ANDROID_SDK_ROOT or install the portable SDK under $portableSdkRoot" } $env:JAVA_HOME = $jdkHome.FullName $env:ANDROID_HOME = $sdkRoot $env:ANDROID_SDK_ROOT = $sdkRoot $gradleArguments = if ($Task -eq 'installDebug') { @('--no-daemon', 'installDebug') } else { @('--no-daemon', 'clean', $Task) } Push-Location (Join-Path $projectRoot 'android') try { & '.\gradlew.bat' @gradleArguments if ($LASTEXITCODE -ne 0) { throw "Gradle $Task failed with exit code $LASTEXITCODE" } } finally { Pop-Location }