hdc命令不如adb好记,所以进行了一些常用命令的封装
支持单设备/多设备,安装/卸载应用、查询当前App的activity,清理App缓存、截图保存到本地
#!/bin/bash
# 检查 hdc 是否是有效命令
check_hdc_command() {
if ! command -v hdc &>/dev/null; then
echo "错误:hdc 命令未找到,请确保已正确安装 hdc 工具并将其添加到 PATH 环境变量中。"
exit 1
fi
}
# 列出连接的设备
list_devices() {
devices=$(hdc list targets | grep -v -F "[Empty]" | grep -v "^$" | cut -d' ' -f1) # 获取设备列表
# 如果设备列表为空,返回非零状态码
if [ -z "$devices" ]; then
return 1 # 返回非零状态表示错误
fi
echo "$devices" # 返回设备列表
return 0 # 返回零状态表示成功
}
# 获取设备信息
get_device_info() {
device_id=$1 # 正确解析传入的参数
# 获取系统版本信息
system_name=$(hdc -t "$device_id" shell param get const.product.software.version 2>/dev/null)
if [ -z "$system_name" ]; then
echo "无法从设备 $device_id 获取系统信息,请检查设备连接状态或授权。"
return
fi
system_name=${system_name:-未知} # 如果为空,则设为默认值
udid=$(hdc -t "$device_id" shell bm get --udid | sed 's/udid of current device is ://')
echo -e "设备ID: $device_id\t设备系统版本: $system_name\tUDID: $udid"
echo "-----------------------------"
}
# 获取应用列表
get_device_app_list() {
device_id=$1 # 正确解析传入的参数
app_list=$(hdc -t "$device_id" shell bm dump -a 2>/dev/null | grep -v "^ID:")
echo -e "设备应用列表:\n$app_list"
}
# 获取当前应用Ability信息
get_app_ability() {
device_id=$1 # 正确解析传入的参数
ability_info=$(hdc -t "$device_id" shell aa dump -l)
echo -e "当前应用Ability信息\n$ability_info"
}
# 截图
get_screenshot() {
device_id=$1
data_img_path=$(hdc -t "$device_id" shell snapshot_display | sed -n 's/.*write to \(.*\) as jpeg.*/\1/p')
img_name=$(basename "$data_img_path")
local_img_path="$HOME/Downloads/$img_name"
hdc -t "$device_id" file recv $data_img_path "$local_img_path" > /dev/null
echo -e "从设备: $data_img_path,保存到: $local_img_path"
}
# 清理应用缓存
clean_app() {
device_id=$1 # 正确解析传入的参数
bundle_name=$2
cl_res=$(hdc -t "$device_id" shell bm clean -n "$bundle_name" -c 2>/dev/null)
if [[ "$cl_res" == *successfully* ]]; then
msg="成功"
else
msg="$cl_res"
fi
echo -e "清理应用缓存: $msg"
}
# 显示当前设备信息(支持单个设备或多设备选择)
select_device() {
devices=$(list_devices) # 获取设备列表
# 检查是否获取设备列表成功
if [ $? -ne 0 ]; then
echo "未检测到任何设备,请检查设备连接或授权。"
exit 1 # 脚本退出
fi
device_count=$(echo "$devices" | wc -w)
if [ "$device_count" -eq 1 ]; then
# 如果只有一个设备,直接选择
device_id=$(echo "$devices" | head -n 1)
echo "当前连接的设备信息:"
get_device_info "$device_id"
elif [ "$device_count" -gt 1 ]; then
# 如果有多个设备,提示用户选择
echo "检测到多个设备,请选择一个设备:"
select device in $devices; do
if [ -n "$device" ]; then
device_id="$device"
echo "当前选择的设备信息:"
get_device_info "$device_id"
break
else
echo "无效选择,请重试。"
fi
done
fi
}
# 安装应用
install_app() {
device_id=$1 # 获取设备 ID
apk_path=$2 # 获取 APK 路径
if [ ! -f "$apk_path" ]; then
echo "APK 文件 $apk_path 不存在,请检查路径是否正确。"
return
fi
echo "正在向设备 $device_id 安装应用..."
install_result=$(hdc -t "$device_id" install "$apk_path" 2>&1)
if echo "$install_result" | grep -q "Success"; then
echo "应用安装成功!"
else
echo "应用安装失败,请检查设备状态或 APK 文件。"
echo "错误信息:$install_result"
fi
echo "-----------------------------"
}
# 卸载应用
uninstall_app() {
device_id=$1 # 获取设备 ID
package_name=$2 # 获取包名
echo "正在向设备 $device_id 卸载应用包:$package_name..."
uninstall_result=$(hdc -t "$device_id" uninstall "$package_name" 2>&1)
if echo "$uninstall_result" | grep -q "Success"; then
echo "应用卸载成功!"
else
echo "应用卸载失败,请检查设备状态或包名。"
echo "错误信息:$uninstall_result"
fi
echo "-----------------------------"
}
# 主菜单
main_menu() {
while true; do
echo ""
echo "======= HDC 工具脚本 ======="
echo "1) 显示设备信息"
echo "2) 安装应用"
echo "3) 卸载应用"
echo "4) 获取设备应用列表"
echo "5) 清理应用缓存"
echo "6) 获取当前应用Ability信息"
echo "7) 获取截图"
echo "0) 退出脚本"
echo "============================"
read -rp "请选择操作: " choice
case $choice in
1)
get_device_info "$device_id" # 显示当前设备信息
;;
2)
# 输入 APK 路径
read -rp "请输入 APK 文件路径: " apk_path
install_app "$device_id" "$apk_path"
;;
3)
# 输入包名
read -rp "请输入要卸载的应用包名: " package_name
uninstall_app "$device_id" "$package_name"
;;
4)
get_device_app_list "$device_id" # 获取设备应用列表
;;
5)
# 输入 APK bundleName
read -rp "请输入要清理缓存的bundleName: " bundle_name
clean_app "$device_id" "$bundle_name" # 清理应用缓存
;;
6)
get_app_ability "$device_id" # 获取当前应用Ability信息
;;
7)
get_screenshot "$device_id" # 获取截图
;;
0)
echo "退出脚本,感谢使用!"
exit 0
;;
*)
echo "无效选项,请输入 1-4 的数字。"
;;
esac
done
}
# 主脚本逻辑
echo "======= HDC 工具脚本 ======="
check_hdc_command
select_device # 检测设备连接并选择设备
main_menu # 启动功能菜单
效果如下