Skip to content

Instantly share code, notes, and snippets.

@rek
Last active August 29, 2015 13:57
Show Gist options
  • Save rek/9593421 to your computer and use it in GitHub Desktop.
Save rek/9593421 to your computer and use it in GitHub Desktop.
Cordova - Android APK Deploy to Production
#!/bin/bash
PROJECTNAME="myproject"
KEYREPO="prod"
echo -e '\n======================================================='
echo "# APK Builder for Play Store deployment #"
echo '======================================================='
echo ' '
read -p "Are you sure you want to do this? [y/n] " -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ' '
echo -e 'Starting build script (Note this can take 2-3 minutes)'
echo ' '
echo '################################'
echo ' STEP ONE: Preparing project'
echo '################################'
echo ' '
# move the files from the project into the android platform
cordova prepare android
echo -e 'Done.\n'
cd platforms/android
echo '################################'
echo ' STEP TWO: Cleaning project (Only do this if you know why)'
echo -e '################################\n'
read -p "Clean the build? [y/n] " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ' '
if ant clean; then
echo 'Clean done... moving on..'
else
echo ' '
echo 'Please fix errors before recleaning...'
echo ' '
exit 1
fi
else
echo ' '
echo "Clean skipped... don't worry, it's no problem."
fi
echo ' '
echo '################################'
echo ' STEP THREE: Building project'
echo '################################'
echo ' '
read -p "Re-build project? [y/n] " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ' '
if ant release; then
echo 'Build done... moving on..'
else
echo ' '
echo 'Please fix errors before rebuilding...'
echo ' '
exit 1
fi
fi
cd ../..
if [ ! -f platforms/android/bin/$PROJECTNAME-release-unsigned.apk ]; then
echo -e "\n\nERROR: APK Not found! Please re-build the project."
echo -e '\nExiting...\n'
exit 1
fi
cp platforms/android/bin/$PROJECTNAME-release-unsigned.apk .
zip -d $PROJECTNAME-release-unsigned.apk assets/www/res/*
jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore ~/.android/prod.keystore -storepass android $PROJECTNAME-release-unsigned.apk $KEYREPO
rm $PROJECTNAME.prod.apk
zipalign -v 4 $PROJECTNAME-release-unsigned.apk $PROJECTNAME.prod.apk
echo ' '
echo 'Building APK Finished'
echo ' '
echo '#################################'
echo ' STEP LAST: Updating the version'
echo '#################################'
echo ' '
read -p "Update the version in AndroidManifest? (Note: Only do so if you plan to upload to Play Store) [y/n] " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
if perl < /dev/null > /dev/null 2>&1 ;
then
if perl -i -pe 's/(versionCode=\")([0-9]+)/{$1.(1+$2)}/ge' platforms/android/AndroidManifest.xml ; then
echo -e '\nAndroidManifest version updated'
echo ' '
else
echo -e '\nError updating AndroidManifest.xml\n'
exit 1
fi
else echo "Cannot find perl. Please manually increment the versionCode in AndroidManafest.xml"
exit 1
fi
read -p "Commit to version control? [y/n] " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo ' '
echo '..Committing changes to version control (Git)'
git commit -a -m "New version of APK built from $HOSTNAME" && git push
echo 'Done.'
echo ' '
fi
fi
echo -e '\nAll work here done. Congratulations!\n'
else # if the user pressed no, then just quit
echo -e '\nExiting..\n'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment