前言
當(dāng)前PC連著多個(gè)安卓設(shè)備時(shí),當(dāng)超出了 adb 所支持的設(shè)備數(shù),步驟如下:
- adb devices 列出你當(dāng)前的設(shè)備列表,然后拷貝你要安裝的設(shè)備Device Id; 使用 adb -s deviceId
- install .... 來(lái)進(jìn)行 APK 安裝
error: more than one device/emulator
adb: error: failed to get feature set: more than one device/emulator
- waiting for device -
error: more than one device/emulator
效果圖
#adb 快捷命令
function adbConnect(){
echo "Your connect is 192.168.$1" # 這個(gè) $1 必須要參考底下命令的下達(dá)
adb connect 192.168.$1
}
function adbDisconnect(){
adb disconnect
}
function adbShell(){
adb shell
}
function devices(){
adb devices
}
# 獲取安卓設(shè)備數(shù)量
function getAdbDevicesCount(){
no_dev=2 #沒(méi)有設(shè)備的總行數(shù) 第一行: List of devices attached 第二行 空
line="`adb devices | wc -l`"
# echo "$line"
echo $(($line-$no_dev))
}
#獲取自定義格式設(shè)備名稱 參數(shù)1: adb devices 設(shè)備ID index
function getFmtDeviceName(){
if [ -n "$1" ]; then
line=$1
let "line++" #跳過(guò)1行
deviceId="`adb devices | sed -n "${line}p" | awk '{printf $1"\n"}' `" # name="`adb devices | sed -n "2p;${line}p" | awk '{printf NR ". " $1"\n"}' `" #簡(jiǎn)單列出設(shè)備ID
manufacturer="`adb -s $deviceId shell getprop ro.product.manufacturer`"
model="`adb -s $deviceId shell getprop ro.product.model`"
version="`adb -s $deviceId shell getprop ro.build.version.release`"
sdk="`adb -s $deviceId shell getprop ro.build.version.sdk`"
name="${manufacturer} ${model} Android ${version} API ${sdk} Serial: ${deviceId} "
# 去除某些設(shè)備后面攜帶回車(chē)符
name=`echo ${name} | tr -d '\r'`
echo ${name}
else
echo "requires an argument"
fi
}
# 列出所有設(shè)備
function listFmtDevices(){
count=`getAdbDevicesCount`
index=1
while(( $index<=count ))
do
name=`getFmtDeviceName ${index}`
echo "${index}. ${name} "
let "index++"
done
}
# 獲取設(shè)備ID 參數(shù)1: adb devices 設(shè)備ID index
function getFmtDeviceId(){
if [ -n "$1" ]; then
line=$1
let "line++" #跳過(guò)1行
deviceId="`adb devices | sed -n "${line}p" | awk '{printf $1"\n"}' `"
# 去除某些設(shè)備后面攜帶回車(chē)符
deviceId=`echo ${deviceId} | tr -d '\r'`
echo ${deviceId}
else
echo "requires an argument"
fi
}
# 安裝apk
function apk(){
if [ -n "$1" ]; then
count=`getAdbDevicesCount`
one_dev=1
if [ $count -eq $one_dev ]
then
# 單設(shè)備
name=`getFmtDeviceName 1`
echo "install apk to devices: ${name}"
adb install -r $1
elif [ $count -gt $one_dev ]
then
# 多設(shè)備
if [ -n "$2" ]; then
# 帶設(shè)備index
index=$2
deviceId=`getFmtDeviceId ${index}`
name=`getFmtDeviceName ${index}`
echo "install apk to devices: $name"
adb -s $deviceId install -r $1
else
# 帶apk文件路徑
echo "install apk to which devices?"
listFmtDevices
read -p "Enter: " index
apk $1 $index
fi
else
echo "no devices"
fi
else
echo "apk requires an apkPath argument"
fi
}
# 卸載apk
function uapk(){
if [ -n "$1" ]; then
count=`getAdbDevicesCount`
one_dev=1
if [ $count -eq $one_dev ]
then
# 單設(shè)備
name=`getFmtDeviceName 1`
echo "uninstall apk on $name"
adb uninstall $1
elif [ $count -gt $one_dev ]
then
# 多設(shè)備
if [ -n "$2" ]; then
# 帶設(shè)備index
index=$2
deviceId=`getFmtDeviceId ${index}`
name=`getFmtDeviceName ${index}`
echo "uninstall apk on devices: $name"
adb -s $deviceId uninstall $1
else
# 帶apk文件路徑
echo "uninstall apk on which devices?"
listFmtDevices
read -p "Enter: " index
uapk $1 $index
fi
else
echo "no devices"
fi
else
echo "uapk requires an pkg argument"
fi
}
# 進(jìn)入adb shell 環(huán)境
function as(){
count=`getAdbDevicesCount`
one_dev=1
if [ $count -eq $one_dev ]
then
# 單設(shè)備
name=`getFmtDeviceName 1`
echo "${name} Last login: `date`"
adb shell
elif [ $count -gt $one_dev ]
then
# 多設(shè)備
if [ -n "$1" ]; then
# 帶設(shè)備index
index=$1
deviceId=`getFmtDeviceId ${index}`
name=`getFmtDeviceName ${index}`
echo "${name} Last login: `date`"
adb -s $deviceId shell
else
# 不帶設(shè)備index
echo "enter shell which devices?"
listFmtDevices
read -p "Enter: " index
as $index
fi
else
echo "no devices"
fi
}
# 截圖
# adb exec-out screencap -p > screen.png
alias scap="adb exec-out screencap -p >"
function fcap(){
# savepath=$(cd `dirname $0`; pwd)
count=`getAdbDevicesCount`
one_dev=1
if [ $count -eq $one_dev ]
then
# 單設(shè)備
name=`getFmtDeviceName 1`
echo "${name} Last login: `date`"
while true;
do
DATE=`date +%Y%m%d%H%M%S`
scap screen_${DATE}.png
echo "save screen_${DATE}.png success!"
read -p 'Press any key to continue screencap ...'
done
elif [ $count -gt $one_dev ]
then
# 多設(shè)備
if [ -n "$1" ]; then
# 帶設(shè)備index
index=$1
deviceId=`getFmtDeviceId ${index}`
name=`getFmtDeviceName ${index}`
echo "${name} Last login: `date`"
while true;
do
DATE=`date +%Y%m%d%H%M%S`
adb -s $deviceId exec-out screencap -p > screen_${DATE}.png
echo "save screen_${DATE}.png success!"
read -p 'Press any key to continue screencap ...'
done
else
# 不帶設(shè)備index
echo "enter shell which devices?"
listFmtDevices
read -p "Enter: " index
fcap $index
fi
else
echo "no devices"
fi
}