Instantly share code, notes, and snippets.

Embed
What would you like to do?
`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
}
@reilost

This comment has been minimized.

reilost commented Sep 18, 2013

this sh file is used to count how many method in the dex file or split dex file to more than one dex file ?

@ligi

This comment has been minimized.

@aried3r

This comment has been minimized.

aried3r commented Jul 9, 2014

Before I roll my own, do you use any kind of script to always point to the latest build tools directory in your PATH?

@VinyWenkz

This comment has been minimized.

VinyWenkz commented Jul 25, 2014

Hi, I'm a new developer.
How do I use this? I already have baksmali results, but when i call sh dex.sh, here is the error: dex.sh: line 3: `dex-method-count': not a valid identifier
Thanks

@sschuberth

This comment has been minimized.

sschuberth commented Jul 28, 2014

@aried3r A version that always uses the latest build tools (and optional CSV output for use with the Jenkins Plot plugin) is here.

@mrsasha

This comment has been minimized.

mrsasha commented Sep 14, 2014

If someone is looking for the windows solution (like I was), I wrote a simple batch file here: https://gist.github.com/mrsasha/9f24e129ced1b1db791b

@jaredrummler

This comment has been minimized.

jaredrummler commented Nov 11, 2014

Don't use cat to feed a single file's content to a filter. cat is a utility used to concatenate the contents of several files together.

This would be better:

head -c 92 $1 | tail -c 4 | hexdump -e '1/4 "%d\n"'

:P


If you just want the dex count for one apk (not by package) you could do:

apk=/path/to/the.apk
unzip -p $apk classes.dex | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d\n"'
@zhywang

This comment has been minimized.

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

This comment has been minimized.

ajaykumarns commented Nov 4, 2015

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