Created
April 3, 2026 11:26
-
-
Save glowinthedark/ebd46ea5a121524871c2d456cda3ef48 to your computer and use it in GitHub Desktop.
get APK from android device
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # Enforce strict error handling | |
| set -euo pipefail | |
| # uncomment for DEBUGGING ONLY | |
| # set -exuo pipefail | |
| # --- take package name as first arg --- | |
| if [[ $# -ne 1 ]]; then | |
| echo "ERROR: Invalid arguments." | |
| echo "Usage: $0 <package_name>" | |
| echo "Example: $0 es.rae.dle" | |
| exit 1 | |
| fi | |
| PKG="$1" | |
| # --- 2. Check for ADB presence --- | |
| if ! command -v adb >/dev/null 2>&1; then | |
| echo "ERROR: 'adb' is not in your PATH. Fix your Android SDK setup." >&2 | |
| exit 1 | |
| fi | |
| if ! adb get-state >/dev/null 2>&1; then | |
| echo "ERROR: No device connected, or multiple devices detected." >&2 | |
| echo "If multiple devices, set the ANDROID_SERIAL environment variable first:" >&2 | |
| echo "export ANDROID_SERIAL=<device_id>" >&2 | |
| exit 1 | |
| fi | |
| echo "[*] Querying package manager for: $PKG" | |
| # --- 3. Retrieve and Sanitize Paths (macOS/Bash 3.2 Compatible) --- | |
| # We still strip the "package:" prefix and the hidden \r carriage returns. | |
| RAW_PATHS=() | |
| while IFS= read -r line; do | |
| # Only add non-empty lines to the array | |
| if [[ -n "$line" ]]; then | |
| RAW_PATHS+=("$line") | |
| fi | |
| done < <(adb shell pm path "$PKG" 2>/dev/null | sed 's/^package://' | tr -d '\r') | |
| if [[ ${#RAW_PATHS[@]} -eq 0 ]]; then | |
| echo "ERROR: Package '$PKG' not found on the device. Is it installed for the current user?" >&2 | |
| exit 1 | |
| fi | |
| # --- 4. Handle Monolithic vs. Split APKs --- | |
| if [[ ${#RAW_PATHS[@]} -eq 1 ]]; then | |
| # Traditional single APK | |
| APK_PATH="${RAW_PATHS[0]}" | |
| echo "[*] Found monolithic APK." | |
| echo "[*] Pulling from: $APK_PATH" | |
| # Pull and rename | |
| adb pull "$APK_PATH" "${PKG}.apk" | |
| echo "[+] Success! Saved as: ${PKG}.apk" | |
| else | |
| # Modern Split APK (App Bundle) | |
| echo "[!] WARNING: '$PKG' is a Split APK (Android App Bundle) composed of ${#RAW_PATHS[@]} parts." | |
| echo "[!] Pulling ONLY the base.apk will result in a broken app missing native libs or resources." | |
| OUT_DIR="${PKG}_apks" | |
| mkdir -p "$OUT_DIR" | |
| echo "[*] Creating directory ./${OUT_DIR}/ and pulling all parts..." | |
| for path in "${RAW_PATHS[@]}"; do | |
| echo " -> Pulling $(basename "$path")..." | |
| adb pull "$path" "$OUT_DIR/" >/dev/null | |
| done | |
| echo "[+] Success! All splits pulled to ./${OUT_DIR}/" | |
| echo "[i] To install this app elsewhere, you CANNOT use 'adb install'." | |
| echo "[i] You MUST use: adb install-multiple ${OUT_DIR}/*.apk" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment