Last active
October 2, 2021 20:37
-
-
Save FPSUsername/7845fba4dd229cf0b8d855311f9c3cc9 to your computer and use it in GitHub Desktop.
Install multiple APK files to a single or multiple devices that are connected over ADB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
package="APK Installer" | |
ver=1.0 | |
# Install APK file | |
install_apk() { | |
local filename=$1 | |
local device=$2 | |
echo "Installing '$filename' on device '$device'" | |
result=$(adb -s $device install $filename) | |
echo "$result" | |
if ! [[ "$result" =~ .*Success.* ]]; then | |
echo Failed to install "$filename" on device "$device" | |
return 1 | |
fi | |
return 0 | |
} | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
key="$1" | |
case $key in | |
-h|--help) | |
echo "$package - Install APK files over ADB" | |
echo "Version: $ver" | |
echo " " | |
echo "$package [options]" | |
echo " " | |
echo "options:" | |
echo "-h, --help Show brief help" | |
echo "-n, --no-delete Don't delete the apk files after installing." | |
echo " Note: not using this option will delete the file regardless of a successful install" | |
echo " " | |
echo "This program relies on ADB, ls and grep" | |
exit 0 | |
;; | |
-n|--no-delete) | |
nodelfile=1 | |
shift # past argument | |
;; | |
*) # unknown option | |
POSITIONAL+=("$1") # save it in an array for later | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" # restore positional parameters | |
# Check if ADB is installed | |
if ! command -v adb &> /dev/null; then | |
echo "<adb> could not be found" | |
echo "Please install the android SDK Platform-Tools" | |
echo "https://developer.android.com/studio/releases/platform-tools" | |
exit 1 | |
fi | |
# Search for APK files | |
declare files=($(ls | grep '\.apk')) | |
set -- "${files[@]}" | |
if [[ "${#files[@]}" == 0 ]]; then | |
echo "No APK files to install found" | |
exit 1 | |
fi | |
# Check for devices | |
declare devices=($(adb devices -l | awk '{print $1, substr($5,7,length($5))}' | tail -n +2)) | |
declare devices_selected | |
if [[ "${#devices[@]}" == 0 ]]; then | |
echo "Please attach a device with ADB access" | |
exit 1 | |
elif [[ "${#devices[@]}" -gt 3 ]]; then | |
echo "Available devices:" | |
for (( i=0; i<${#devices[@]}; i=i+2)); do | |
echo "${devices[i]} ${devices[i+1]}" | |
done | |
while read -p 'Select device(s): '; do | |
case $REPLY in | |
"q"|"quit") | |
echo "Exiting program" | |
exit 0 | |
;; | |
"a"|"all") | |
for index in "${!devices[@]}"; do | |
if [[ $(( index % 2 )) -eq 0 ]]; then | |
devices_selected+=(${devices[$index]}) | |
fi | |
done | |
break | |
;; | |
*) | |
if [[ -n $REPLY ]]; then | |
# Iterate over $REPLY | |
for device in ${REPLY[@]}; do | |
curr_length=${#devices_selected[@]} | |
# Iterate over the devices array by index | |
for index in "${!devices[@]}"; do | |
# Check if the device matches the index | |
if [[ "${devices[${index}]}" =~ (^|[^[:alpha:]])$device([^[:alpha:]]|$) ]]; then | |
# Check if the index of the device from $REPLY is uneven to select the proper ID | |
if [[ $(( index % 2 )) -ne 0 ]]; then | |
index=$((index -1)) | |
fi | |
sel_device="${devices[$index]}" # Select the device | |
# Check if device is already present | |
if ! [[ "${devices_selected[*]}" =~ (^|[^[:alpha:]])$sel_device([^[:alpha:]]|$) ]]; then | |
devices_selected+=("$sel_device") | |
else | |
continue 2 # Device already present | |
fi | |
fi | |
done | |
new_length=${#devices_selected[@]} | |
# Check if the device actually exists | |
if [[ "$curr_length" -eq "$new_length" ]]; then | |
echo "Device $device does not exist" | |
fi | |
done | |
break | |
fi | |
echo "No device selected. Select a device ('a' or 'all' for all devices) or send 'q' or 'quit' to exit the program." | |
;; | |
esac | |
done | |
else # default to the first device | |
devices_selected+=(${devices[0]}) | |
fi | |
echo "Selected device(s):" "${devices_selected[@]}" | |
# Install APK files | |
for filename in "${files[@]}"; do | |
for device in "${devices_selected[@]}"; do | |
install_apk "$filename" "$device" & | |
done | |
wait | |
# Delete APK file | |
if [[ -z "$nodelfile" ]]; then | |
rm "$filename" | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment