Files
Cognitive_Agent/N7Cockpit/scripts/install-migrated-apk.ps1

57 lines
1.8 KiB
PowerShell

[CmdletBinding()]
param(
[string]$AdbPath = '',
[string]$ApkPath = '',
[string]$Serial = '',
[ValidateSet('debug', 'release')]
[string]$Variant = 'debug'
)
$ErrorActionPreference = 'Stop'
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
if (-not $AdbPath) {
$sdkRoots = @(
$env:ANDROID_SDK_ROOT,
$env:ANDROID_HOME,
(Join-Path $env:LOCALAPPDATA 'Android\Sdk')
) | Where-Object { $_ }
foreach ($sdkRoot in $sdkRoots) {
$candidate = Join-Path $sdkRoot 'platform-tools\adb.exe'
if (Test-Path -LiteralPath $candidate) { $AdbPath = $candidate; break }
}
}
if (-not $AdbPath -or -not (Test-Path -LiteralPath $AdbPath)) {
throw 'adb.exe was not found. Set -AdbPath or ANDROID_SDK_ROOT.'
}
if (-not $ApkPath) {
$ApkPath = Join-Path $projectRoot "dist\N7Cockpit-remote-$Variant.apk"
}
if (-not (Test-Path -LiteralPath $ApkPath)) {
throw "APK does not exist: $ApkPath"
}
$adbArgs = @()
if ($Serial) { $adbArgs = @('-s', $Serial) }
$online = @(& $AdbPath devices) | Where-Object { $_ -match "\tdevice$" }
if (-not $Serial -and $online.Count -ne 1) {
throw "Exactly one online Android device is required; found $($online.Count)."
}
& $AdbPath @adbArgs install -r $ApkPath
if ($LASTEXITCODE -ne 0) { throw 'APK installation failed.' }
$package = if ($Variant -eq 'debug') {
'com.seeker.n7cockpit.remote'
} else {
'com.seeker.n7cockpit'
}
$component = "$package/com.seeker.n7cockpit.MainActivity"
& $AdbPath @adbArgs shell am force-stop $package | Out-Null
$launch = & $AdbPath @adbArgs shell am start -W -n $component 2>&1
if ($LASTEXITCODE -ne 0 -or ($launch -join "`n") -notmatch 'Status:\s+ok') {
throw "APK installed but launch failed:`n$($launch -join "`n")"
}
Write-Host 'INSTALL PASSED'
Write-Host "Package: $package"
Write-Host "Component: $component"