What does 'String count' mean in Dex file

2019-02-19 19:15发布

问题:

From adt-21-preview DOC

Building

There's a new flag to force "jumbo mode" for dex which allows a larger number of strings in the dex files; set dex.force.jumbo=true in project.properties

I red this what-are-the-dex-method-and-string-limits-in-an-android-app too, where @SamWise answers:

With jumbo dex on, the string reference limit is somewhere between 110K and 120K.

Question:

What is String or String reference in Dex? Do they mean variable length? For example if I'll write:

private String foo = "fessy";

Doe it mean string is 3 or value length, in my case 5?


Sub-question:

I used following script dex-method-counts to calculate method Count but I didn't find any reference to calculate String count.

Please help,

回答1:

All strings constants are stored in a dex file in a single sorted list. The strings are then referenced elsewhere in the dex file by index within that list.

These string indexes are generally an unsigned 32-bit integer. As far as the actual instructions go, there are 2 instructions that can reference a string: const-string and const-string/jumbo. The first takes a 16-bit string index, and the second takes a 32-bit string index.

So, the maximum number of strings is 2^32 = 4294967296



回答2:

To answer the OP's specific question:

private String foo = "fessy";

This would add two new entries to the string table, assuming no other class uses a variable called foo, and the string "fessy" does not appear anywhere else in your application.

If multiple independent classes use the same name for their variables/string literals, it only adds a single entry to the string table. This is one of the reasons to follow consistent naming conventions (ideally based on Android Framework code). Besides improving readability, it helps keeping the string count low.

You can easily check the string count by first extracting classes.dex from your apk file, and then dumping 4 bytes of the DEX header at offset 56 (see Dalvik Executable format):

$ jar xvf app.apk classes.dex && hexdump -s 56 -n 4 -e '1/4 "%d\n"' classes.dex

The maximum number of strings for a (non-jumbo mode) dex file is 2^16 = 65,536.



标签: java android dex