Skip to content

Instantly share code, notes, and snippets.

@JakeWharton
Last active March 25, 2024 13:54
Show Gist options
  • Save JakeWharton/6002797 to your computer and use it in GitHub Desktop.
Save JakeWharton/6002797 to your computer and use it in GitHub Desktop.
`classes.dex` method count helpers. Requires smali/baksmali from https://code.google.com/p/smali/ and dexdump from the build-tools in the Android SDK be on your PATH.
function dex-method-count() {
cat $1 | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d\n"'
}
function dex-method-count-by-package() {
dir=$(mktemp -d -t dex)
baksmali $1 -o $dir
for pkg in `find $dir/* -type d`; do
smali $pkg -o $pkg/classes.dex
count=$(dex-method-count $pkg/classes.dex)
name=$(echo ${pkg:(${#dir} + 1)} | tr '/' '.')
echo -e "$count\t$name"
done
rm -rf $dir
}
@zhywang
Copy link

zhywang commented Mar 19, 2015

function dex-method-count-by-package() {
dexdump -f classes.dex | grep \(in | awk '{print $4}'|awk -F/ '{print $1"."$2"."$3;}' | sort | uniq -c | sort
}

It's not very accurate but it's fast and simple :)

@ajaykumarns
Copy link

dex-method-count seems to return incorrect number of methods for me. I used the following script to count the number of methods in my jar file:

dex_dir=$(mktemp -d -t dex)
dex_file="$dex_dir/dexed.jar"
dx --dex --output $dex_file $1
baksmali $dex_file -o $dex_dir
count=$(find $dex_dir -type f -exec grep "\.method" '{}' \; | wc -l | tr -d ' ')
echo "There are $count methods in $1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment