Skip to content

Instantly share code, notes, and snippets.

@lopesivan
Created November 15, 2025 21:05
Show Gist options
  • Select an option

  • Save lopesivan/d03781bc035d0967d65efe07be60c408 to your computer and use it in GitHub Desktop.

Select an option

Save lopesivan/d03781bc035d0967d65efe07be60c408 to your computer and use it in GitHub Desktop.
INSTALL APK
#!/usr/bin/env bash
#
# install-apk.sh - Install APK files via ADB
# Description: Robust APK installer with device detection, batch installation,
# and error handling
#
set -e # Exit on error
# ============================================================================
# Configuration
# ============================================================================
readonly SCRIPT_NAME=$(basename "$0")
readonly ADB_TIMEOUT=5000 # milliseconds
# Colors for output
readonly COLOR_RED='\033[0;31m'
readonly COLOR_GREEN='\033[0;32m'
readonly COLOR_YELLOW='\033[1;33m'
readonly COLOR_BLUE='\033[0;34m'
readonly COLOR_RESET='\033[0m'
# ============================================================================
# Helper Functions
# ============================================================================
log() {
echo -e "${COLOR_BLUE}[INFO]${COLOR_RESET} $*"
}
success() {
echo -e "${COLOR_GREEN}[SUCCESS]${COLOR_RESET} $*"
}
warn() {
echo -e "${COLOR_YELLOW}[WARNING]${COLOR_RESET} $*"
}
error() {
echo -e "${COLOR_RED}[ERROR]${COLOR_RESET} $*" >&2
}
fatal() {
error "$*"
exit 1
}
# ============================================================================
# ADB Functions
# ============================================================================
check_adb_installed() {
if ! command -v adb &> /dev/null; then
fatal "ADB not found. Please install Android SDK Platform Tools."
fi
log "ADB found: $(adb version | head -n1)"
}
start_adb_server() {
log "Starting ADB server..."
adb start-server &> /dev/null || warn "Failed to start ADB server"
}
list_devices() {
local devices
devices=$(adb devices | grep -v "List of devices" | grep "device$" | cut -f1)
echo "$devices"
}
count_devices() {
local devices="$1"
echo "$devices" | grep -c . || echo "0"
}
select_device() {
local devices
devices=$(list_devices)
local device_count
device_count=$(count_devices "$devices")
if [ "$device_count" -eq 0 ]; then
fatal "No devices found. Please connect a device and enable USB debugging."
fi
if [ "$device_count" -eq 1 ]; then
echo "$devices"
return 0
fi
# Multiple devices - let user choose
log "Multiple devices detected:"
echo ""
local index=1
while IFS= read -r device; do
local model=$(adb -s "$device" shell getprop ro.product.model 2>/dev/null | tr -d '\r')
local android=$(adb -s "$device" shell getprop ro.build.version.release 2>/dev/null | tr -d '\r')
echo " [$index] $device - $model (Android $android)"
((index++))
done <<< "$devices"
echo ""
read -p "Select device number: " choice
local selected_device
selected_device=$(echo "$devices" | sed -n "${choice}p")
if [ -z "$selected_device" ]; then
fatal "Invalid selection"
fi
echo "$selected_device"
}
get_device_info() {
local device="$1"
local model=$(adb -s "$device" shell getprop ro.product.model 2>/dev/null | tr -d '\r')
local android=$(adb -s "$device" shell getprop ro.build.version.release 2>/dev/null | tr -d '\r')
local sdk=$(adb -s "$device" shell getprop ro.build.version.sdk 2>/dev/null | tr -d '\r')
log "Device: $model (Android $android / SDK $sdk)"
}
# ============================================================================
# APK Installation Functions
# ============================================================================
validate_apk() {
local apk_file="$1"
if [ ! -f "$apk_file" ]; then
error "File not found: $apk_file"
return 1
fi
if [[ ! "$apk_file" =~ \.apk$ ]]; then
error "Not an APK file: $apk_file"
return 1
fi
return 0
}
install_single_apk() {
local device="$1"
local apk_file="$2"
local install_flags="${3:-}"
log "Installing: $(basename "$apk_file")"
local output
if output=$(adb -s "$device" install ${install_flags} "$apk_file" 2>&1); then
if echo "$output" | grep -q "Success"; then
success "Installed: $(basename "$apk_file")"
return 0
else
error "Installation failed: $output"
return 1
fi
else
error "ADB error: $output"
return 1
fi
}
install_multiple_apks() {
local device="$1"
local install_flags="$2"
shift 2
local apk_files=("$@")
local total=${#apk_files[@]}
local installed=0
local failed=0
log "Installing $total APK(s)..."
echo ""
for apk_file in "${apk_files[@]}"; do
if validate_apk "$apk_file"; then
if install_single_apk "$device" "$apk_file" "$install_flags"; then
((installed++))
else
((failed++))
fi
else
((failed++))
fi
echo ""
done
echo "================================"
success "Installed: $installed"
[ $failed -gt 0 ] && error "Failed: $failed" || true
echo "================================"
}
# ============================================================================
# Uninstall Functions
# ============================================================================
uninstall_package() {
local device="$1"
local package_name="$2"
log "Uninstalling: $package_name"
local output
if output=$(adb -s "$device" uninstall "$package_name" 2>&1); then
if echo "$output" | grep -q "Success"; then
success "Uninstalled: $package_name"
return 0
else
error "Uninstall failed: $output"
return 1
fi
else
error "ADB error: $output"
return 1
fi
}
# ============================================================================
# Utility Functions
# ============================================================================
list_installed_packages() {
local device="$1"
local filter="${2:-}"
log "Listing installed packages..."
if [ -z "$filter" ]; then
adb -s "$device" shell pm list packages | sed 's/package://'
else
adb -s "$device" shell pm list packages | grep "$filter" | sed 's/package://'
fi
}
show_usage() {
cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS] <APK_FILE(s)>
Install APK files to Android device via ADB
OPTIONS:
-r, --replace Replace existing application
-d, --downgrade Allow version code downgrade
-g, --grant Grant all runtime permissions
-t, --test Install as test package
-s, --system Install to system partition (requires root)
-u, --uninstall Uninstall package by name
-l, --list List installed packages
-h, --help Show this help message
EXAMPLES:
# Install single APK
$SCRIPT_NAME app.apk
# Install multiple APKs
$SCRIPT_NAME app1.apk app2.apk app3.apk
# Install all APKs in directory
$SCRIPT_NAME *.apk
# Replace existing app
$SCRIPT_NAME -r app.apk
# Install with all permissions granted
$SCRIPT_NAME -g app.apk
# Uninstall package
$SCRIPT_NAME -u com.example.app
# List installed packages
$SCRIPT_NAME -l
$SCRIPT_NAME -l chrome # Filter by name
EOF
}
# ============================================================================
# Argument Parsing
# ============================================================================
parse_arguments() {
local install_flags=""
local mode="install"
local package_name=""
local filter=""
local apk_files=()
if [ $# -eq 0 ]; then
show_usage
exit 0
fi
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_usage
exit 0
;;
-r|--replace)
install_flags="${install_flags} -r"
shift
;;
-d|--downgrade)
install_flags="${install_flags} -d"
shift
;;
-g|--grant)
install_flags="${install_flags} -g"
shift
;;
-t|--test)
install_flags="${install_flags} -t"
shift
;;
-s|--system)
install_flags="${install_flags} -s"
shift
;;
-u|--uninstall)
mode="uninstall"
package_name="$2"
shift 2
;;
-l|--list)
mode="list"
filter="${2:-}"
shift
[ -n "$filter" ] && shift
;;
-*)
fatal "Unknown option: $1"
;;
*)
apk_files+=("$1")
shift
;;
esac
done
# Export for use in main
export INSTALL_FLAGS="$install_flags"
export MODE="$mode"
export PACKAGE_NAME="$package_name"
export FILTER="$filter"
export APK_FILES=("${apk_files[@]}")
}
# ============================================================================
# Main Execution
# ============================================================================
main() {
echo "=== APK Installer via ADB ==="
echo ""
# Parse command line arguments
parse_arguments "$@"
# Check ADB installation
check_adb_installed
# Start ADB server
start_adb_server
# Select device
local device
device=$(select_device)
echo ""
get_device_info "$device"
echo ""
# Execute based on mode
case "$MODE" in
install)
if [ ${#APK_FILES[@]} -eq 0 ]; then
fatal "No APK files specified"
fi
install_multiple_apks "$device" "$INSTALL_FLAGS" "${APK_FILES[@]}"
;;
uninstall)
if [ -z "$PACKAGE_NAME" ]; then
fatal "No package name specified"
fi
uninstall_package "$device" "$PACKAGE_NAME"
;;
list)
list_installed_packages "$device" "$FILTER"
;;
esac
echo ""
log "Operation completed"
}
# Run main function
main "$@"
exit 0
@lopesivan
Copy link
Author

como usar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment