Reputation: 336
I was wondering if it is possible to count the number of times an API is called by parsing the classes.dex file. I know ways to get all the API's called but wanted to know if there is a way to get the number of times an api was called without running the application, just by parsing the classes.dex
. Is this information stored in dex file ?
I had quick look at dex file format but wasn't able to find that information.
Ex : if substring()
is used once in class1 and class2 and class3 , I should be able to get information that substring has been called 3 times.
Upvotes: 0
Views: 768
Reputation: 1214
There's no way to tell how many times an API call is made by just parsing the code. For example, how many times is foo()
called?:
public static void bar(int x) {
for ( int i = 0; i < x; i++ ) {
foo();
}
}
If you just look at the source code, the answer is once but if you analyze the semantics, the answer is x
times.
If you only want to know the number of times an API call exists in the source code, you can get that easily.
Use baksmali on the dex file to get Smali code.
Grep (or ack) the Smali for the API call you want. Example regex for SmsManager.sendTextMessage() is:
"invoke[^,]+, Landroid/telephony/SmsManager;->sendTextMessage\(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/app/PendingIntent;Landroid/app/PendingIntent;\)V"
Count the number of times you find that line. Something like:
grep api_call_pattern . | wc -l
This can only be considered a rough proxy for actual API call counts. If you want something more accurate, you'll have to do symbolic analysis. Unfortunately, there isn't a turn-key solution for this that you can use that I'm aware of.
Upvotes: 2