Skip to content

Instantly share code, notes, and snippets.

@bradiosd
Last active February 23, 2017 11:43
Show Gist options
  • Save bradiosd/7224e9f2eb26e696b7e6670d33d8c7de to your computer and use it in GitHub Desktop.
Save bradiosd/7224e9f2eb26e696b7e6670d33d8c7de to your computer and use it in GitHub Desktop.
Automatically build and sign Android APK files for Ionic 2.0
#!/bin/bash
# Author: Brad Bird
# Website: http://bradders.rocks
# Email: hello@bradders.rocks
# Instructions
# 0. Place this file in the root directory of your Ionic app
# 1. If you haven't made a keystore file go here https://developer.android.com/studio/publish/app-signing.html#signing-manually
# 2. Place your .keystore file in the root directory
# 3. Make sure you have access to "zipalign" and "apksigner" commands by typing them in the terminal
# 4. ????
# 5. PROFIT
# Set colours
RESTORE='\033[0m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
# Set up some vars
# Can change these to suit your preferences
APK_DIR=platforms/android/build/outputs/apk/
KEYSTORE_DEFAULT=braddersrocks.keystore
# Check .keystore file exists
if [ ! -f ${KEYSTORE_DEFAULT} ]; then
echo -e "${RED}ERROR${RESTORE} Keystore file not found!"
exit
fi
# Enter version number for build.
read -p "What version is this build?: " APK_VERSION
# Check .keystore file exists
if [ -z ${APK_VERSION} ]; then
echo -e "${RED}ERROR${RESTORE} You must set a version number!"
exit
fi
# Check if the release already exists
# If it does ask the user if they wish to rebuild it
if [ -f ${APK_DIR}android-release-${APK_VERSION}.apk ]; then
echo -ne "${YELLOW}WARNING${RESTORE}: "
read -p "A version already exists for ${APK_VERSION}. Do you want to overwrite it? (y/n): " CONFIRM_OVERWRITE
# If nothing is entered assume it is a "y"
if [ -z ${CONFIRM_OVERWRITE} ]; then
CONFIRM_OVERWRITE="y"
fi
if [ ${CONFIRM_OVERWRITE} = "n" ]; then
exit
fi
fi
# Make Ionic release build for Android
ionic build android --release
# Align the .apk
zipalign -v -p 4 ${APK_DIR}android-release-unsigned.apk ${APK_DIR}android-release-unsigned-aligned.apk
# Sign the .apk with the version number provided earlier
apksigner sign --ks braddersrocks.keystore --out ${APK_DIR}android-release-${APK_VERSION}.apk ${APK_DIR}android-release-unsigned-aligned.apk
# Clear temporary unsigned/unaligned .apks
rm ${APK_DIR}android-release-unsigned.apk
rm ${APK_DIR}android-release-unsigned-aligned.apk
# Success message!
echo -e "${GREEN}SUCCESS${RESTORE}: Your signed Android release has been built"
echo -e "${GREEN}SUCCESS${RESTORE}: APK located at ${APK_DIR}android-release-${APK_VERSION}.apk"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment