Skip to content

Instantly share code, notes, and snippets.

@austynmahoney
Created July 27, 2018 04:02
Show Gist options
  • Save austynmahoney/db8e6fd8080fa76466fcb699b04d34ef to your computer and use it in GitHub Desktop.
Save austynmahoney/db8e6fd8080fa76466fcb699b04d34ef to your computer and use it in GitHub Desktop.
Android APK Signer
# MIT License
#
# Copyright (c) 2018 Austyn Mahoney
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#!/usr/bin/env bash
# Store Android SDK Path
if [[ -z "${ANDROID}" ]]; then
echo "\$ANDROID environment variable must be set to SDK Path"
exit 1
else
ANDROID_SDK=${ANDROID}
fi
usage () {
cat <<HELP_USAGE
usage: $0 [--i <path>] [--o <path>] [--k <path>] [--b <version>]
-i Input APK path
-o Output APK path
-k Keystore to sign the APK with
-b Build-tools version used to sign the APK (e.g. "27.0.3")
HELP_USAGE
}
# Gets the command name without path
cmd(){ echo `basename $0`; }
# Error message
error(){
echo "`cmd`: invalid option -- '$1'";
echo "Try '`cmd` -h' for more information.";
exit 1;
}
while getopts :i:o:k:b: args; do
case "${args}" in
i) INPUT=${OPTARG};;
o) OUTPUT=${OPTARG};;
k) KEYSTORE=${OPTARG};;
b) BUILD_TOOLS_VERSION=${OPTARG};;
:) echo error $OPTARG 1>&2;;
\?) echo usage
esac
done
# Build path to tools
BUILD_TOOLS="$ANDROID_SDK/build-tools/$BUILD_TOOLS_VERSION"
ZIPALIGN="$BUILD_TOOLS/zipalign" # https://developer.android.com/studio/command-line/zipalign.html
APKSIGNER="$BUILD_TOOLS/apksigner" # https://developer.android.com/studio/command-line/apksigner.html
if [ -z "${INPUT}" ] || [ -z "${OUTPUT}" ] || [ -z "${KEYSTORE}" ] || [ -z "${BUILD_TOOLS_VERSION}" ]; then
echo "INVALID USAGE"
usage
exit 1
fi
if [ ! -x ${APKSIGNER} ]; then
echo "Invalid apksigner path: \"$APKSIGNER\""
exit 1
fi
tput rmam;
echo ""
echo " █████╗ ██████╗ ██╗ ██╗ ███████╗██╗ ██████╗ ███╗ ██╗███████╗██████╗ "
echo "██╔══██╗██╔══██╗██║ ██╔╝ ██╔════╝██║██╔════╝ ████╗ ██║██╔════╝██╔══██╗"
echo "███████║██████╔╝█████╔╝ ███████╗██║██║ ███╗██╔██╗ ██║█████╗ ██████╔╝"
echo "██╔══██║██╔═══╝ ██╔═██╗ ╚════██║██║██║ ██║██║╚██╗██║██╔══╝ ██╔══██╗"
echo "██║ ██║██║ ██║ ██╗ ███████║██║╚██████╔╝██║ ╚████║███████╗██║ ██║"
echo "╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝"
echo ""
tput smam
echo "-- Signing \"${INPUT}\" --"
echo "You will now be prompted for the keystore/alias password"
echo ""
# Zip Align the APK
TEMP_ZIP_ALIGN_FILE=$(mktemp)
eval "${ZIPALIGN} -p -f 4 ${INPUT} ${TEMP_ZIP_ALIGN_FILE}"
if [ $? -ne 0 ]; then
echo "Failed to zipalign APK"
exit 2
fi
INPUT=${TEMP_ZIP_ALIGN_FILE}
# Sign the APK
eval "${APKSIGNER} sign --ks ${KEYSTORE} --out ${OUTPUT} ${INPUT}"
if [ $? -eq 0 ]; then
echo "-- APK signed successfully: \"$OUTPUT\" --"
else
echo ""
echo "Error signing APK!"
exit 3
fi
echo ""
echo "-- Verifying APK Signature... --"
echo ""
eval "${APKSIGNER} verify ${OUTPUT}"
echo ""
echo "-- Finished apksigner verification --"
echo ""
echo "Signed APK: \"$OUTPUT\""
# Delete temp file used for zipalign
rm ${TEMP_ZIP_ALIGN_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment