[CmdletBinding()] param( [string]$AdbPath = (Join-Path $env:ANDROID_HOME "platform-tools\adb.exe") ) Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $repositoryRoot = Split-Path -Parent $PSScriptRoot $gradleWrapper = Join-Path $repositoryRoot "gradlew.bat" $mainApk = Join-Path $repositoryRoot "app\build\outputs\apk\debug\app-debug.apk" $testApk = Join-Path $repositoryRoot "app\build\outputs\apk\androidTest\debug\app-debug-androidTest.apk" $instrumentation = "net.opcapp.flash.test/androidx.test.runner.AndroidJUnitRunner" if (-not (Test-Path -LiteralPath $AdbPath -PathType Leaf)) { throw "ADB executable not found: $AdbPath" } & $gradleWrapper :app:assembleDebug :app:assembleDebugAndroidTest --no-daemon --console=plain if ($LASTEXITCODE -ne 0) { throw "Android app or test APK build failed." } $deviceState = (& $AdbPath get-state 2>$null) if ($LASTEXITCODE -ne 0 -or $deviceState.Trim() -ne "device") { throw "No authorized Android device is available through: $AdbPath" } & $AdbPath install -r $mainApk if ($LASTEXITCODE -ne 0) { throw "Main APK installation failed." } & $AdbPath install -r $testApk if ($LASTEXITCODE -ne 0) { throw "Test APK installation failed." } $instrumentationOutput = & $AdbPath shell am instrument -w -r $instrumentation 2>&1 $instrumentationExitCode = $LASTEXITCODE $instrumentationOutput | Write-Output $joinedOutput = $instrumentationOutput -join "`n" if ( $instrumentationExitCode -ne 0 -or $joinedOutput -match "FAILURES!!!" -or $joinedOutput -notmatch "OK \(\d+ tests?\)" ) { throw "Connected Android instrumentation tests failed." } Write-Output "Connected Android instrumentation tests passed."