Created
October 20, 2021 17:11
-
-
Save hjc4869/d6246309bbcfd0deb0546525df81adcd to your computer and use it in GitHub Desktop.
APK installer for Windows Subsystem for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param([string]$ApkFile) | |
| # Acquire adb.exe/AdbWinApi.dll from Android SDK and copy them to the same directory | |
| # Run with admin cmd to associate the script with APK files: | |
| # assoc .apk=apkfile | |
| # ftype apkfile=powershell.exe -File <path\to>\apkinst.ps1 "%1" | |
| $ErrorActionPreference="Stop" | |
| $AdbPath="$PSScriptRoot\adb.exe" | |
| if (-not(Test-Path -Path $AdbPath)) { | |
| Write-Host "Adb not found" | |
| pause | |
| } | |
| $mutex=[System.Threading.Mutex]::new($false, "Global\WsaAppInstaller") | |
| try { | |
| if (-not(Test-Path -Path $ApkFile)) { | |
| throw "Cannot find file $ApkFile" | |
| } | |
| $mutex.WaitOne() | Out-Null | |
| $process = Get-Process WsaClient -ErrorAction SilentlyContinue | |
| if (-not($process)) { | |
| throw "Windows Subsystem for Android™ is not running. Please start the subsystem before installing any apps." | |
| } | |
| $port = (Get-NetTCPConnection -OwningProcess $process.Id | ? { $_.State -eq "Listen" }| Select-Object -First 1).LocalPort | |
| & $AdbPath kill-server | |
| if ($LastExitCode -ne 0) { | |
| throw "Failed to kill adb server" | |
| } | |
| & $AdbPath connect "127.0.0.1:$port" | |
| if ($LastExitCode -ne 0) { | |
| throw "Failed to connect to 127.0.0.1:$port" | |
| } | |
| & $AdbPath install $ApkFile | |
| if ($LastExitCode -ne 0) { | |
| throw "Failed to install APK" | |
| } | |
| } catch { | |
| echo "Failed to install app: $_" | |
| pause | |
| } finally { | |
| & $AdbPath kill-server | |
| $mutex.ReleaseMutex() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment