Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are native C++ methods counted in dex file method count?

I was wondering if the methods written in the native C++ are counted in the Dex file method count for sake of Dex method count limit.

If yes, how many methods is added to the Dex count if 1 native method is added?

And how many methods does a Java method add to the dex count because I don't seem to have a solid number in every build I make...

like image 667
Amir Avatar asked Oct 17 '22 21:10

Amir


1 Answers

To get answers we should walk through .Dex Format. In our case the most interesting part is a method_ids array:

method identifiers list. These are identifiers for all methods referred to by this file, whether defined in the file or not. This list must be sorted, where the defining type (by type_id index) is the major order, method name (by string_id index) is the intermediate order, and method prototype (by proto_id index) is the minor order. The list must not contain any duplicate entries.

Regardless to the fact that number of array records is stored as 32-bit unsigned integer (see method_ids_size field) in practice this array can not contain more than 65536 entries. This is because method_id operand of invoke-xxxx dex instructions is a 16-bit entity and must be a valid index into the method_ids. As result records with indices greater that 65535 would be inaccessible by bytecode. All that results in well-known "64K Methods" problem.

So, as docs say - method_ids has one record per each method defined by that dex as well as for external ones, that are referred by code of defined methods.

Consequently, each time you add code like:

public native void foo();

to one of your classes - you get one extra record in method_ids. This is also true for declarations of an abstract methods. Then, each time you add implementation of some regular method like:

public void baz() {
    /* ... */
}

you get one new record for baz() itself and records for all methods that are referenced by baz() and are not added to method_ids yet.

Native code has no impact to dex content at all, since all C/C++ sources are compiled into machine code, that is distributed via .so files. These ones use ELF format, that has its own limitations, and is absolutely independent from DEX.

like image 50
Sergio Avatar answered Oct 29 '22 21:10

Sergio