| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- param(
- [string]$ProjectPath = ".\PRS.Avalonia.Android.csproj",
- [string]$Configuration = "Release",
- [string]$Framework = "net8.0-android",
- [string]$AppId = "com.frogsoftware.avalonia",
- [int]$VersionCode = 858100,
- [string]$VersionName = "8.58.1",
- [string]$AndroidSDK = "/Users/frankvandenbos/Library/Android/sdk",
- # Keystore
- [string]$KeystorePath = ".\upload-keystore.jks",
- [string]$KeyAlias = "upload",
- # If omitted, you will be prompted without echo.
- [string]$KeystorePass = $null,
- [string]$KeyPass = $null,
- # Optional switches
- [switch]$HardClean = $true,
- [switch]$ClearNuGet = $false
- )
- $ErrorActionPreference = "Stop"
- Write-Host "=== Android AAB publish for $ProjectPath ===`n"
- # --- Helpers -----------------------------------------------------------------
- function Test-Cmd($cmd) {
- $null -ne (Get-Command $cmd -ErrorAction SilentlyContinue)
- }
- function Remove-DirIfExists([string]$path) {
- if (Test-Path $path) {
- Write-Host "Removing $path"
- Remove-Item -LiteralPath $path -Recurse -Force -ErrorAction SilentlyContinue
- }
- }
- function Clean-BuildArtifacts([string]$projectDir) {
- Remove-DirIfExists (Join-Path $projectDir "bin")
- Remove-DirIfExists (Join-Path $projectDir "obj")
- Write-Host "Removing nested bin/obj under $projectDir ..."
- Get-ChildItem -LiteralPath $projectDir -Directory -Recurse -Force |
- Where-Object { $_.Name -in @("bin","obj") } |
- ForEach-Object { Remove-DirIfExists $_.FullName }
- }
- function Clear-NuGetCaches {
- Write-Host "Clearing NuGet caches..."
- & dotnet nuget locals all --clear | Write-Host
- if ($LASTEXITCODE -ne 0) { throw "dotnet nuget locals --clear failed." }
- }
- function Restore-Project([string]$projectPath) {
- Write-Host "Restoring..."
- & dotnet restore "$projectPath"
- if ($LASTEXITCODE -ne 0) { throw "dotnet restore failed." }
- }
- function Publish-Project {
- param(
- [string]$projectPath,
- [string]$Configuration,
- [string]$Framework,
- [string]$AndroidSDK,
- [string]$KeystorePath,
- [string]$KeystorePass,
- [string]$KeyAlias,
- [string]$KeyPass,
- [string]$AppId,
- [int]$VersionCode,
- [string]$VersionName
- )
-
- Write-Host "Publishing signed .aab..."
- & dotnet publish "$projectPath" `
- -c "$Configuration" `
- -f "$Framework" `
- -v diag `
- /restore:false `
- /p:AndroidKeyStore=true `
- /p:AndroidSdkDirectory="$AndroidSDK" `
- /p:AndroidSigningKeyStore="$KeystorePath" `
- /p:AndroidSigningStorePass="$KeystorePass" `
- /p:AndroidSigningKeyAlias="$KeyAlias" `
- /p:AndroidSigningKeyPass="$KeyPass" `
- /p:AndroidPackageFormat=aab `
- /p:ApplicationId="$AppId" `
- /p:VersionCode="$VersionCode" `
- /p:VersionName="$VersionName"
- if ($LASTEXITCODE -ne 0) { throw "dotnet publish failed." }
- }
- function To-PlainText([SecureString]$sec) {
- if ($null -eq $sec) { return $null }
- $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($sec)
- try { return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) }
- finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) }
- }
- # --- Preconditions ------------------------------------------------------------
- if (-not (Test-Cmd "dotnet")) {
- throw "dotnet not found on PATH. Install .NET 9 SDK."
- }
- if (-not (Test-Path $AndroidSDK)) {
- throw "Android SDK directory not found: $AndroidSDK"
- }
- if (-not (Test-Path $ProjectPath)) {
- throw "Project not found at $ProjectPath"
- }
- $ProjectDir = Split-Path -Parent (Resolve-Path $ProjectPath)
- # keytool/jarsigner (only required if you want to auto-create keystore)
- $HasKeytool = Test-Cmd "keytool"
- if (-not $HasKeytool) {
- Write-Warning "keytool not found on PATH. Keystore auto-creation is disabled."
- }
- # --- Keystore and secrets -----------------------------------------------------
- if (-not (Test-Path $KeystorePath)) {
- throw "Keystore not found at '$KeystorePath'. Create it once (recommended) and re-run."
- # If you want auto-creation, uncomment and implement the keytool section here.
- }
- if (-not $KeystorePass) {
- $sec = Read-Host -AsSecureString "Enter keystore password"
- $KeystorePass = To-PlainText $sec
- }
- if (-not $KeyPass) {
- $sec2 = Read-Host -AsSecureString "Enter key password (press Enter to reuse keystore password)"
- $KeyPass = To-PlainText $sec2
- if ([string]::IsNullOrWhiteSpace($KeyPass)) { $KeyPass = $KeystorePass }
- }
- # --- Clean / Restore / Publish ------------------------------------------------
- if ($HardClean) { Clean-BuildArtifacts $ProjectDir }
- if ($ClearNuGet) { Clear-NuGetCaches }
- Restore-Project $ProjectPath
- $KeystoreFullPath = (Resolve-Path $KeystorePath).Path
- Write-Host "Keystore: $KeystoreFullPath"
- Write-Host "Alias: $KeyAlias"
- Write-Host "KeyPass empty? $([string]::IsNullOrWhiteSpace($KeyPass))"
- Publish-Project $ProjectPath `
- -Configuration $Configuration `
- -Framework $Framework `
- -AndroidSDK $AndroidSDK `
- -KeystorePath $KeystorePath `
- -KeystorePass $KeystorePass `
- -KeyAlias $KeyAlias `Ay
- -KeyPass $KeyPass `
- -AppId $AppId `
- -VersionCode $VersionCode `
- -VersionName $VersionName
- # --- Locate output ------------------------------------------------------------
- $binRoot = Join-Path $ProjectDir "bin\$Configuration\$Framework"
- $aab = Get-ChildItem -Path $binRoot -Recurse -Filter "*.aab" |
- Sort-Object LastWriteTime -Descending |
- Select-Object -First 1
- if (-not $aab) {
- throw "No .aab found under $binRoot"
- }
- Write-Host "`nSUCCESS"
- Write-Host "AAB: $($aab.FullName)"
- Write-Host "Size: $([Math]::Round($aab.Length / 1MB, 2)) MB"
|