扫描Android设备,全量安装应用(多线程) Shell脚本

使用方法

sh device.sh [***.apk]

整体脚本

function device_install(){
    echo $1"开始处理"
    #1 判断是否已安装
    package_count=$(adb -s $1 shell pm list packages | grep $2 | wc -l)
    if [ ${package_count} -gt 0 ]; then
        echo $1"app已存在,卸载中"
        adb -s $1 uninstall $2
        echo $1"卸载完毕"
    fi
    # -r 强制覆盖安装  -d 解决低版本覆盖高版本的问题
    adb -s $1 install -r -d $3
    
}

if [ ! -n "$1" ] ;then
    echo '缺少apk文件'
    exit 0
else
    apk_url=$1;
fi

package=$(aapt dump badging ${apk_url} | grep 'package: name=' | awk '{print $2}' | awk -F"\'" '{print $2}')

devices=$(adb devices | awk 'NR>1{print $1}')
count=$(adb devices | awk 'NR>1{if($0!="") print $1}' | wc -l)
if [ ${count} -lt 1 ]; then
   echo '设备列表为空'
   exit -1
fi

for device_id in `echo ${devices}`
do
    {
        device_install ${device_id} ${package} ${apk_url}
        if [ "$?" = "0" ]; then
           echo ${device_id}"安装成功" 
        else
           echo ${device_id}"安装出现异常,请单独处理"
        fi 
    } &
done
wait
echo '全部设备安装完毕'