apkpolling.ps1
#此脚本可以单独执行 #将脚本和apk包放在同一个文件夹,请自行解压任意apk到当前的脚本目录 # 以下是执行命令举例,打开powershell,切换到当前脚本目录 # 无法执行的话或执行报错,在powershell中执行 set-ExecutionPolicy RemoteSigned 选择y #.\polling-all-apk.ps1 -deviceid 串号 是轮巡当前文件夹下所有apk #.\polling-all-apk.ps1 -deviceid 串号 -apk_list ("androbench-5.0.1.apk", "HD Video Player.apk") 是轮巡当前文件夹下指定的apk #.\polling-all-apk.ps1 -polling 0 -deviceid 串号 是停止当前文件夹下所有apk #.\polling-all-apk.ps1 -polling 0 -deviceid 串号 -apk_list ("androbench-5.0.1.apk", "HD Video Player.apk") 是停止当前文件夹下指定的apk # 打开powershell !!! # 打开powershell !!! # 打开powershell !!! # 无法执行的话或执行报错,在powershell中执行 set-ExecutionPolicy RemoteSigned 选择y !!! # 无法执行的话或执行报错,在powershell中执行 set-ExecutionPolicy RemoteSigned 选择y !!! # 无法执行的话或执行报错,在powershell中执行 set-ExecutionPolicy RemoteSigned 选择y !!! Param( [int]$polling = 1, [int]$pollingAndrobench = 1, [string]$apkdiristrue = "0", [string[]]$apk_list = $null, [string]$deviceid = "0123456789ABCDEF" ) $scriptName = "pollingapk" $tempFile = "$scriptName-$deviceid" #获取当前脚本所在路径 $CurrentyDir = Split-Path -Parent $MyInvocation.MyCommand.Definition Write-Output $CurrentyDir $apkdir1 = (Get-ChildItem $CurrentyDir| Where-Object{$_.psiscontainer -eq $true}) $apkdir = "$CurrentyDir\$apkdir1" $pack_num=0 function deleteTmpFile { $FILE_PATH="$CurrentyDir\$tempFile-*.txt" $TRUE_FALSE=(Test-Path $FILE_PATH) if($TRUE_FALSE -eq "True") { remove-Item -Recurse -Force $FILE_PATH } } #获取包名&activity名 function getPackAndActivityName([Object[]]$pathApk) { foreach($i in $pathApk){aapt d badging $LOCALAPKDir\$i|Select-String '^package: name'| out-string -width 4096 >> $CurrentyDir\$tempFile-package1.txt} Get-Content $CurrentyDir\$tempFile-package1.txt|Where-Object{-not [String]::IsNullOrWhiteSpace($_)} | set-content $CurrentyDir\$tempFile-package_old.txt Get-Content $CurrentyDir\$tempFile-package_old.txt |ForEach-Object{$_.split('=')[1]}|ForEach-Object{$_.split(' ')[0]} >> $CurrentyDir\$tempFile-package.txt #写入当前所有apk的activity名 foreach($i in $pathApk){ if(aapt d badging $LOCALAPKDir\$i|Select-String '^launchable-activity: name'){ aapt d badging $LOCALAPKDir\$i|Select-String '^launchable-activity: name'| out-string -width 4096 >> $CurrentyDir\$tempFile-activity1.txt }else{ Write-Output "no-launchable-activity: name" >> $CurrentyDir\$tempFile-activity1.txt } } Get-Content $CurrentyDir\$tempFile-activity1.txt|Where-Object{-not [String]::IsNullOrWhiteSpace($_)} | set-content $CurrentyDir\$tempFile-activity_old.txt Get-Content $CurrentyDir\$tempFile-activity_old.txt | ForEach-Object {if ($_.StartsWith("launchable-activity")){ ForEach-Object{$_.split('=')[1]}|ForEach-Object{$_.split(' ')[0]} >> $CurrentyDir\$tempFile-activity.txt }else{ Write-Output $_ >> $CurrentyDir\$tempFile-activity.txt } } } #获取当前top 是否已经打开 function confirmTopActiity([string]$topAct) { $count=0 while ($count -lt 5) { adb -s $deviceid shell "dumpsys activity activities|head -20|grep $topAct" $Flag = Write-Output $? if( $Flag -ne "True" ){ Write-Output "not TOP" Start-Sleep 1 } else{ break } $count += 1 } } function CalcStartApkTime([string]$packActivity) { $script:singleApkSpend=((adb -s $deviceid shell am start -W $packActivity|Select-String WaitTime).ToString().Split(":")[1]) } function Invoke-TimeOutCommand() { $doit = "$packagename/$line" $job = Start-Job -ScriptBlock { param($num) $numBlock = [Scriptblock]::Create($num) $numBlock.Invoke() } -ArgumentList (CalcStartApkTime $doit)| Wait-Job -Timeout 15 if($job.State -ne 'Completed') { Write-Warning 'timeout' Start-Sleep 1 adb -s $deviceid shell pm clear $packagename adb -s $deviceid shell am force-stop $packagename $job | Stop-Job | Remove-Job return $null } else { return $job | Receive-Job } } function startApk { #轮巡安装的app #获取包名/activity名 $allAPKstartSpend=0 $lines = Get-Content $CurrentyDir\$tempFile-activity.txt $file_rows=(get-content $CurrentyDir\$tempFile-package.txt|Measure-Object).Count if( $file_rows -eq 1 ){ foreach ($line in $lines){$packagename=(Get-Content $CurrentyDir\$tempFile-package.txt -TotalCount 500) $packagename=$packagename -replace "'", "" Write-Output "$deviceid $line start..." Invoke-TimeOutCommand Write-Output "WaitTime: $singleApkSpend" $allAPKstartSpend = $allAPKstartSpend + $singleApkSpend Start-Sleep 5 } }else{ foreach ($line in $lines){$packagename=(Get-Content $CurrentyDir\$tempFile-package.txt -TotalCount 500)[$pack_num] $pack_num = $pack_num+1;$packagename=$packagename -replace "'", "" Write-Output "$deviceid $line start..." Invoke-TimeOutCommand Write-Output "WaitTime: $singleApkSpend" $allAPKstartSpend = $allAPKstartSpend + $singleApkSpend Start-Sleep 5 } } Write-Host -ForegroundColor Red ($deviceid + ' all apk start spend: ' + ($allAPKstartSpend/1000) + 's------------------') } function stopApk { $lines_pack = Get-Content $CurrentyDir\$tempFile-package.txt foreach ($line in $lines_pack){Write-Output "$line stop app";adb -s $deviceid shell pm clear $line;adb -s $deviceid shell am force-stop $line} } #testflow $start = Get-Date #删除中间文件 deleteTmpFile #获取 包名&activity名 if( $null -eq $apk_list ){ if( $apkdiristrue -eq 0){ $pathApk = Get-ChildItem $CurrentyDir\*.apk -name $LOCALAPKDir = $CurrentyDir getPackAndActivityName $pathApk }else{ $pathApk = Get-ChildItem $apkdir\*.apk -name $LOCALAPKDir = $apkdir getPackAndActivityName $pathApk } }else{ $pathApk = $apk_list getPackAndActivityName $pathApk } #轮巡或者停止apk if( $pollingAndrobench -eq 1 ){ if( $polling -eq 1 ){ startApk #删除中间文件 deleteTmpFile }else{ stopApk #删除中间文件 deleteTmpFile } }else{ Write-Output "skip by python" } $end = Get-Date Write-Host -ForegroundColor Red ($deviceid + ' Total Runtime: ' + ($end - $start).TotalSeconds)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】