#aapt.exe adb.exe 设置为环境变量
#将脚本和apk包放在同一个文件夹
#.\autoinstallapk.ps1 -install 1 -deviceid 串号 是安装当前文件夹下所有apk
#管理员打开powershell set-ExecutionPolicy RemoteSigned 选择A
Param(
[int]$install = 1,
[string[]]$apk_list = $null,
[string]$apkdiristrue = "0",
[string]$deviceid = "0123456789ABCDEF"
)
$scriptName = "autoinstallapk"
$tempFile = "$scriptName-$deviceid"
$PSScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
$CurrentyDir = $PSScriptRoot
$apkdir1 = (Get-ChildItem $CurrentyDir| ?{$_.psiscontainer -eq $true})
$apkdir = "$CurrentyDir\$apkdir1"
echo "Currentdir: "$CurrentyDir
echo ""
echo "1 install, 0 uninstall "$install
$global:TotalSpend = 0
$global:TotalSpendList = @()
function deleteTmpFile
{
$FILE_PATH="$CurrentyDir\$tempFile-package*.txt"
$TRUE_FALSE=(Test-Path $FILE_PATH)
if($TRUE_FALSE -eq "True")
{
remove-Item -Recurse -Force $FILE_PATH
}
}
function getVersionRelease
{
$cmdstr="getprop |grep -w ro.build.version.release | awk -F[ '{print `$3`}'|awk -F] '{print `$1`}'"
$adbCommand=adb -s $deviceid shell $cmdstr
$VersionR=Invoke-Expression $adbCommand
return $VersionR
}
function installApk([Object[]]$pathApk, [REF]$ans, [REF]$ansl)
{
$VersionR = getVersionRelease
if (Test-Path $LOCALAPKDir\oldAndrobench\'androbench-5.0.1.apk') {
if ( $VersionR -eq "14" ){
# adb -s $deviceid shell pm uninstall 'com.andromeda.androbench2';
adb -s $deviceid install '--bypass-low-target-sdk-block' $LOCALAPKDir\oldAndrobench\'androbench-5.0.1.apk';
}else{
# adb -s $deviceid shell pm uninstall 'com.andromeda.androbench2';
adb -s $deviceid install $LOCALAPKDir\oldAndrobench\'androbench-5.0.1.apk';
}
}
$num=1
foreach($i in $pathApk){
$start1 = Get-Date;
adb -s $deviceid install -r -g $LOCALAPKDir\$i;
$flag = $?;
$end1 = Get-Date;
$singleSpend = ($end1 - $start1);
if (!($flag)){
$ansl.Value += 0;
Write-Host ($deviceid + ' apkinstall '+ $i + ' Runtime: ' + 0);
$ans.Value = $ans.Value + 0;
}else{
$ansl.Value += $singleSpend.TotalSeconds;
Write-Host ($deviceid + ' apkinstall '+ $i + ' Runtime: ' + $singleSpend.TotalSeconds);
$ans.Value = $ans.Value + $singleSpend.TotalSeconds;
};
$num++}
}
function uninstallApk([Object[]]$pathApk)
{
foreach ($i in $pathApk){aapt d badging $LOCALAPKDir\$i|Select-String '^package: name'| out-string -width 4096 >> $CurrentyDir\$tempFile-package1.txt }
gc $CurrentyDir\$tempFile-package1.txt|?{-not [String]::IsNullOrWhiteSpace($_)} | set-content $CurrentyDir\$tempFile-package2.txt
gc $CurrentyDir\$tempFile-package2.txt |%{$_.split('=')[1]}|%{$_.split(' ')[0]} >> $CurrentyDir\$tempFile-package3.txt
#2遍历 卸载apk
$lines = gc $CurrentyDir\$tempFile-package3.txt
foreach ($line in $lines){echo "$line uninstall...";adb -s $deviceid shell pm uninstall $line}
}
#删除中间文件
deleteTmpFile
if ( $apk_list -ne $null ){
$pathApk = $apk_list
}else{
if( $apkdiristrue -eq 0){
$pathApk = dir $CurrentyDir\*.apk -name
$LOCALAPKDir = $CurrentyDir
}else{
$pathApk = dir $apkdir\*.apk -name
$LOCALAPKDir = $apkdir
}
}
if( $install -eq 1 ){
installApk $pathApk ([REF]$global:TotalSpend) ([REF]$global:TotalSpendList)
}else{
uninstallApk $pathApk
}
#删除中间文件
deleteTmpFile
#统计个数
#$packnums=((adb -s $deviceid shell pm list packages| select-string package).count)
Write-Host -ForegroundColor Red ('**********************************')
echo $TotalSpendList
Write-Host -ForegroundColor Red ($deviceid + ' apkinstall '+ $install + ' Total Runtime: ' + $TotalSpend)
Write-Host -ForegroundColor Red ('**********************************')