Can i have the count of all methods used in a jar file . My APK uses certain external JARS and there are a number of classes around hundred to be precise.
I have use
I wrote a script based on @JesusFreke's answer for this problem and for another problem (https://stackoverflow.com/a/21485222/6643139):
#!/bin/bash
unzip $1 classes.jar -d aarsize &&
dx --dex --output=aarsize/temp.dex aarsize/classes.jar &&
hexdump -s 88 -n 4 -e '1/4 "%d\n"' aarsize/temp.dex &&
rm -rf aarsize
Save it to a file name "aarsize", chmod +x
for it, output of aarsize your_aar_file
:
$ aarsize status-bar-compat-release.aar
Archive: status-bar-compat-release.aar
inflating: aarsize/classes.jar
91
The 91
is the method counts of your aar file.
You can convert the jar to a dex file, and then pull the number of method references out of the header. It is stored as an unsigned little endian integer, at offset 88 (0x58).
dx --dex --output=temp.dex orig.jar
cat temp.dex | head -c 92 | tail -c 4 | hexdump -e '1/4 "%d\n"'
Keep in mind that this is the number of unique methods referenced, not the number of method references. In other words, if a particular method is referenced twice in the dex file, it will only be counted once in the count in the header. And when you import this jar into your apk, the method references that are common between the two are deduplicated, so the total method reference count of the final merged apk will be <= the sum of the two.
Use the http://github.com/mihaip/dex-method-counts for calculationg number of methods from everywhere (JAR, AAR, DEX, APK)
./dex-method-counts <your_file_path>.DEX
or
./dex-method-counts <your_file_path>.APK
just make a DEX from JAR as it was shown above like this:
<your_path_to_android_buil_tools>/dx --dex --output=temp.dex orig.jar
and then
./dex-method-counts temp.dex
Firstly unzip it (yeah AAR it is ZIP actually) and then use classes.jar as it shown above in JAR section
unzip mylib.aar -d mylib
dx --dex --output=temp.dex mylib/classes.jar
dex-method-counts temp.dex
To find methods count in gradle library, you can use this - http://www.methodscount.com
It also provides plugin for android studio.
http://www.methodscount.com/plugins
I just wrote a python script for this to get a rough estimate
import re
import subprocess
import sys
for jarfile in sys.argv[1:]:
class_output = subprocess.check_output(['jar', 'tf', jarfile])
classes = [c.replace('/', '.') for c in re.findall(r'(.*)\.class', class_output)]
methods = []
if classes:
def_out = subprocess.check_output(['javap', '-classpath', jarfile] + classes)
# This is pretty hacky: look for parentheses in the declaration line.
num_methods = sum(1 for line in def_out if '(' in line)
else:
num_methods = 0
print '{} {} {}'.format(num_methods, len(classes), jarfile)
I had just run into the Android 64K method limit, and my dependencies weren't indexed by the methodcount.com service yet.
Use the jar program with the -x parameter to extract the files from your jar file.
Apply a decompiler to each .class file to get the number of methods in each file.
Add up the method counts.