[CmdletBinding()] param( [string]$Serial ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot $sdkRoot = if ($env:ANDROID_HOME) { $env:ANDROID_HOME } else { 'C:\Users\shenw_o246oq4\AppData\Local\Android\Sdk' } $adb = Join-Path $sdkRoot 'platform-tools\adb.exe' $apk = Join-Path $root 'build\app\outputs\apk\debug\app-debug.apk' if (-not (Test-Path $adb)) { throw "adb not found: $adb" } if (-not (Test-Path $apk)) { throw "APK not found: $apk. Run 打包.cmd first." } $devices = @(& $adb devices | Select-Object -Skip 1 | ForEach-Object { if ($_ -match '^([^\s]+)\s+device$') { $Matches[1] } }) if ($Serial) { if ($Serial -notin $devices) { throw "Requested device is not connected and authorized: $Serial" } $selected = $Serial } elseif ($devices.Count -eq 1) { $selected = $devices[0] } elseif ($devices.Count -eq 0) { throw 'No authorized Android device found. Connect the target phone and enable USB debugging.' } else { throw "More than one device is connected. Re-run with -Serial; choices: $($devices -join ', ')" } & $adb -s $selected install -r $apk if ($LASTEXITCODE -ne 0) { throw 'M1 APK installation failed.' } & $adb -s $selected shell am force-stop org.neslite.app.debug & $adb -s $selected shell am start -W -n org.neslite.app.debug/org.neslite.app.MainActivity if ($LASTEXITCODE -ne 0) { throw 'M1 diagnostic activity did not start.' } $appPid = (& $adb -s $selected shell pidof org.neslite.app.debug).Trim() if (-not $appPid) { throw 'M1 process is not running after launch.' } Write-Output "M1 diagnostic app is running on $selected (pid $appPid)." Write-Output 'Complete the on-screen X5 Lite flow, then use “导出诊断 JSON”.'