Skip to content

Instantly share code, notes, and snippets.

@hjc4869
Created October 20, 2021 17:11
Show Gist options
  • Select an option

  • Save hjc4869/d6246309bbcfd0deb0546525df81adcd to your computer and use it in GitHub Desktop.

Select an option

Save hjc4869/d6246309bbcfd0deb0546525df81adcd to your computer and use it in GitHub Desktop.
APK installer for Windows Subsystem for Android
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