Files
workphone-sdk/sdk/scripts/android_free_space.sh

54 lines
2.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# 通过 ADB 安全释放手机存储:系统缓存裁剪 + 常见临时/缩略图(不卸载应用、不清登录态)
# 用法:手机 USB 调试已连接后执行
# bash sdk/scripts/android_free_space.sh
# 多设备ANDROID_SERIAL=序列号 bash sdk/scripts/android_free_space.sh
# 更激进(仍不 pm clear 应用):加参数 --more
set -euo pipefail
ADB=(adb)
if [[ -n "${ANDROID_SERIAL:-}" ]]; then
ADB=(adb -s "$ANDROID_SERIAL")
fi
if ! "${ADB[@]}" get-state 2>/dev/null | grep -q device; then
echo "❌ 未检测到已授权的 ADB 设备。请USB 连接 → 开启 USB 调试 → 本机执行 adb devices 出现 device。"
exit 1
fi
echo "=== 设备 ==="
"${ADB[@]}" shell getprop ro.product.model 2>/dev/null | tr -d '\r'
"${ADB[@]}" devices -l | grep -v "^List"
echo ""
echo "=== 清理前 /data或整机存储==="
"${ADB[@]}" shell df -h /data 2>/dev/null | tr -d '\r' || "${ADB[@]}" shell df -h | head -20 | tr -d '\r'
# 系统级裁剪各应用 cacheAndroid 7+,无需 root
echo ""
echo "=== pm trim-caches按目标剩余空间触发缓存回收==="
# 部分机型参数为「希望至少腾出/保持」的数值,单位因版本而异;依次尝试常见写法
"${ADB[@]}" shell pm trim-caches 999999M 2>/dev/null | tr -d '\r' || true
"${ADB[@]}" shell "pm trim-caches 8589934592" 2>/dev/null | tr -d '\r' || true
echo ""
echo "=== 临时目录 ==="
"${ADB[@]}" shell "rm -rf /data/local/tmp/*" 2>/dev/null || true
echo "=== DCIM 缩略图(可再生成)==="
"${ADB[@]}" shell "rm -rf /sdcard/DCIM/.thumbnails/*" 2>/dev/null || true
"${ADB[@]}" shell "rm -rf /storage/emulated/0/DCIM/.thumbnails/*" 2>/dev/null || true
if [[ "${1:-}" == "--more" ]]; then
echo "=== --more常见缓存目录不含微信/QQ 聊天记录)==="
"${ADB[@]}" shell "rm -rf /sdcard/Android/data/*/cache/*" 2>/dev/null || true
"${ADB[@]}" shell "rm -rf /storage/emulated/0/Android/data/*/cache/*" 2>/dev/null || true
"${ADB[@]}" shell "rm -rf /sdcard/.Trash/*" 2>/dev/null || true
"${ADB[@]}" shell "rm -rf /sdcard/Download/.tmp/*" 2>/dev/null || true
fi
echo ""
echo "=== 清理后 /data ==="
"${ADB[@]}" shell df -h /data 2>/dev/null | tr -d '\r' || true
echo "✅ 完成。若空间仍不足,可在手机「设置 → 存储」里清理各 App或卸载不常用应用。"