Last active
January 5, 2026 20:57
-
-
Save vbnin/2cad617f8872816f7ecd3fcbe7f3e029 to your computer and use it in GitHub Desktop.
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
| #!/bin/zsh | |
| #################################################################################################### | |
| # | |
| ### Written by: Vincent Bonnin - Technical Enablement Manager at Jamf | |
| ### Last updated on 5 January 2026 | |
| ### Version: 1.1 | |
| # | |
| ### This script will extract the signing key and app package name from any Android app provided as a .apk file | |
| ### This script will also check and/or install prerequisites tools. It is required to run the script as root when installing Android CLI tools. | |
| ### This script has been tested with the following setup: | |
| # - A MacBook Pro M1 (Silicon architecture) | |
| # - Java Open JDK 21 LTS installed | |
| # | |
| ### INSTRUCTIONS | |
| # | |
| # 1) Ensure Java JDK21 is installed on the Mac by running the command 'java -version'. | |
| # You can download an installer PKG here: https://adoptium.net/en-GB/temurin/releases/?os=mac&package=jdk&version=21 | |
| # | |
| # 2) Review and edit the editable variables if needed | |
| # | |
| # 3) On the computer, run the script by providing a .apk file path as an argument. E.g.: sudo zsh "/path/to/your/script.sh" "/path/to/your/app.apk" | |
| # | |
| #################################################################################################### | |
| echo "### Starting script to extract the package name and signing key from a .apk file ###" | |
| # Editable variables (edit only if needed) | |
| DOWNLOAD_URL="https://dl.google.com/android/repository/commandlinetools-mac-11076708_latest.zip" # Define the download URL of Android CLI tools | |
| SDK_ROOT="/Library/Android/SDK" | |
| TARGET_APK="$1" # Replace $1 with your .apk file path or run the script with providing the file path as an argument | |
| # Check if JDK is installed | |
| echo "Checking if Java JDK 21 or later is installed on this Mac" | |
| JAVA_VERSION=$(java -version 2>&1 | grep -oE '"[0-9]+(\.[0-9]+)*"' | tr -d '"') | |
| JAVA_MAJOR_VERSION=$(echo "$JAVA_VERSION" | cut -d'.' -f1) | |
| if [ "$JAVA_MAJOR_VERSION" -ge 21 ]; then | |
| echo "Java JDK 21 or later is installed. Proceeding..." | |
| # Proceed with the script | |
| else | |
| echo "Java JDK is not installed or below the required version. Cannot proceed..." | |
| exit 1 | |
| fi | |
| # Check if Android SDK folder exists | |
| SDK_PATH="${SDK_ROOT}/cmdline-tools/latest" | |
| if [[ ! -e "${SDK_PATH}" ]]; then | |
| # Create SDK root directory | |
| echo "Setting SDK install directory to ${SDK_PATH}" | |
| mkdir -p "${SDK_PATH}" | |
| chmod 755 "${SDK_PATH}" | |
| else | |
| echo "Android SDK root folder found, proceeding" | |
| fi | |
| # Check if Android Build tools are available | |
| echo "Checking if Android Build tools are available" | |
| AAPT=$(find ${SDK_ROOT}/ -name 'aapt') | |
| APKSIGNER=$(find ${SDK_ROOT}/ -name 'apksigner') | |
| if [[ -z ${AAPT} || -z ${APKSIGNER} ]]; then | |
| # Download and install Android CLI Tools | |
| echo "Couldn't find Android Build Tools... Downloading and installing Android CLI and Build Tools now" | |
| curl -L -o /tmp/cli.zip "$DOWNLOAD_URL" | |
| unzip -qq /tmp/cli.zip -d ${SDK_PATH}/tmp | |
| mv ${SDK_PATH}/tmp/cmdline-tools/* ${SDK_PATH} | |
| rm -rf ${SDK_PATH}/tmp | |
| # Check if SDK manager has been properly installed before proceeding | |
| if [[ ! -e "${SDK_PATH}/bin/sdkmanager" ]]; then | |
| echo "The sdkmanager binary hasn't been found in ${SDK_PATH}/bin. Maybe check the download URL is still valid? Exiting now..." | |
| exit 1 | |
| fi | |
| # Install additional resources needed by Android CLI tools | |
| echo "Installing additional SDK tools" | |
| echo y | ${SDK_PATH}/bin/sdkmanager --install "build-tools;35.0.0" | |
| AAPT=$(find ${SDK_ROOT}/ -name 'aapt') | |
| APKSIGNER=$(find ${SDK_ROOT}/ -name 'apksigner') | |
| if [[ -z ${AAPT} || -z ${APKSIGNER} ]]; then | |
| echo "Still unable to find Android build tools, aborting..." | |
| exit 1 | |
| fi | |
| else | |
| echo "Android CLI tools are installed on this Mac" | |
| fi | |
| # Extract info from the APK | |
| echo "Starting extraction process" | |
| echo "Customizing Java JDK options" | |
| export JDK_JAVA_OPTIONS="--enable-native-access=ALL-UNNAMED" | |
| PACKAGE_NAME=$("${AAPT}" dump badging "${TARGET_APK}" | grep package | sed "s/ versionCode=.*//") | |
| SIGNING_KEY=$("${APKSIGNER}" verify --print-certs "${TARGET_APK}" | grep "Signer #1 certificate SHA-256 digest:") | |
| echo "Extraction complete! Results:" | |
| echo " - ${PACKAGE_NAME}" | |
| echo " - ${SIGNING_KEY}" | |
| exit 0 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script will extract the signing key and app package name from any Android app provided as a .apk file
This script will also check and/or install prerequisites tools
This script has been tested with the following setup:
INSTRUCTIONS:
Ensure Java JDK21 is installed on the Mac by running the command 'java -version'.
You can download an installer PKG here: https://adoptium.net/en-GB/temurin/releases/?os=mac&package=jdk&version=21
Review and edit the editable variables if needed
On the computer, run the script by providing a .apk file path as an argument. Running the script as root is required when installing Android CLI tools.
E.g.:
sudo zsh "/path/to/your/script.sh" "/path/to/your/app.apk"Results will be provided in this form: