Skip to content

Instantly share code, notes, and snippets.

@kamikat
Created November 28, 2014 15:43
Show Gist options
  • Save kamikat/2b9b7b98ecc87dfdb6dc to your computer and use it in GitHub Desktop.
Save kamikat/2b9b7b98ecc87dfdb6dc to your computer and use it in GitHub Desktop.
Post-building script patching AndroidManifest.xml for UMENG channel names
#!/bin/sh
####################################################################
# Channel distribution builder script
# Dependency:
# apktool 2.x
# Android SDK & Build Tools
# Usage:
# distchannel <apk file> <channel configuration> <output path>
# Environment Variable:
# TMPDIR - temporary file directory (set by default on OSX)
# KEYSTORE - path to keystore file
# KEYSYORE_PASS - password to keystore file and key alias (same?)
# KEY_ALIAS - key alias name
#####################################################################
ZIPALIGN="$HOME/.local/android-sdk/build-tools/19.1.0/zipalign"
APKTOOL2="$HOME/.local/bin/apktool"
APK_PATH="$1"
CHANNELS="$2"
DST_PATH="$3"
ROOT=$TMPDIR/"$(basename $0)-$(date +%s)"
APP_PATH=$ROOT/"build"
MANIFEST=$ROOT/"AndroidManifest.xml"
RS_PATCH=$ROOT/"patch.list"
APK_TEMP=$ROOT/"tmpbuild"
echo >&2 "Setup building root..."
mkdir -p "$ROOT" || exit 1
mkdir -p "$DST_PATH" || exit 1
echo >&2 "Decoding APK package..."
$APKTOOL2 d "$APK_PATH" -o "$APP_PATH" || exit 1
cp "$APP_PATH/AndroidManifest.xml" "$MANIFEST"
echo >&2 "APK package Decoded."
echo >&2 "Running package build test..."
$APKTOOL2 b "$APP_PATH" -o "$APK_TEMP.apk" 2>&1 | grep "Error retrieving parent for item" | sed "s@^\([^:]*\):\([^:]*\).* name '\(.*\)'.@\1:\2:\3@g" | sort -r > "$RS_PATCH"
echo >&2 "Applying resource patch..."
(
cat | awk -f - "$RS_PATCH" << EOF
BEGIN { FS=":" }
{
print "sed -i '' $'" \$2 "i\\\\\\\\\\\\n\\\\t" "<style name=\"" \$3 "\" parent=\"\" />\\\\n" "' " \$1
}
EOF
) < /dev/null | bash
cat "$CHANNELS" | (while read APK_DIST CHANNEL_NAME
do
echo >&2 "Editing AndroidManifest.xml for $CHANNEL_NAME..."
sed "s@generic@$CHANNEL_NAME@g" "$MANIFEST" > "$APP_PATH/AndroidManifest.xml"
echo >&2 "Building $APK_DIST($CHANNEL_NAME)..."
$APKTOOL2 b "$APP_PATH" -o "$APK_TEMP-unsigned.apk" || exit 1
echo >&2 "Signing..."
jarsigner -sigalg SHA1withRSA -digestalg SHA1 \
-keystore $KEYSTORE -storepass $KEYSTORE_PASS \
-signedjar "$APK_TEMP-unaligned.apk" "$APK_TEMP-unsigned.apk" "$KEY_ALIAS" || exit 1
echo >&2 "Running zipalign..."
$ZIPALIGN 4 "$APK_TEMP-unaligned.apk" "$DST_PATH/$APK_DIST" || exit 1
echo >&2 "Built."
done)
echo >&2 "Cleaning..."
rm -rf $ROOT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment