批量安装、卸载apk脚本
需要自己建立.bat文件
支持相对路径,把需要进行安装测试的apk放在bat文件同一个文件夹下即可
安装脚本install_apks.bat
@echo off
setlocal enabledelayedexpansion:: 设置 APK 所在文件夹(当前脚本所在目录)
set "APK_DIR=%~dp0":: 检查 adb 是否可用
adb version >nul 2>&1
if %errorlevel% neq 0 (echo Error: adb not found. Please ensure Android SDK is installed and platform-tools is in PATH.pauseexit /b 1
):: 检查设备连接
adb devices | findstr /C:"device" >nul
if %errorlevel% neq 0 (echo Error: No connected Android devices found. Please connect a device and enable USB debugging.pauseexit /b 1
)echo Found Android device(s) connected.
echo.:: 统计APK文件数量
set count=0
for %%f in ("%APK_DIR%*.apk") do set /a count+=1if %count% equ 0 (echo Error: No APK files found in directory: %APK_DIR%pauseexit /b 1
)echo Found %count% APK file(s) to install.
echo.:: 遍历当前目录中的所有 .apk 文件并安装
set success_count=0
set failed_count=0for %%f in ("%APK_DIR%*.apk") do (echo.echo Installing: %%~nxfecho ----------------------------------------adb install -r -d "%%f"if !errorlevel! equ 0 (echo [SUCCESS] %%~nxf installed successfullyset /a success_count+=1) else (echo [FAILED] Failed to install %%~nxfset /a failed_count+=1)echo ----------------------------------------
):: 显示安装结果统计
echo.
echo ===== Installation Summary =====
echo Total APKs processed: %count%
echo Successfully installed: %success_count%
echo Failed installations: %failed_count%
echo ==============================
echo.if %failed_count% equ 0 (echo All APKs have been installed successfully!
) else (echo Some APKs failed to install. Please check the output above.
)echo.
echo Press any key to exit this window...
pause >nul
卸载脚本uninstall_apks.bat
由于技术有限,aapt2.exe的路径加入PATH后一直不生效,所以用的是绝对路径实现的效果
set "AAPT2_PATH=<你build_tools的路径>\aapt2.exe"——这个部分是要进行修改的
@echo off
setlocal enabledelayedexpansionecho Scanning and uninstalling APKs in current directory...
echo.:: Check if adb is available
adb version >nul 2>&1
if %errorlevel% neq 0 (echo Error: adb not found. Please ensure Android SDK is installed and platform-tools is in PATH.pauseexit /b 1
):: Check device connection
adb devices | findstr "device" >nul
if %errorlevel% neq 0 (echo Error: No connected Android devices found. Please connect a device and enable USB debugging.pauseexit /b 1
)echo Found connected Android device(s).
echo.:: Set aapt2.exe path
set "AAPT2_PATH=<你build_tools的路径>\aapt2.exe":: Check if aapt2 is available
if not exist "%AAPT2_PATH%" (echo Error: aapt2.exe not found at: %AAPT2_PATH%pauseexit /b 1
)echo Scanning APK files...
echo.:: Count and uninstall APKs
set count=0
set success_count=0for %%f in (*.apk) do (set /a count+=1echo Processing: %%f:: Get package namefor /f "tokens=*" %%p in ('call "%AAPT2_PATH%" dump packagename "%%f"') do (set "package_name=%%p")if defined package_name (echo Package name: !package_name!:: Check if app is installedadb shell pm list packages | findstr "!package_name!" >nulif !errorlevel! equ 0 (echo Uninstalling: !package_name!adb uninstall !package_name!if !errorlevel! equ 0 (echo [SUCCESS] !package_name! uninstalledset /a success_count+=1) else (echo [FAILED] Failed to uninstall !package_name!)) else (echo [SKIP] !package_name! not installed)) else (echo [FAILED] Could not get package name for: %%f)echo.
)echo ===== Uninstallation Complete =====
echo Total APKs processed: %count%
echo Successfully uninstalled: %success_count%
echo ==================================
echo.pause