apex-forger/repack.sh
#!/bin/bash
set -euo pipefail

MYPATH=$(dirname "$0")
source "$MYPATH/../envsetup.sh"

if test "$#" -ne 5 ; then
	echo "Usage: $(basename "$0") source-dir payload_privkey payload_pubkey apk_privkey apk_cert" >&2
	echo "  payload_privkey: .pem" >&2
	echo "  payload_pubkey: .avbpubkey" >&2
	echo "  apk_privkey: .pk8" >&2
	echo "  apk_cert: .x509.pem" >&2
	exit 1
fi

if ! test -d "$1" ; then
	echo "\"$1\" should be a directory created by unpack.sh" >&2
	exit 2
fi

declare -a EXTRA_ARGS
if test -f "$1/canned_fs_config" ; then EXTRA_ARGS+=("--canned_fs_config" "$1/canned_fs_config") ; fi
if test -f "$1/AndroidManifest.xml" ; then EXTRA_ARGS+=("--android_manifest" "$1/AndroidManifest.xml") ; fi

apexer -v \
	--apexer_tool_path "$ANDROID_HOST_OUT/bin" \
	--build_info "$1/apex_build_info.pb" \
	--manifest "$1/apex_manifest.pb" \
	--android_jar_path "$AOSP/prebuilts/sdk/$API_VER/public/android.jar" \
	--key "$2" \
	--pubkey "$3" \
	"${EXTRA_ARGS[@]}" \
	"$1/payload" "$1/forged-unaligned.apex"

zipalign 4096 "$1/forged-unaligned.apex" "$1/forged.apex"
apksigner sign --key "$4" --cert "$5" "$1/forged.apex"
