param( [ValidateSet('dev','prod')][string]$Environment = 'dev', [string]$GradlePath = '', [string]$Architectures = 'arm64-v8a' ) $ErrorActionPreference = 'Stop' $taskApp = Split-Path $PSScriptRoot -Parent $taskRepo = Split-Path $taskApp -Parent if (!(Test-Path -LiteralPath (Join-Path $taskApp 'app.config.js'))) { throw 'Expo App root not found' } if (!$env:JAVA_HOME -or !$env:ANDROID_HOME) { throw 'Set JAVA_HOME (Java 17+) and ANDROID_HOME first.' } if ($Environment -eq 'prod') { foreach ($name in @('APP_API_URL','APP_UPDATE_ORIGIN','CMVR_KEYSTORE','CMVR_STORE_PASSWORD','CMVR_KEY_ALIAS','CMVR_KEY_PASSWORD')) { if (![Environment]::GetEnvironmentVariable($name)) { throw "Missing production environment variable: $name" } } if (!(Test-Path -LiteralPath (Join-Path $taskApp 'certs/prod.pem'))) { throw 'Configure the production OTA public certificate first. Never use the development key.' } } $env:APP_ENV = $Environment $env:EXPO_NO_TELEMETRY = '1' $taskTmp = Join-Path $taskRepo '.codex-tmp/jtmp' New-Item -ItemType Directory -Path $taskTmp -Force | Out-Null $env:JAVA_TOOL_OPTIONS = "$env:JAVA_TOOL_OPTIONS -Dfile.encoding=UTF-8 -Djava.io.tmpdir=$taskTmp -Djdk.net.unixdomain.tmpdir=$taskTmp" Push-Location $taskApp try { & npm.cmd run typecheck if ($LASTEXITCODE) { throw 'Type checking failed' } & npm.cmd test if ($LASTEXITCODE) { throw 'Regression tests failed' } # Do not carry a signing configuration or application ID across environments. $taskGenerated = Join-Path $taskApp 'android' $taskGeneratedGradle = Join-Path $taskGenerated 'app/build.gradle' $taskExpectedPackage = if ($Environment -eq 'prod') { 'cn.cmvr.wms' } else { 'cn.cmvr.wms.dev' } if (Test-Path -LiteralPath $taskGeneratedGradle) { $taskGradleSource = Get-Content -LiteralPath $taskGeneratedGradle -Raw if ($taskGradleSource -notmatch ('applicationId\s+["'']' + [regex]::Escape($taskExpectedPackage) + '["'']')) { $taskResolvedGenerated = (Resolve-Path -LiteralPath $taskGenerated).Path if ($taskResolvedGenerated -ne (Join-Path (Resolve-Path -LiteralPath $taskApp).Path 'android')) { throw 'Unexpected generated Android directory' } Move-Item -LiteralPath $taskResolvedGenerated -Destination (Join-Path $taskRepo ('.codex-tmp/expo-android-' + [Guid]::NewGuid().ToString('N'))) } } # This is Expo's generated directory in mobile-app, never the existing android-app. & npx.cmd expo prebuild --platform android --no-install if ($LASTEXITCODE) { throw 'Native configuration generation failed' } # Ninja 1.10 can build fresh objects but cannot reopen its absolute-header # dependency cache under this long Windows checkout. Preserve only our own # generated native cache and reconfigure; never change machine security settings. if ([Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT) { $taskNativeCache = Join-Path $taskApp 'android/app/.cxx' if (Test-Path -LiteralPath $taskNativeCache) { $taskResolvedCache = (Resolve-Path -LiteralPath $taskNativeCache).Path $taskAllowedApp = (Resolve-Path -LiteralPath (Join-Path $taskApp 'android/app')).Path if ($taskResolvedCache -ne (Join-Path $taskAllowedApp '.cxx')) { throw 'Unexpected native cache path' } $taskCacheBackup = Join-Path $taskRepo ('.codex-tmp/expo-cxx-' + [Guid]::NewGuid().ToString('N')) Move-Item -LiteralPath $taskResolvedCache -Destination $taskCacheBackup } } Push-Location (Join-Path $taskApp 'android') try { if ($GradlePath) { & $GradlePath ':app:assembleRelease' '--no-daemon' "-PreactNativeArchitectures=$Architectures" } else { & ./gradlew.bat ':app:assembleRelease' '--no-daemon' "-PreactNativeArchitectures=$Architectures" } if ($LASTEXITCODE) { throw 'Android build failed' } } finally { Pop-Location } Write-Output (Join-Path $taskApp 'android/app/build/outputs/apk/release/app-release.apk') } finally { Pop-Location }