Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get count of number methods used in a jar file

Tags:

java

android

dex

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 used decompilers like dex2jar JAD and others to name a few ,but they all seem to show methods only in particular class file.

Is there a way i can get a total count ?

like image 276
freeky9 Avatar asked Dec 24 '12 15:12

freeky9


People also ask

How do I find a class file in a bunch of jars?

this is a very simple and useful tool for windows. A simple exe file you click on, give it a directory to search in, a class name and it will find the jar file that contains that class.


2 Answers

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.

like image 186
JesusFreke Avatar answered Sep 22 '22 17:09

JesusFreke


This gradle plugin https://github.com/KeepSafe/dexcount-gradle-plugin will show you the total method count after assembling and also generates a report with the method count of each package. Which after being sorted looks like this:

30145    com 27704    org 20950    android 17140    org.spongycastle 16605    android.support 9760     com.google 8930     com.fasterxml.jackson 8930     com.fasterxml 8633     android.support.v4 7020     com.fasterxml.jackson.databind 6426     android.support.v7 5311     com.google.protobuf 4705     org.spongycastle.crypto ... 

In combination with the gradle command

.\gradlew app:dependencies 

which prints out the dependency tree you will get a good overview of which dependency needs how many methods.

like image 37
Patrick Favre Avatar answered Sep 19 '22 17:09

Patrick Favre