-
Notifications
You must be signed in to change notification settings - Fork 4
/
DexFormat.html
3144 lines (2904 loc) · 89.8 KB
/
DexFormat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Description" content="This document describes the layout and contents of .dex files, which are used to hold a set of class definitions and their associated adjunct data. LEB128 (" L ittle- E ndian B ase 128 ") is a variable-length encoding for arbitrary signed or…">
<title>Dalvik Executable format | Android Open Source Project</title>
<!-- STYLESHEETS -->
<link href="default.css" rel="stylesheet" type="text/css">
</head>
<body class="gc-documentation
" itemscope="" itemtype="http://schema.org/Article">
<a name="top"></a>
<!-- Header -->
<!-- /Header -->
<div id="searchResults" class="wrap" style="display:none;">
<h2 id="searchTitle" style="margin-bottom: 0px;">Results</h2><hr>
<div id="leftSearchControl" class="search-control">Loading...</div>
</div>
<div class="wrap clearfix" id="body-content">
<!-- end side-nav -->
<div class="col-12" id="doc-col">
<h1 itemprop="name">Dalvik Executable format</h1>
<div id="jd-content">
<div class="jd-descr" itemprop="articleBody">
<!--
Copyright 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<p>This document describes the layout and contents of <code>.dex</code>
files, which are used to hold a set of class definitions and their associated
adjunct data.</p>
<h2 id="types" style="margin-bottom: 0px;">Guide to types</h2><hr>
<table class="guide">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>byte</td>
<td>8-bit signed int</td>
</tr>
<tr>
<td>ubyte</td>
<td>8-bit unsigned int</td>
</tr>
<tr>
<td>short</td>
<td>16-bit signed int, little-endian</td>
</tr>
<tr>
<td>ushort</td>
<td>16-bit unsigned int, little-endian</td>
</tr>
<tr>
<td>int</td>
<td>32-bit signed int, little-endian</td>
</tr>
<tr>
<td>uint</td>
<td>32-bit unsigned int, little-endian</td>
</tr>
<tr>
<td>long</td>
<td>64-bit signed int, little-endian</td>
</tr>
<tr>
<td>ulong</td>
<td>64-bit unsigned int, little-endian</td>
</tr>
<tr>
<td>sleb128</td>
<td>signed LEB128, variable-length (see below)</td>
</tr>
<tr>
<td>uleb128</td>
<td>unsigned LEB128, variable-length (see below)</td>
</tr>
<tr>
<td>uleb128p1</td>
<td>unsigned LEB128 plus <code>1</code>, variable-length (see below)</td>
</tr>
</tbody>
</table>
<h3 id="leb128">LEB128</h3>
<p>LEB128 ("<b>L</b>ittle-<b>E</b>ndian <b>B</b>ase <b>128</b>") is a
variable-length encoding for
arbitrary signed or unsigned integer quantities. The format was
borrowed from the <a href="http://dwarfstd.org/Dwarf3Std.php">DWARF3</a>
specification. In a <code>.dex</code> file, LEB128 is only ever used to
encode 32-bit quantities.</p>
<p>Each LEB128 encoded value consists of one to five
bytes, which together represent a single 32-bit value. Each
byte has its most significant bit set except for the final byte in the
sequence, which has its most significant bit clear. The remaining
seven bits of each byte are payload, with the least significant seven
bits of the quantity in the first byte, the next seven in the second
byte and so on. In the case of a signed LEB128 (<code>sleb128</code>),
the most significant payload bit of the final byte in the sequence is
sign-extended to produce the final value. In the unsigned case
(<code>uleb128</code>), any bits not explicitly represented are
interpreted as <code>0</code>.
</p><table class="leb128Bits">
<thead>
<tr><th colspan="16">Bitwise diagram of a two-byte LEB128 value</th></tr>
<tr>
<th colspan="8">First byte
</th><th colspan="8">Second byte
</th></tr>
</thead>
<tbody>
<tr>
<td class="start1"><code>1</code></td>
<td>bit<sub>6</sub></td>
<td>bit<sub>5</sub></td>
<td>bit<sub>4</sub></td>
<td>bit<sub>3</sub></td>
<td>bit<sub>2</sub></td>
<td>bit<sub>1</sub></td>
<td>bit<sub>0</sub></td>
<td class="start2"><code>0</code></td>
<td>bit<sub>13</sub></td>
<td>bit<sub>12</sub></td>
<td>bit<sub>11</sub></td>
<td>bit<sub>10</sub></td>
<td>bit<sub>9</sub></td>
<td>bit<sub>8</sub></td>
<td class="end2">bit<sub>7</sub></td>
</tr>
</tbody>
</table>
<p>The variant <code>uleb128p1</code> is used to represent a signed
value, where the representation is of the value <i>plus one</i> encoded
as a <code>uleb128</code>. This makes the encoding of <code>-1</code>
(alternatively thought of as the unsigned value <code>0xffffffff</code>)
— but no other negative number — a single byte, and is
useful in exactly those cases where the represented number must either
be non-negative or <code>-1</code> (or <code>0xffffffff</code>),
and where no other negative values are allowed (or where large unsigned
values are unlikely to be needed).</p>
<p>Here are some examples of the formats:</p>
<table class="leb128">
<thead>
<tr>
<th>Encoded Sequence</th>
<th>As <code>sleb128</code></th>
<th>As <code>uleb128</code></th>
<th>As <code>uleb128p1</code></th>
</tr>
</thead>
<tbody>
<tr><td>00</td><td>0</td><td>0</td><td>-1</td></tr>
<tr><td>01</td><td>1</td><td>1</td><td>0</td></tr>
<tr><td>7f</td><td>-1</td><td>127</td><td>126</td></tr>
<tr><td>80 7f</td><td>-128</td><td>16256</td><td>16255</td></tr>
</tbody>
</table>
<h2 id="file-layout" style="margin-bottom: 0px;">File layout</h2><hr>
<table class="format">
<thead>
<tr>
<th>Name</th>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>header</td>
<td>header_item</td>
<td>the header</td>
</tr>
<tr>
<td>string_ids</td>
<td>string_id_item[]</td>
<td>string identifiers list. These are identifiers for all the strings
used by this file, either for internal naming (e.g., type descriptors)
or as constant objects referred to by code. This list must be sorted
by string contents, using UTF-16 code point values (not in a
locale-sensitive manner), and it must not contain any duplicate entries.
</td>
</tr>
<tr>
<td>type_ids</td>
<td>type_id_item[]</td>
<td>type identifiers list. These are identifiers for all types (classes,
arrays, or primitive types) referred to by this file, whether defined
in the file or not. This list must be sorted by <code>string_id</code>
index, and it must not contain any duplicate entries.
</td>
</tr>
<tr>
<td>proto_ids</td>
<td>proto_id_item[]</td>
<td>method prototype identifiers list. These are identifiers for all
prototypes referred to by this file. This list must be sorted in
return-type (by <code>type_id</code> index) major order, and then
by argument list (lexicographic ordering, individual arguments
ordered by <code>type_id</code> index). The list must not
contain any duplicate entries.
</td>
</tr>
<tr>
<td>field_ids</td>
<td>field_id_item[]</td>
<td>field identifiers list. These are identifiers for all fields
referred to by this file, whether defined in the file or not. This
list must be sorted, where the defining type (by <code>type_id</code>
index) is the major order, field name (by <code>string_id</code> index)
is the intermediate order, and type (by <code>type_id</code> index)
is the minor order. The list must not contain any duplicate entries.
</td>
</tr>
<tr>
<td>method_ids</td>
<td>method_id_item[]</td>
<td>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 <code>type_id</code>
index) is the major order, method name (by <code>string_id</code>
index) is the intermediate order, and method prototype (by
<code>proto_id</code> index) is the minor order. The list must not
contain any duplicate entries.
</td>
</tr>
<tr>
<td>class_defs</td>
<td>class_def_item[]</td>
<td>class definitions list. The classes must be ordered such that a given
class's superclass and implemented interfaces appear in the
list earlier than the referring class. Furthermore, it is invalid for
a definition for the same-named class to appear more than once in
the list.
</td>
</tr>
<tr>
<td>data</td>
<td>ubyte[]</td>
<td>data area, containing all the support data for the tables listed above.
Different items have different alignment requirements, and
padding bytes are inserted before each item if necessary to achieve
proper alignment.
</td>
</tr>
<tr>
<td>link_data</td>
<td>ubyte[]</td>
<td>data used in statically linked files. The format of the data in
this section is left unspecified by this document.
This section is empty in unlinked files, and runtime implementations
may use it as they see fit.
</td>
</tr>
</tbody>
</table>
<h2 id="definitions" style="margin-bottom: 0px;">Bitfield, string and constant definitions</h2><hr>
<h3 id="dex-file-magic">DEX_FILE_MAGIC</h3>
<h4>embedded in header_item</h4>
<p>The constant array/string <code>DEX_FILE_MAGIC</code> is the list of
bytes that must appear at the beginning of a <code>.dex</code> file
in order for it to be recognized as such. The value intentionally
contains a newline (<code>"\n"</code> or <code>0x0a</code>) and a
null byte (<code>"\0"</code> or <code>0x00</code>) in order to help
in the detection of certain forms of corruption. The value also
encodes a format version number as three decimal digits, which is
expected to increase monotonically over time as the format evolves.</p>
<pre class="prettyprint"><span class="pln">ubyte</span><span class="pun">[</span><span class="lit">8</span><span class="pun">]</span><span class="pln"> DEX_FILE_MAGIC </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="lit">0x64</span><span class="pln"> </span><span class="lit">0x65</span><span class="pln"> </span><span class="lit">0x78</span><span class="pln"> </span><span class="lit">0x0a</span><span class="pln"> </span><span class="lit">0x30</span><span class="pln"> </span><span class="lit">0x33</span><span class="pln"> </span><span class="lit">0x37</span><span class="pln"> </span><span class="lit">0x00</span><span class="pln"> </span><span class="pun">}</span><span class="pln"><br> </span><span class="pun">=</span><span class="pln"> </span><span class="str">"dex\n037\0"</span></pre>
<p class="note"><strong>Note:</strong> At least a couple earlier versions of the format have
been used in widely-available public software releases. For example,
version <code>009</code> was used for the M3 releases of the
Android platform (November–December 2007),
and version <code>013</code> was used for the M5 releases of the Android
platform (February–March 2008). In several respects, these earlier
versions of the format differ significantly from the version described in this
document.</p>
<p class="note"><strong>Note:</strong> Support for version <code>037</code> of
the format was added in the Android 7.0 release. Prior to this release most
versions of Android have used version <code>035</code> of the format. The only
difference between versions <code>035</code> and <code>037</code> is the
addition of default methods and the adjustment of the <code>invoke</code>
instruction semantics to support this feature. Due to a Dalvik bug present in
older versions of Android, Dex version <code>036</code> has been skipped.
Dex version <code>036</code> is not valid for any version of Android and never
will be.</p>
<h3 id="endian-constant">ENDIAN_CONSTANT and REVERSE_ENDIAN_CONSTANT</h3>
<h4>embedded in header_item</h4>
<p>The constant <code>ENDIAN_CONSTANT</code> is used to indicate the
endianness of the file in which it is found. Although the standard
<code>.dex</code> format is little-endian, implementations may choose
to perform byte-swapping. Should an implementation come across a
header whose <code>endian_tag</code> is <code>REVERSE_ENDIAN_CONSTANT</code>
instead of <code>ENDIAN_CONSTANT</code>, it would know that the file
has been byte-swapped from the expected form.</p>
<pre class="prettyprint"><span class="kwd">uint</span><span class="pln"> ENDIAN_CONSTANT </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0x12345678</span><span class="pun">;</span><span class="pln"><br></span><span class="kwd">uint</span><span class="pln"> REVERSE_ENDIAN_CONSTANT </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0x78563412</span><span class="pun">;</span></pre>
<h3 id="no-index">NO_INDEX</h3>
<h4>embedded in class_def_item and debug_info_item</h4>
<p>The constant <code>NO_INDEX</code> is used to indicate that
an index value is absent.</p>
<p class="note"><strong>Note:</strong> This value isn't defined to be
<code>0</code>, because that is in fact typically a valid index.</p>
<p>The chosen value for <code>NO_INDEX</code> is
representable as a single byte in the <code>uleb128p1</code> encoding.</p>
<pre class="prettyprint"><span class="kwd">uint</span><span class="pln"> NO_INDEX </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0xffffffff</span><span class="pun">;</span><span class="pln"> </span><span class="com">// == -1 if treated as a signed int</span></pre>
<h3 id="access-flags">access_flags definitions</h3>
<h4>embedded in class_def_item, encoded_field, encoded_method, and
InnerClass</h4>
<p>Bitfields of these flags are used to indicate the accessibility and
overall properties of classes and class members.</p>
<table class="accessFlags">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>For Classes (and <code>InnerClass</code> annotations)</th>
<th>For Fields</th>
<th>For Methods</th>
</tr>
</thead>
<tbody>
<tr>
<td>ACC_PUBLIC</td>
<td>0x1</td>
<td><code>public</code>: visible everywhere</td>
<td><code>public</code>: visible everywhere</td>
<td><code>public</code>: visible everywhere</td>
</tr>
<tr>
<td>ACC_PRIVATE</td>
<td>0x2</td>
<td><super>*</super>
<code>private</code>: only visible to defining class
</td>
<td><code>private</code>: only visible to defining class</td>
<td><code>private</code>: only visible to defining class</td>
</tr>
<tr>
<td>ACC_PROTECTED</td>
<td>0x4</td>
<td><super>*</super>
<code>protected</code>: visible to package and subclasses
</td>
<td><code>protected</code>: visible to package and subclasses</td>
<td><code>protected</code>: visible to package and subclasses</td>
</tr>
<tr>
<td>ACC_STATIC</td>
<td>0x8</td>
<td><super>*</super>
<code>static</code>: is not constructed with an outer
<code>this</code> reference</td>
<td><code>static</code>: global to defining class</td>
<td><code>static</code>: does not take a <code>this</code> argument</td>
</tr>
<tr>
<td>ACC_FINAL</td>
<td>0x10</td>
<td><code>final</code>: not subclassable</td>
<td><code>final</code>: immutable after construction</td>
<td><code>final</code>: not overridable</td>
</tr>
<tr>
<td>ACC_SYNCHRONIZED</td>
<td>0x20</td>
<td> </td>
<td> </td>
<td><code>synchronized</code>: associated lock automatically acquired
around call to this method. <p class="note"><strong>Note:</strong> This is only valid to set when
<code>ACC_NATIVE</code> is also set. </p></td>
</tr>
<tr>
<td>ACC_VOLATILE</td>
<td>0x40</td>
<td> </td>
<td><code>volatile</code>: special access rules to help with thread
safety</td>
<td> </td>
</tr>
<tr>
<td>ACC_BRIDGE</td>
<td>0x40</td>
<td> </td>
<td> </td>
<td>bridge method, added automatically by compiler as a type-safe
bridge</td>
</tr>
<tr>
<td>ACC_TRANSIENT</td>
<td>0x80</td>
<td> </td>
<td><code>transient</code>: not to be saved by default serialization</td>
<td> </td>
</tr>
<tr>
<td>ACC_VARARGS</td>
<td>0x80</td>
<td> </td>
<td> </td>
<td>last argument should be treated as a "rest" argument by compiler</td>
</tr>
<tr>
<td>ACC_NATIVE</td>
<td>0x100</td>
<td> </td>
<td> </td>
<td><code>native</code>: implemented in native code</td>
</tr>
<tr>
<td>ACC_INTERFACE</td>
<td>0x200</td>
<td><code>interface</code>: multiply-implementable abstract class</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ACC_ABSTRACT</td>
<td>0x400</td>
<td><code>abstract</code>: not directly instantiable</td>
<td> </td>
<td><code>abstract</code>: unimplemented by this class</td>
</tr>
<tr>
<td>ACC_STRICT</td>
<td>0x800</td>
<td> </td>
<td> </td>
<td><code>strictfp</code>: strict rules for floating-point arithmetic</td>
</tr>
<tr>
<td>ACC_SYNTHETIC</td>
<td>0x1000</td>
<td>not directly defined in source code</td>
<td>not directly defined in source code</td>
<td>not directly defined in source code</td>
</tr>
<tr>
<td>ACC_ANNOTATION</td>
<td>0x2000</td>
<td>declared as an annotation class</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ACC_ENUM</td>
<td>0x4000</td>
<td>declared as an enumerated type</td>
<td>declared as an enumerated value</td>
<td> </td>
</tr>
<tr>
<td><i>(unused)</i></td>
<td>0x8000</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>ACC_CONSTRUCTOR</td>
<td>0x10000</td>
<td> </td>
<td> </td>
<td>constructor method (class or instance initializer)</td>
</tr>
<tr>
<td>ACC_DECLARED_<br>SYNCHRONIZED</td>
<td>0x20000</td>
<td> </td>
<td> </td>
<td>declared <code>synchronized</code>. <p class="note"><strong>Note:</strong> This has no effect on
execution (other than in reflection of this flag, per se).</p>
</td>
</tr>
</tbody>
</table>
<p><super>*</super> Only allowed on for <code>InnerClass</code> annotations,
and must not ever be on in a <code>class_def_item</code>.</p>
<h3 id="mutf-8">MUTF-8 (Modified UTF-8) Encoding</h3>
<p>As a concession to easier legacy support, the <code>.dex</code> format
encodes its string data in a de facto standard modified UTF-8 form, hereafter
referred to as MUTF-8. This form is identical to standard UTF-8, except:</p>
<ul>
<li>Only the one-, two-, and three-byte encodings are used.</li>
<li>Code points in the range <code>U+10000</code> …
<code>U+10ffff</code> are encoded as a surrogate pair, each of
which is represented as a three-byte encoded value.</li>
<li>The code point <code>U+0000</code> is encoded in two-byte form.</li>
<li>A plain null byte (value <code>0</code>) indicates the end of
a string, as is the standard C language interpretation.</li>
</ul>
<p>The first two items above can be summarized as: MUTF-8
is an encoding format for UTF-16, instead of being a more direct
encoding format for Unicode characters.</p>
<p>The final two items above make it simultaneously possible to include
the code point <code>U+0000</code> in a string <i>and</i> still manipulate
it as a C-style null-terminated string.</p>
<p>However, the special encoding of <code>U+0000</code> means that, unlike
normal UTF-8, the result of calling the standard C function
<code>strcmp()</code> on a pair of MUTF-8 strings does not always
indicate the properly signed result of comparison of <i>unequal</i> strings.
When ordering (not just equality) is a concern, the most straightforward
way to compare MUTF-8 strings is to decode them character by character,
and compare the decoded values. (However, more clever implementations are
also possible.)</p>
<p>Please refer to <a href="http://unicode.org">The Unicode
Standard</a> for further information about character encoding.
MUTF-8 is actually closer to the (relatively less well-known) encoding
<a href="http://www.unicode.org/reports/tr26/">CESU-8</a> than to UTF-8
per se.</p>
<h3 id="encoding">encoded_value encoding</h3>
<h4>embedded in annotation_element and encoded_array_item </h4>
<p>An <code>encoded_value</code> is an encoded piece of (nearly)
arbitrary hierarchically structured data. The encoding is meant to
be both compact and straightforward to parse.</p>
<table class="format">
<thead>
<tr>
<th>Name</th>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>(value_arg << 5) | value_type</td>
<td>ubyte</td>
<td>byte indicating the type of the immediately subsequent
<code>value</code> along
with an optional clarifying argument in the high-order three bits.
See below for the various <code>value</code> definitions.
In most cases, <code>value_arg</code> encodes the length of
the immediately-subsequent <code>value</code> in bytes, as
<code>(size - 1)</code>, e.g., <code>0</code> means that
the value requires one byte, and <code>7</code> means it requires
eight bytes; however, there are exceptions as noted below.
</td>
</tr>
<tr>
<td>value</td>
<td>ubyte[]</td>
<td>bytes representing the value, variable in length and interpreted
differently for different <code>value_type</code> bytes, though
always little-endian. See the various value definitions below for
details.
</td>
</tr>
</tbody>
</table>
<h3 id="value-formats">Value formats</h3>
<table class="encodedValue">
<thead>
<tr>
<th>Type Name</th>
<th><code>value_type</code></th>
<th><code>value_arg</code> Format</th>
<th><code>value</code> Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>VALUE_BYTE</td>
<td>0x00</td>
<td><i>(none; must be <code>0</code>)</i></td>
<td>ubyte[1]</td>
<td>signed one-byte integer value</td>
</tr>
<tr>
<td>VALUE_SHORT</td>
<td>0x02</td>
<td>size - 1 (0…1)</td>
<td>ubyte[size]</td>
<td>signed two-byte integer value, sign-extended</td>
</tr>
<tr>
<td>VALUE_CHAR</td>
<td>0x03</td>
<td>size - 1 (0…1)</td>
<td>ubyte[size]</td>
<td>unsigned two-byte integer value, zero-extended</td>
</tr>
<tr>
<td>VALUE_INT</td>
<td>0x04</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>signed four-byte integer value, sign-extended</td>
</tr>
<tr>
<td>VALUE_LONG</td>
<td>0x06</td>
<td>size - 1 (0…7)</td>
<td>ubyte[size]</td>
<td>signed eight-byte integer value, sign-extended</td>
</tr>
<tr>
<td>VALUE_FLOAT</td>
<td>0x10</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>four-byte bit pattern, zero-extended <i>to the right</i>, and
interpreted as an IEEE754 32-bit floating point value
</td>
</tr>
<tr>
<td>VALUE_DOUBLE</td>
<td>0x11</td>
<td>size - 1 (0…7)</td>
<td>ubyte[size]</td>
<td>eight-byte bit pattern, zero-extended <i>to the right</i>, and
interpreted as an IEEE754 64-bit floating point value
</td>
</tr>
<tr>
<td>VALUE_STRING</td>
<td>0x17</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>unsigned (zero-extended) four-byte integer value,
interpreted as an index into
the <code>string_ids</code> section and representing a string value
</td>
</tr>
<tr>
<td>VALUE_TYPE</td>
<td>0x18</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>unsigned (zero-extended) four-byte integer value,
interpreted as an index into
the <code>type_ids</code> section and representing a reflective
type/class value
</td>
</tr>
<tr>
<td>VALUE_FIELD</td>
<td>0x19</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>unsigned (zero-extended) four-byte integer value,
interpreted as an index into
the <code>field_ids</code> section and representing a reflective
field value
</td>
</tr>
<tr>
<td>VALUE_METHOD</td>
<td>0x1a</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>unsigned (zero-extended) four-byte integer value,
interpreted as an index into
the <code>method_ids</code> section and representing a reflective
method value
</td>
</tr>
<tr>
<td>VALUE_ENUM</td>
<td>0x1b</td>
<td>size - 1 (0…3)</td>
<td>ubyte[size]</td>
<td>unsigned (zero-extended) four-byte integer value,
interpreted as an index into
the <code>field_ids</code> section and representing the value of
an enumerated type constant
</td>
</tr>
<tr>
<td>VALUE_ARRAY</td>
<td>0x1c</td>
<td><i>(none; must be <code>0</code>)</i></td>
<td>encoded_array</td>
<td>an array of values, in the format specified by
"<code>encoded_array</code> format" below. The size
of the <code>value</code> is implicit in the encoding.
</td>
</tr>
<tr>
<td>VALUE_ANNOTATION</td>
<td>0x1d</td>
<td><i>(none; must be <code>0</code>)</i></td>
<td>encoded_annotation</td>
<td>a sub-annotation, in the format specified by
"<code>encoded_annotation</code> format" below. The size
of the <code>value</code> is implicit in the encoding.
</td>
</tr>
<tr>
<td>VALUE_NULL</td>
<td>0x1e</td>
<td><i>(none; must be <code>0</code>)</i></td>
<td><i>(none)</i></td>
<td><code>null</code> reference value</td>
</tr>
<tr>
<td>VALUE_BOOLEAN</td>
<td>0x1f</td>
<td>boolean (0…1)</td>
<td><i>(none)</i></td>
<td>one-bit value; <code>0</code> for <code>false</code> and
<code>1</code> for <code>true</code>. The bit is represented in the
<code>value_arg</code>.
</td>
</tr>
</tbody>
</table>
<h3 id="encoded-array">encoded_array format</h3>
<table class="format">
<thead>
<tr>
<th>Name</th>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>size</td>
<td>uleb128</td>
<td>number of elements in the array</td>
</tr>
<tr>
<td>values</td>
<td>encoded_value[size]</td>
<td>a series of <code>size</code> <code>encoded_value</code> byte
sequences in the format specified by this section, concatenated
sequentially.
</td>
</tr>
</tbody>
</table>
<h3 id="encoded-annotation">encoded_annotation format</h3>
<table class="format">
<thead>
<tr>
<th>Name</th>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>type_idx</td>
<td>uleb128</td>
<td>type of the annotation. This must be a class (not array or primitive)
type.
</td>
</tr>
<tr>
<td>size</td>
<td>uleb128</td>
<td>number of name-value mappings in this annotation</td>
</tr>
<tr>
<td>elements</td>
<td>annotation_element[size]</td>
<td>elements of the annotation, represented directly in-line (not as
offsets). Elements must be sorted in increasing order by
<code>string_id</code> index.
</td>
</tr>
</tbody>
</table>
<h3 id="annotation-element">annotation_element format</h3>
<table class="format">
<thead>
<tr>
<th>Name</th>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>name_idx</td>
<td>uleb128</td>
<td>element name, represented as an index into the
<code>string_ids</code> section. The string must conform to the
syntax for <i>MemberName</i>, defined above.
</td>
</tr>
<tr>
<td>value</td>
<td>encoded_value</td>
<td>element value</td>
</tr>
</tbody>
</table>
<h2 id="string-syntax" style="margin-bottom: 0px;">String syntax</h2><hr>
<p>There are several kinds of item in a <code>.dex</code> file which
ultimately refer to a string. The following BNF-style definitions
indicate the acceptable syntax for these strings.</p>
<h3 id="simplename"><i>SimpleName</i></h3>
<p>A <i>SimpleName</i> is the basis for the syntax of the names of other
things. The <code>.dex</code> format allows a fair amount of latitude
here (much more than most common source languages). In brief, a simple
name consists of any low-ASCII alphabetic character or digit, a few
specific low-ASCII symbols, and most non-ASCII code points that are not
control, space, or special characters. Note that surrogate code points
(in the range <code>U+d800</code> … <code>U+dfff</code>) are not
considered valid name characters, per se, but Unicode supplemental
characters <i>are</i> valid (which are represented by the final
alternative of the rule for <i>SimpleNameChar</i>), and they should be
represented in a file as pairs of surrogate code points in the MUTF-8
encoding.</p>
<table class="bnf">
<tbody><tr><td colspan="2" class="def"><i>SimpleName</i> →</td></tr>
<tr>
<td>
</td><td><i>SimpleNameChar</i> (<i>SimpleNameChar</i>)*</td>
</tr>
<tr><td colspan="2" class="def"><i>SimpleNameChar</i> →</td></tr>
<tr>
<td>
</td><td><code>'A'</code> … <code>'Z'</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>'a'</code> … <code>'z'</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>'0'</code> … <code>'9'</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>'$'</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>'-'</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>'_'</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>U+00a1</code> … <code>U+1fff</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>U+2010</code> … <code>U+2027</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>U+2030</code> … <code>U+d7ff</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>U+e000</code> … <code>U+ffef</code></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>U+10000</code> … <code>U+10ffff</code></td>
</tr>
</tbody></table>
<h3 id="membername"><i>MemberName</i></h3>
<h4>used by field_id_item and method_id_item</h4>
<p>A <i>MemberName</i> is the name of a member of a class, members being
fields, methods, and inner classes.</p>
<table class="bnf">
<tbody><tr><td colspan="2" class="def"><i>MemberName</i> →</td></tr>
<tr>
<td>
</td><td><i>SimpleName</i></td>
</tr>
<tr>
<td class="bar">|</td>
<td><code>'<'</code> <i>SimpleName</i> <code>'>'</code></td>
</tr>
</tbody></table>
<h3 id="fullclassname"><i>FullClassName</i></h3>
<p>A <i>FullClassName</i> is a fully-qualified class name, including an
optional package specifier followed by a required name.</p>
<table class="bnf">
<tbody><tr><td colspan="2" class="def"><i>FullClassName</i> →</td></tr>
<tr>
<td>
</td><td><i>OptionalPackagePrefix</i> <i>SimpleName</i></td>
</tr>
<tr><td colspan="2" class="def"><i>OptionalPackagePrefix</i> →</td></tr>
<tr>
<td>
</td><td>(<i>SimpleName</i> <code>'/'</code>)*</td>
</tr>
</tbody></table>
<h3 id="typedescriptor"><i>TypeDescriptor</i></h3>
<h4>used by type_id_item</h4>
<p>A <i>TypeDescriptor</i> is the representation of any type, including
primitives, classes, arrays, and <code>void</code>. See below for
the meaning of the various versions.</p>
<table class="bnf">
<tbody><tr><td colspan="2" class="def"><i>TypeDescriptor</i> →</td></tr>
<tr>