Skip to content

Instantly share code, notes, and snippets.

@uzen
Created September 17, 2018 23:27
Show Gist options
  • Save uzen/5e648a00658a7d8d5fe0d4f79810b2d6 to your computer and use it in GitHub Desktop.
Save uzen/5e648a00658a7d8d5fe0d4f79810b2d6 to your computer and use it in GitHub Desktop.
Signapk
test-key
- testkey.pk8
- testkey.x509.pem
#!/bin/bash
BUILD_TOOLS_VERSION=28.0.2
prg="$0"
# Need this for relative symlinks.
while [ -h "$prg" ] ; do
ls=`ls -ld "$prg"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
prg="$link"
else
prg=`dirname "$prg"`"/$link"
fi
done
saved="`pwd`"
cd "`dirname \"$prg\"`" >/dev/null
app_dir="`pwd -P`"
root_dir=`dirname "${app_dir}"`
cd "$saved" >/dev/null
ANDROID_HOME=${ANDROID_HOME:-$root_dir}
ANDROID_BUILD_TOOLS="$ANDROID_HOME/android-sdk/build-tools/$BUILD_TOOLS_VERSION"
declare -A KEYFILES=([key]="testkey.pk8" [cert]="testkey.x509.pem")
OUTPUT_APK="$2"
if [ -z "$1" ]; then
echo "Unknown path to the original APK"
exit 1
else
if [ -z "$OUTPUT_APK" ]; then
echo "Unknown path to the signed APK"
filename=$(basename "$1")
extension="${filename##*.}"
OUTPUT_APK="${filename%.*}"_signed.$extension
echo "New file name: $OUTPUT_APK"
fi
if [ -f "$OUTPUT_APK" ]; then
read -p "File with the same name already exists, overwrite? [y/N]: " response
if [[ ${response,,} != y ]]; then
echo "Shutdown..."
exit 0
fi
echo "File will be overwritten: $OUTPUT_APK"
rm -f $OUTPUT_APK
fi
fi
libdir="$app_dir/test-key"
for file in ${KEYFILES[@]}; do
if [ ! -r "$libdir/$file" ]; then
echo `basename "$prg"`": can't find $file"
exit 1
fi
done
scheme=""
options="--key $libdir/${KEYFILES[key]} --cert $libdir/${KEYFILES[cert]}"
while expr "x$1" : 'x--J' >/dev/null; do
opt=`expr "$1" : '--J\(.*\)'`
if [[ "$opt" =~ ^v[0-2]-signing-enabled$ ]]; then
scheme=${opt:1:1}
else
options="${options} --${opt}"
fi
shift
done
if [[ -z $scheme ]]; then
echo "Use the traditional apk signing scheme?"
echo "verison: [0]all [1-2]1 or 2 [3]auto"
read -p "[0-3]:" scheme
fi
message=""
case $scheme in
[0-2]*)
message="APK Signature Scheme v$scheme"
[[ $scheme -le 2 ]] && val="true" || val="false"
options+=" --v1-signing-enabled $val"
[[ $scheme -ne 1 ]] && val="true" || val="false"
options+=" --v2-signing-enabled $val"
;;
*)
message="Uses the values of --min-sdk-version and --max-sdk-version to decide when to apply this signature scheme."
;;
esac
echo $message
$ANDROID_BUILD_TOOLS/zipalign -v -p 4 "$1" "$OUTPUT_APK"
$ANDROID_BUILD_TOOLS/apksigner sign $options "$OUTPUT_APK"
@uzen
Copy link
Author

uzen commented Sep 29, 2018

  1. права доступа 755 для скрипта
  2. создаём ключи в папке test-key (testkey.pk8 testkey.x509.pem)
  3. указываем путь к родительской папке android-sdk (см. строки 21-22)
    #если все инструменты находятся в одном каталоге, то этот шаг можно пропустить
    $ export ANDROID_HOME="/ABSOLUTE/PATH"
  4. запускаем наш скрипт
    $ ./sign input_apk # для выбра схемы добавьте ключ --v1-signing-enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment