Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do private methods increase Dex Count in Android?

Tags:

android

dex

I was doing code review and told someone to remove a private method that was only used once. They said that it didn't matter since dex count wouldn't get increased by private method references. Is this true? I wasn't able to find an answer with a simple google search.

like image 934
Tiensi Avatar asked Feb 05 '23 15:02

Tiensi


1 Answers

The 64k limit is a limit on the number of unique method references in a dex file. A method reference consists of a specific class name, the method name and the method prototype, and is created when you either invoke a method or declare/define/override a method.

So yes, defining a new private method will add a method reference to the dex file.


For more information, see: https://source.android.com/devices/tech/dalvik/dex-format.html and https://source.android.com/devices/tech/dalvik/dalvik-bytecode.html, which are the main references for the dex format.

The "method reference list" is a sorted list of method_id_items in the dex file. e.g. look for "method_ids" in the "File layout" section of dex-format.html. And further down the page, method_id_item is defined as consisting of a class reference, a method name and a method prototype.

The class_data_item section is used to define the methods and fields that are defined by the class. The "direct_methods" and "virtual_methods" lists are lists of indexes into the method_ids list - which requires that a reference for that method exists in the method_ids list.

And in dalvik-bytecode.html, the invoke-* instructions use a method index to refer the method to invoke.

Interestingly, the method reference list itself is defined with a 32-bit size value (search for "method_ids_size" in dex-format.html). So the list of method references itself can be as large as 4294967296 entries.

However, the problem comes when you need to reference any of these methods. The invoke-* instructions only use 16 bits to encode the method index.

Additionally, the method references in the class_data item can be up to the full 32 bits. So you could theoretically have definitions of methods past the 64k limit in a dex file, as long as you never actually tried to invoke them from within that dex file. But they could still be invoked from another dex file.

like image 67
JesusFreke Avatar answered Feb 15 '23 11:02

JesusFreke