Skip to content

Instantly share code, notes, and snippets.

@kiwipxl
Created February 20, 2018 05:29
Show Gist options
  • Save kiwipxl/4fa0b0f7dbce2a9e56f1e99b6f45f208 to your computer and use it in GitHub Desktop.
Save kiwipxl/4fa0b0f7dbce2a9e56f1e99b6f45f208 to your computer and use it in GitHub Desktop.
#signs an apk with a keystore (even if it's already been signed)
#example use: sh sign.sh builds/android.apk metro.keystore
APK_PATH=$1
KEYSTORE_PATH=$2
if [ ! -e "$APK_PATH" ]
then
echo "could not find apk at path $APK_PATH"
exit
fi
if [ ! -e "$KEYSTORE_PATH" ]
then
echo "could not find keystore at path $KEYSTORE_PATH"
exit
fi
APK_OUT_PATH=out.apk
#now we need to find our build-tools path in the android sdk (where apksigner lives)
if [ -e "$ANDROID_HOME" ]
then
ANDROID_SDK_PATH=$ANDROID_HOME
else
ANDROID_SDK_PATH=$HOME/Library/Android/sdk
fi
if [ ! -e "$ANDROID_SDK_PATH" ]
then
echo "failed to find android sdk path at $ANDROID_SDK_PATH"
exit
fi
TOOLS_PATH=$ANDROID_SDK_PATH/build-tools/27.0.0
if [ ! -e "$TOOLS_PATH" ]
then
echo "failed to find android sdk build-tools path at $TOOLS_PATH"
exit
fi
keytool -list -printcert -jarfile $APK_PATH
echo "found android sdk build-tools at $TOOLS_PATH"
sleep 2
#need to unzip and delete META_INF before we can resign an apk
rm -rf temp_apk_zip
mkdir temp_apk_zip
unzip $APK_PATH -d temp_apk_zip
rm -rf temp_apk_zip/META-INF
cd temp_apk_zip
find . | zip ../$APK_OUT_PATH -@
cd ../
rm -rf temp_apk_zip
#now we can sign the apk.
#we could use jarsigner, but apksigner is more appropriate for apks
$TOOLS_PATH/apksigner sign --ks $KEYSTORE_PATH $APK_OUT_PATH
echo "signed $APK_OUT_PATH with $KEYSTORE_PATH"
keytool -list -v -printcert -jarfile $APK_OUT_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment