Skip to content

Instantly share code, notes, and snippets.

@arekolek
Last active June 27, 2020 14:28
Show Gist options
  • Save arekolek/3cebafa690638eb864ba165ac54f2f01 to your computer and use it in GitHub Desktop.
Save arekolek/3cebafa690638eb864ba165ac54f2f01 to your computer and use it in GitHub Desktop.
Decompile APK
#!/usr/local/bin/zsh
if [[ -z $1 ]]; then
echo "usage: ./decompile.sh path_to_apk"
exit 0
elif ! [[ -f $1 ]]; then
echo "$1: No such file or directory"
exit 1
fi
if ! [ -x "$(command -v apktool)" ]; then
if ! [ -x "$(command -v brew)" ]; then
echo 'You have to install apktool manually.'
echo 'See: https://ibotpeaches.github.io/Apktool/install/'
exit 1
fi
echo 'Trying to install apktool for you.'
brew install apktool
fi
D2J_URL=https://github.com/pxb1988/dex2jar/files/1867564/dex-tools-2.1-SNAPSHOT.zip
D2J_DIR=dex-tools-2.1-SNAPSHOT
if ! [[ -d $D2J_DIR ]]; then
if ! [ -x "$(command -v curl)" ]; then
echo 'You have to install dex2jar manually.'
echo 'See: https://github.com/pxb1988/dex2jar/releases/'
exit 1
fi
echo 'Trying to download dex2jar for you.'
curl -SL $D2J_URL | tar -xf - -C .
chmod u+x $D2J_DIR/d2j*.sh
fi
JDGUI_URL=https://github.com/java-decompiler/jd-gui/releases/download/v1.6.6/jd-gui-osx-1.6.6.tar
if ! open -Ra "JD-GUI" ; then
if ! [ -x "$(command -v curl)" ]; then
echo "Install JD-GUI from http://java-decompiler.github.io/#jd-gui-download and launch it at least once."
exit 1
fi
echo 'Trying to download JD-GUI for you.'
curl -SL $JDGUI_URL | tar -xf - -C .
exit 1
fi
# outputs `app.apk`
echo "Copying APK to working directory..."
cp $1 app.apk
# outputs `app` directory
echo "Decoding with apktool..."
apktool decode -f -s app.apk
# clean `app.apk` output
echo "Cleaning up APK file..."
rm app.apk
# outputs `$D2J_DIR/classes.dex`
echo "Copying DEX file to working directory..."
cp app/classes.dex $D2J_DIR
# clean `app` directory output
echo "Cleaning up apktool output..."
rm -r app
cd $D2J_DIR
# outputs `$D2J_DIR/classes-dex2jar.jar`
echo "Converting DEX to JAR..."
./d2j-dex2jar.sh classes.dex
# clean `classes.dex` output
echo "Cleaning up DEX file..."
rm classes.dex
# clean `$D2J_DIR/classes-dex2jar.jar` output
# outputs jar with name taken from input apk
mv classes-dex2jar.jar ../$1:t:r.jar
echo "Output $(realpath ../$1:t:r.jar)"
echo "Opening JD-GUI..."
open -a JD-GUI ../$1:t:r.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment