From 1199905e59ae3435072581c171ae0d83e0bb3c16 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Fri, 21 Sep 2018 10:51:27 +0800 Subject: [PATCH] Add tools to generate and sign release apks --- .gitignore | 2 ++ Dockerfile | 4 ++++ genkeystore | 46 +++++++++++++++++++++++++++++++++++++++ release | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100755 genkeystore create mode 100755 release diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb452fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +releases.jks +releases.hex diff --git a/Dockerfile b/Dockerfile index 84d2e11..45ab88e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,10 +43,14 @@ RUN echo "y" | sdkmanager \ # "ndk-bundle" \ # "lldb;2.3" \ # "cmake;3.6.4111459" \ + "extras;google;m2repository" \ "platform-tools" \ "platforms;android-26" \ "build-tools;26.0.2" +# Add more build tools +COPY release /usr/local/bin/release + # Build directory ENV SRC /src RUN mkdir $SRC diff --git a/genkeystore b/genkeystore new file mode 100755 index 0000000..732df6b --- /dev/null +++ b/genkeystore @@ -0,0 +1,46 @@ +#!/bin/sh + +set -e + +ALIAS=release +COMMON_NAME=AppVenture +STORE_FILE=releases.jks +STORE_FILE_HEX=releases.hex +STORE_PASS=$1 +VALIDITY=10000 + +if [ -z "$STORE_PASS" ]; then + echo "Usage: ./genkeystore " + exit 1 +fi + +echo +echo "Using keytool to generate a keystore" + +keytool -genkey -v \ + -keystore "$STORE_FILE" \ + -storepass "$STORE_PASS" \ + -keyalg RSA -keysize 2048 \ + -validity "$VALIDITY" \ + -alias "$ALIAS" \ + -dname "CN=$COMMON_NAME" + +echo +echo "Exporting hex keystore" + +xxd -ps "$STORE_FILE" > "$STORE_FILE_HEX" + +echo +echo "Here's the environmental variables you need to set:" + +echo +echo "store_file" +cat "$STORE_FILE_HEX" + +echo +echo "store_password" +echo "$STORE_PASS" + +echo +echo "key_alias" +echo "$ALIAS" diff --git a/release b/release new file mode 100755 index 0000000..819cbaa --- /dev/null +++ b/release @@ -0,0 +1,62 @@ +#!/bin/sh + +set -e + +BUILT_APK=app/build/outputs/apk/app-unsigned.apk +ALIGNED_APK=app/build/outputs/apk/app-unsigned-aligned.apk +SIGNED_APK=app/build/outputs/apk/app-release.apk + +TEMP_DIR="$(mktemp)" +TEMP_STORE_FILE="$TEMP_DIR/store.jks" + +if [ -z "$store_file" ] || [ -z "$store_password" ] || [ -z "$key_alias" ]; then + echo "store_file, store_password or key_alias are not configured secrets. Aborting..." + exit 1 +fi + +echo +echo "Reading store_file from environment" +echo $store_file | xxd -ps -r > $TEMP_STORE_FILE + +# The following file is not required for the following process +# but I'm gonna leave it here anyway +echo > keystore.properties << EOF +storeFile=$TEMP_STORE_FILE +storePassword=$store_password +keyAlias=$key_alias +EOF + +echo +echo "I think you're lazy so we will build a unsigned release APK and then sign it manually," +ecoh "rather than using the gradle process." + +echo +echo "Building unsigned release APK" +set -x +./gradlew assembleRelease +set +x + +echo +echo "Doing zipalign" +set -x +zipalign -v -p 4 $BUILT_APK $ALIGNED_APK +set +x + +echo +echo "Signing" +set -x +apksigner sign \ + --ks $TEMP_STORE_FILE \ + --ks-pass pass:$store_password \ + --ks-key-alias $key_alias \ + --out $SIGNED_APK \ + $ALIGNED_APK +set +x + +echo +echo "Verifying" +set -x +apksigner verify $SIGNED_APK +set +x + +echo "Done!"