Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Created January 3, 2020 06:48
Show Gist options
  • Save gwsu2008/432c8c064187773a8489a4a5304c093b to your computer and use it in GitHub Desktop.
Save gwsu2008/432c8c064187773a8489a4a5304c093b to your computer and use it in GitHub Desktop.
Android APK decompile and update properties
#!/bin/bash +x
info() {
echo -e "\033[34m[INFO]\033[0m $1"
}
error() {
echo -e "\033[31m[ERROR] $1\033[0m"
}
success() {
echo -e "\033[32m[SUCCESS]\033[0m $1"
}
warn() {
echo -e "\033[33m[WARN]\033[0m $1"
}
#################################################
#
# Main
#
#################################################
MVN=$(which mvn 2>/dev/null)
if [ $? -ne 0 ]; then
if [ -f /usr/local/bin/mvn ]; then
MVN='/usr/local/bin/mvn'
elif [ -f $MAVEN_HOME/bin/mvn ]; then
MVN="$MAVEN_HOME/bin/mvn"
else
error "Maven installation not found."
exit 1
fi
fi
info "Get POM version from APA pom"
POM_FILE=${WORKSPACE}/devops/apa/pom-apa.xml
POM_INFO=$(${MVN} help:evaluate -f ${POM_FILE} -Dexpression=project.artifact.id -q -DforceStdout)
if [ $? -ne 0 ]; then
error "Failed to get version from pom file"
exit 1
fi
POM_VERSION=$(echo ${POM_INFO} | awk -F":" '{print $NF}')
POM_GROUP_ID=$(echo ${POM_INFO} | awk -F":" '{print $1}')
ARTIFACT_ID='apa'
info "ARTIFACT_ID: $ARTIFACT_ID"
cat << EOF > pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId><artifactId>download-artifact</artifactId><name>download-artifact</name><version>${POM_VERSION}</version>
<dependencies />
<build><extensions><extension><groupId>org.springframework.build</groupId><artifactId>aws-maven</artifactId><version>5.0.0.RELEASE</version></extension></extensions>
<plugins />
</build>
</project>
EOF
if [ -n "${SNAPSHOT_VERSION}" ]; then
info "Retrieving arfiact using snapshot version: $SNAPSHOT_VERSION from S3 repo"
POM_VERSION=${SNAPSHOT_VERSION}
fi
info "Retrieving arfiact $POM_GROUP_ID:$ARTIFACT_ID:$POM_VERSION from S3 repo"
${MVN} -s $WORKSPACE/settings-env.xml -B -U -f pom.xml org.apache.maven.plugins:maven-dependency-plugin:3.1.1:unpack \
-Dartifact=$POM_GROUP_ID:$ARTIFACT_ID:$POM_VERSION:zip -DoutputDirectory=target -Dmdep.useBaseVersion=true
if [ $? -ne 0 ]; then
error "Failed to retrieve artifact $POM_GROUP_ID:$ARTIFACT_ID:$POM_VERSION:zip"
exit 1
fi
cp -fv $WORKSPACE/keystore.jks $WORKSPACE/target/${KEY_STORE}
KEY_ALIAS='xyz'
KEY_STORE='keystore.jks'
KEY_PASSWORD='abc'
info "List key in the keystore ${KEY_STORE}"
keytool -list -v -keystore $WORKSPACE/target/${KEY_STORE} -storepass ${KEY_PASSWORD}
if [ $? -ne 0 ]; then
error "List keys from keystore failed"
exit 1
fi
pushd $WORKSPACE/target/ > /dev/null
info "Fetch apktool.zip from S3 repository"
wget -q https://repository/releases/io/github/ibotpeaches/apktool/apktool-2.4.0.zip
if [ $? -ne 0 ]; then
error "apktool.zip is missing"
exit 1
fi
info "Extract apktool.jar from zip archive"
unzip -oq apktool-2.4.0.zip && rm -f apktool-2.4.0.zip
info "Decompile APK: java -jar apktool.jar d apa-${version}-app-release.apk"
java -jar apktool.jar d apa-${version}-app-release.apk
if [ $? -ne 0 ]; then
error "Decompile apa-${version}-app-release.apk failed"
exit 1
fi
rm -f apa-${version}-app-release.apk
info "Update renameManifestPackage to ${PACKAGE_NAME}"
pushd apa-${version}-app-release/ > /dev/null
sed -i -e "s/renameManifestPackage.*/renameManifestPackage: '${PACKAGE_NAME}'/g" apktool.yml
OLD_PACKAGE=$(grep 'package="' AndroidManifest.xml | awk -F"package=" '{print $2}' | awk '{print $1}' | tr -d '"')
info "Replace package name ${OLD_PACKAGE} with ${PACKAGE_NAME}"
sed -i -e "s/${OLD_PACKAGE}/${PACKAGE_NAME}/g" AndroidManifest.xml
popd > /dev/null
info "Replace project.properties with project.properties.${PROPERTY_SUFFIX}"
cp -fv $WORKSPACE/project.properties.${PROPERTY_SUFFIX} $WORKSPACE/target/apa-${version}-app-release/assets/project.properties
if [ $? -ne 0 ]; then
error "Failed to update project.properties"
exit 1
fi
info "Build APK: java -jar apktool.jar b -f apa-${version}-app-release"
java -jar apktool.jar b -f apa-${version}-app-release > $WORKSPACE/apktool_build.log 2>&1
if [ $? -ne 0 ]; then
error "Compile APK failed"
exit 1
fi
mv -f $WORKSPACE/target/apa-${version}-app-release/dist/apa-${version}-app-release.apk $WORKSPACE/target/apa-${version}-app-release-unsigned.apk
if [ $? -ne 0 ]; then
error "Missing dist/apa-${version}-app-release.apk file"
exit 1
fi
info "Signing apa-${version}-app-release.apk with ${KEY_STORE}"
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ${KEY_STORE} -storetype jks -storepass ${KEY_PASSWORD} apa-${version}-app-release-unsigned.apk ${KEY_ALIAS} > $WORKSPACE/jarsigner_build.log 2>&1
if [ $? -ne 0 ]; then
error "APK signing failed"
exit 1
fi
info "Verify apa-${version}-app-release.apk Signature"
echo "jarsigner -verify -verbose -certs apa-${version}-app-release-unsigned.apk"
jarsigner -verify -verbose -certs apa-${version}-app-release-unsigned.apk
ANDROID_BUILD_TOOLS_VERSION=$(grep ANDROID_BUILD_TOOLS_VERSION $WORKSPACE/gradle.properties | awk -F= '{print $2}')
info "Run zipalign on apa-${version}-app-release.apk"
echo "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/zipalign -v 4 apa-${version}-app-release-unsigned.apk apa-${version}-app-release.apk"
${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/zipalign -v 4 apa-${version}-app-release-unsigned.apk apa-${version}-app-release.apk > $WORKSPACE/zipalign_build.log 2>&1
if [ $? -ne 0 ]; then
error "Zipalign failed"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment