Google cam kết thúc đẩy công bằng chủng tộc cho Cộng đồng người da đen. Xem cách thực hiện.

Định dạng thực thi Dalvik

Tài liệu này mô tả bố cục và nội dung của tệp .dex , được sử dụng để chứa một tập hợp các định nghĩa lớp và dữ liệu bổ trợ liên quan của chúng.

Hướng dẫn các loại

Tên Sự mô tả
byte Int có ký 8-bit
ubyte Int 8-bit không dấu
ngắn 16-bit có dấu int, little-endian
ushort 16-bit không dấu int, little-endian
int 32-bit có dấu int, little-endian
uint 32-bit unsigned int, little-endian
Dài 64-bit có dấu int, little-endian
dài 64-bit không dấu int, little-endian
slb128 ký hiệu LEB128, độ dài thay đổi (xem bên dưới)
uleb128 LEB128 không dấu, chiều dài thay đổi (xem bên dưới)
uleb128p1 LEB128 không dấu cộng với 1 , chiều dài thay đổi (xem bên dưới)

LEB128

LEB128 (" L ittle- E ndian B ase 128 ") là một mã hóa có độ dài thay đổi cho các đại lượng số nguyên có dấu hoặc không dấu tùy ý. Định dạng được mượn từ đặc tả DWARF3 . Trong tệp .dex , LEB128 chỉ được sử dụng để mã hóa số lượng 32 bit.

Mỗi giá trị được mã hóa LEB128 bao gồm một đến năm byte, cùng nhau đại diện cho một giá trị 32 bit duy nhất. Mỗi byte có bộ bit quan trọng nhất của nó ngoại trừ byte cuối cùng trong chuỗi, có bit quan trọng nhất của nó rõ ràng. Bảy bit còn lại của mỗi byte là trọng tải, với số lượng ít nhất là bảy bit trong byte đầu tiên, bảy bit tiếp theo trong byte thứ hai, v.v. Trong trường hợp LEB128 có dấu ( sleb128 ), bit trọng tải quan trọng nhất của byte cuối cùng trong chuỗi được mở rộng dấu để tạo ra giá trị cuối cùng. Trong trường hợp không dấu ( uleb128 ), bất kỳ bit nào không được biểu diễn rõ ràng đều được hiểu là 0 .

Biểu đồ theo chiều bit của giá trị LEB128 hai byte
Byte đầu tiên Byte thứ hai
1 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 0 bit 13 bit 12 bit 11 bit 10 bit 9 bit 8 bit 7

Biến thể uleb128p1 được sử dụng để biểu thị một giá trị có dấu, trong đó biểu diễn là giá trị cộng với một giá trị được mã hóa dưới dạng uleb128 . Điều này làm cho mã hóa -1 (cách khác được coi là giá trị không dấu 0xffffffff ) - nhưng không có số âm nào khác - một byte duy nhất và hữu ích trong chính xác những trường hợp mà số được đại diện phải không âm hoặc -1 (hoặc 0xffffffff ), và khi không cho phép các giá trị âm khác (hoặc khi các giá trị không dấu lớn không cần thiết).

Dưới đây là một số ví dụ về các định dạng:

Trình tự được mã hóa As sleb128 Như uleb128 Như uleb128p1
00 0 0 -1
01 1 1 0
7f -1 127 126
80 7f -128 16256 16255

Bố cục tệp

Tên Định dạng Sự mô tả
tiêu đề header_item tiêu đề
string_ids string_id_item [] danh sách định danh chuỗi. Đây là các mã định danh cho tất cả các chuỗi được tệp này sử dụng, hoặc để đặt tên nội bộ (ví dụ: bộ mô tả kiểu) hoặc như các đối tượng hằng được tham chiếu bằng mã. Danh sách này phải được sắp xếp theo nội dung chuỗi, sử dụng các giá trị điểm mã UTF-16 (không phân biệt ngôn ngữ) và nó không được chứa bất kỳ mục nhập trùng lặp nào.
type_ids type_id_item [] gõ danh sách số nhận dạng. Đây là số nhận dạng cho tất cả các kiểu (lớp, mảng hoặc kiểu nguyên thủy) được tệp này tham chiếu, cho dù được định nghĩa trong tệp hay không. Danh sách này phải được sắp xếp theo chỉ mục string_id và nó không được chứa bất kỳ mục nào trùng lặp.
proto_ids proto_id_item [] danh sách định danh nguyên mẫu của phương pháp. Đây là những số nhận dạng cho tất cả các nguyên mẫu được tham chiếu bởi tệp này. Danh sách này phải được sắp xếp theo thứ tự chính kiểu trả về (theo chỉ mục type_id ) và sau đó là danh sách đối số (thứ tự từ vựng, các đối số riêng lẻ được sắp xếp theo chỉ mục type_id ). Danh sách không được chứa bất kỳ mục nào trùng lặp.
field_ids field_id_item [] danh sách định danh trường. Đây là những số nhận dạng cho tất cả các trường được tệp này tham chiếu, cho dù được xác định trong tệp hay không. Danh sách này phải được sắp xếp, trong đó kiểu xác định (theo chỉ mục type_id ) là thứ tự chính, tên trường (theo chỉ mục string_id ) là thứ tự trung gian và kiểu (theo chỉ mục type_id ) là thứ tự phụ. Danh sách không được chứa bất kỳ mục nào trùng lặp.
method_ids method_id_item [] danh sách định danh phương thức. Đây là những số nhận dạng cho tất cả các phương thức được tệp này tham chiếu, cho dù được định nghĩa trong tệp hay không. Danh sách này phải được sắp xếp, trong đó kiểu xác định (theo chỉ mục type_id ) là thứ tự chính, tên phương thức (theo chỉ mục string_id ) là thứ tự trung gian và nguyên mẫu phương thức (theo chỉ mục proto_id ) là thứ tự nhỏ. Danh sách không được chứa bất kỳ mục nào trùng lặp.
class_defs class_def_item [] danh sách định nghĩa lớp. Các lớp phải được sắp xếp sao cho lớp cha của một lớp nhất định và các giao diện được triển khai xuất hiện trong danh sách sớm hơn lớp giới thiệu. Hơn nữa, định nghĩa cho lớp cùng tên xuất hiện nhiều lần trong danh sách là không hợp lệ.
call_site_ids call_site_id_item [] gọi danh sách nhận dạng trang web. Đây là những số nhận dạng cho tất cả các trang web cuộc gọi được tham chiếu bởi tệp này, cho dù được xác định trong tệp hay không. Danh sách này phải được sắp xếp theo thứ tự tăng dần của call_site_off .
method_handles method_handle_item [] phương thức xử lý danh sách. Danh sách tất cả các xử lý phương thức được tham chiếu bởi tệp này, cho dù được xác định trong tệp hay không. Danh sách này không được sắp xếp và có thể chứa các bản sao sẽ tương ứng một cách hợp lý với các trường hợp xử lý phương thức khác nhau.
dữ liệu ubyte [] vùng dữ liệu, chứa tất cả dữ liệu hỗ trợ cho các bảng được liệt kê ở trên. Các mục khác nhau có các yêu cầu căn chỉnh khác nhau và các byte đệm được chèn trước mỗi mục nếu cần thiết để đạt được căn chỉnh phù hợp.
link_data ubyte [] dữ liệu được sử dụng trong các tệp được liên kết tĩnh. Định dạng của dữ liệu trong phần này được tài liệu này không xác định. Phần này trống trong các tệp không được liên kết và các triển khai thời gian chạy có thể sử dụng phần này khi chúng thấy phù hợp.

Định nghĩa trường bit, chuỗi và hằng số

DEX_FILE_MAGIC

nhúng vào header_item

Mảng / chuỗi DEX_FILE_MAGIC là danh sách các byte phải xuất hiện ở đầu tệp .dex để tệp được nhận dạng như vậy. Giá trị cố ý chứa một dòng mới ( "\n" hoặc 0x0a ) và một byte rỗng ( "\0" hoặc 0x00 ) để giúp phát hiện một số dạng hỏng nhất định. Giá trị cũng mã hóa số phiên bản định dạng dưới dạng ba chữ số thập phân, dự kiến ​​sẽ tăng đơn điệu theo thời gian khi định dạng phát triển.

ubyte[8] DEX_FILE_MAGIC = { 0x64 0x65 0x78 0x0a 0x30 0x33 0x39 0x00 }
                        = "dex\n039\0"

Lưu ý: Hỗ trợ cho phiên bản 039 của định dạng đã được thêm vào trong bản phát hành Android 9.0, giới thiệu hai mã byte mới, const-method-handleconst-method-type . (Mỗi cái này được mô tả trong bảng Tóm tắt về bộ mã bytecode .) Trong Android 10, phiên bản 039 mở rộng định dạng tệp DEX để bao gồm thông tin API ẩn chỉ áp dụng cho tệp DEX trên đường dẫn lớp khởi động.

Lưu ý: Hỗ trợ cho phiên bản 038 của định dạng đã được thêm vào trong bản phát hành Android 8.0. Phiên bản 038 đã thêm các mã bytecodes mới ( invoke-polymorphicinvoke-custom ) và dữ liệu cho các xử lý phương thức.

Lưu ý: Hỗ trợ cho phiên bản 037 của định dạng đã được thêm vào trong bản phát hành Android 7.0. Trước phiên bản 037 hầu hết các phiên bản Android đều sử dụng định dạng phiên bản 035 . Sự khác biệt duy nhất giữa phiên bản 035037 là việc bổ sung các phương thức mặc định và điều chỉnh lệnh invoke .

Lưu ý: Ít nhất một vài phiên bản trước đó của định dạng đã được sử dụng trong các bản phát hành phần mềm công cộng có sẵn rộng rãi. Ví dụ: phiên bản 009 được sử dụng cho các bản phát hành M3 của nền tảng Android (tháng 11 đến tháng 12 năm 2007) và phiên bản 013 được sử dụng cho các bản phát hành M5 của nền tảng Android (tháng 2 đến tháng 3 năm 2008). Theo một số khía cạnh, các phiên bản trước của định dạng này khác đáng kể so với phiên bản được mô tả trong tài liệu này.

ENDIAN_CONSTANT và REVERSE_ENDIAN_CONSTANT

nhúng vào header_item

Hằng số ENDIAN_CONSTANT được sử dụng để chỉ ra mức độ cuối cùng của tệp mà nó được tìm thấy. Mặc dù định dạng .dex tiêu chuẩn là ít cuối cùng, nhưng việc triển khai có thể chọn thực hiện hoán đổi byte. Nếu một triển khai gặp phải tiêu đề có endian_tagREVERSE_ENDIAN_CONSTANT thay vì ENDIAN_CONSTANT , nó sẽ biết rằng tệp đã được hoán đổi byte từ biểu mẫu mong đợi.

uint ENDIAN_CONSTANT = 0x12345678;
uint REVERSE_ENDIAN_CONSTANT = 0x78563412;

NO_INDEX

được nhúng trong class_def_item và debug_info_item

Hằng số NO_INDEX được sử dụng để chỉ ra rằng không có giá trị chỉ mục.

Lưu ý: Giá trị này không được xác định là 0 , vì trên thực tế, đó thường là một chỉ mục hợp lệ.

Giá trị đã chọn cho NO_INDEX có thể biểu diễn dưới dạng một byte đơn trong mã hóa uleb128p1 .

uint NO_INDEX = 0xffffffff;    // == -1 if treated as a signed int

định nghĩa access_flags

được nhúng trong class_def_item, encoded_field, encoded_method và InnerClass

Các trường bit của các cờ này được sử dụng để chỉ ra khả năng truy cập và các thuộc tính tổng thể của các lớp và thành viên lớp.

Tên Giá trị Dành cho các lớp (và chú thích InnerClass ) Đối với các lĩnh vực Đối với các phương pháp
ACC_PUBLIC 0x1 public : hiển thị ở mọi nơi public : hiển thị ở mọi nơi public : hiển thị ở mọi nơi
ACC_PRIVATE 0x2 * private : chỉ hiển thị cho lớp xác định private : chỉ hiển thị cho lớp xác định private : chỉ hiển thị cho lớp xác định
ACC_PROTECTED 0x4 * protected : hiển thị với gói và các lớp con protected : hiển thị với gói và các lớp con protected : hiển thị với gói và các lớp con
ACC_STATIC 0x8 * static : không được xây dựng với một tham chiếu bên ngoài this static : toàn cầu để xác định lớp static : không lấy đối số this
ACC_FINAL 0x10 final : không phân lớp được final : bất biến sau khi xây dựng final : không thể ghi đè
ACC_SYNCHRONIZED 0x20 synchronized : khóa được liên kết tự động nhận được xung quanh cuộc gọi đến phương thức này.

Lưu ý: Điều này chỉ hợp lệ để đặt khi ACC_NATIVE cũng được đặt.

ACC_VOLATILE 0x40 volatile : các quy tắc truy cập đặc biệt để giúp đảm bảo an toàn cho chuỗi
ACC_BRIDGE 0x40 phương pháp cầu nối, được thêm tự động bởi trình biên dịch dưới dạng một cầu nối loại an toàn
ACC_TRANSIENT 0x80 transient : không được lưu bằng tuần tự hóa mặc định
ACC_VARARGS 0x80 đối số cuối cùng phải được trình biên dịch coi là đối số "nghỉ ngơi"
ACC_NATIVE 0x100 native : được triển khai bằng mã gốc
ACC_INTERFACE 0x200 interface : lớp trừu tượng có thể triển khai nhân
ACC_ABSTRACT 0x400 abstract : không thể tạo trực tiếp abstract : không được thực hiện bởi lớp này
ACC_STRICT 0x800 strictfp : quy tắc nghiêm ngặt cho số học dấu phẩy động
ACC_SYNTHETIC 0x1000 không được xác định trực tiếp trong mã nguồn không được xác định trực tiếp trong mã nguồn không được xác định trực tiếp trong mã nguồn
ACC_ANNOTATION 0x2000 được khai báo như một lớp chú thích
ACC_ENUM 0x4000 được khai báo như một kiểu liệt kê được khai báo như một giá trị được liệt kê
(không sử dụng) 0x8000
ACC_CONTHEROR 0x10000 phương thức khởi tạo (bộ khởi tạo lớp hoặc phiên bản)
ACC_DECLARED_
ĐỒNG BỘ HÓA
0x20000 đã khai báo synchronized .

Lưu ý: Điều này không ảnh hưởng đến việc thực thi (ngoại trừ sự phản ánh của cờ này).

* Chỉ được phép bật cho các chú thích InnerClass và không được phép bật trong class_def_item .

Mã hóa MUTF-8 (UTF-8 được sửa đổi)

Như một sự nhượng bộ để hỗ trợ kế thừa dễ dàng hơn, định dạng .dex mã hóa dữ liệu chuỗi của nó ở dạng UTF-8 được sửa đổi theo tiêu chuẩn trên thực tế, sau đây được gọi là MUTF-8. Biểu mẫu này giống với UTF-8 tiêu chuẩn, ngoại trừ:

  • Chỉ các mã hóa một, hai và ba byte được sử dụng.
  • Các điểm mã trong phạm vi U+10000U+10ffff được mã hóa dưới dạng một cặp thay thế, mỗi điểm trong số đó được biểu diễn dưới dạng giá trị được mã hóa ba byte.
  • Điểm mã U+0000 được mã hóa ở dạng hai byte.
  • Một byte rỗng đơn thuần (giá trị 0 ) chỉ ra phần cuối của một chuỗi, cũng như cách diễn giải ngôn ngữ C tiêu chuẩn.

Hai mục đầu tiên ở trên có thể được tóm tắt là: MUTF-8 là định dạng mã hóa cho UTF-16, thay vì là định dạng mã hóa trực tiếp hơn cho các ký tự Unicode.

Hai mục cuối cùng ở trên làm cho nó có thể đồng thời bao gồm điểm mã U+0000 trong một chuỗi vẫn thao tác nó như một chuỗi kết thúc bằng null kiểu C.

Tuy nhiên, mã hóa đặc biệt của U+0000 có nghĩa là, không giống như UTF-8 bình thường, kết quả của việc gọi hàm C chuẩn strcmp() trên một cặp chuỗi MUTF -8 không phải lúc nào cũng chỉ ra kết quả được ký đúng khi so sánh các chuỗi không bằng nhau . Khi thứ tự (không chỉ bình đẳng) là một mối quan tâm, cách đơn giản nhất để so sánh các chuỗi MUTF-8 là giải mã chúng theo từng ký tự và so sánh các giá trị được giải mã. (Tuy nhiên, cũng có thể triển khai thông minh hơn.)

Vui lòng tham khảo Tiêu chuẩn Unicode để biết thêm thông tin về mã hóa ký tự. MUTF-8 thực sự gần với mã hóa CESU-8 (tương đối ít nổi tiếng) hơn là UTF-8.

mã hóa encoded_value

được nhúng trong annotation_element và encoded_array_item

encoded_value mã hóa là một phần được mã hóa của (gần như) dữ liệu có cấu trúc phân cấp tùy ý. Mã hóa có nghĩa là vừa nhỏ gọn và dễ phân tích cú pháp.

Tên Định dạng Sự mô tả
(value_arg << 5) | value_type ubyte byte chỉ ra loại của value ngay sau đó cùng với đối số làm rõ tùy chọn trong ba bit bậc cao. Xem bên dưới để biết các định nghĩa value khác nhau. Trong hầu hết các trường hợp, value_arg mã hóa độ dài của value ngay sau đó theo byte, như (size - 1) , ví dụ: 0 có nghĩa là giá trị yêu cầu một byte và 7 có nghĩa là nó yêu cầu tám byte; tuy nhiên, có những trường hợp ngoại lệ như được lưu ý dưới đây.
giá trị ubyte [] byte đại diện cho giá trị, có độ dài thay đổi và được diễn giải khác nhau cho các byte value_type khác nhau, mặc dù luôn luôn là phần cuối nhỏ. Xem các định nghĩa giá trị khác nhau bên dưới để biết chi tiết.

Định dạng giá trị

Tên loại value_type Định dạng value_arg định dạng value Sự mô tả
VALUE_BYTE 0x00 (không có; phải là 0 ) ubyte [1] giá trị số nguyên một byte đã ký
VALUE_SHORT 0x02 kích thước - 1 (0… 1) ubyte [kích thước] giá trị số nguyên hai byte đã ký, dấu mở rộng
VALUE_CHAR 0x03 kích thước - 1 (0… 1) ubyte [kích thước] giá trị số nguyên hai byte không dấu, không mở rộng
VALUE_INT 0x04 kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte đã ký, dấu mở rộng
VALUE_LONG 0x06 kích thước - 1 (0… 7) ubyte [kích thước] giá trị số nguyên tám byte đã ký, dấu mở rộng
VALUE_FLOAT 0x10 kích thước - 1 (0… 3) ubyte [kích thước] mẫu bit bốn byte, không được mở rộng sang bên phải và được hiểu là giá trị dấu chấm động 32 bit IEEE754
VALUE_DOUBLE 0x11 kích thước - 1 (0… 7) ubyte [kích thước] mẫu bit tám byte, không được mở rộng sang bên phải và được hiểu là giá trị dấu chấm động 64 bit IEEE754
VALUE_METHOD_TYPE 0x15 kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần proto_ids và đại diện cho một giá trị kiểu phương thức
VALUE_METHOD_HANDLE 0x16 kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần method_handles và đại diện cho một giá trị xử lý phương thức
VALUE_STRING 0x17 kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần string_ids và đại diện cho một giá trị chuỗi
VALUE_TYPE 0x18 kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần type_ids và đại diện cho một giá trị kiểu / lớp phản chiếu
VALUE_FIELD 0x19 kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần field_ids và đại diện cho một giá trị trường phản chiếu
VALUE_METHOD 0x1a kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần method_ids và đại diện cho một giá trị phương thức phản chiếu
VALUE_ENUM 0x1b kích thước - 1 (0… 3) ubyte [kích thước] giá trị số nguyên bốn byte không dấu (không mở rộng), được hiểu là một chỉ mục trong phần field_ids và đại diện cho giá trị của một hằng số kiểu liệt kê
VALUE_ARRAY 0x1c (không có; phải là 0 ) encoded_array một mảng các giá trị, ở định dạng được chỉ định bởi "định dạng encoded_array " bên dưới. Kích thước của value được ngầm định trong mã hóa.
VALUE_ANNOTATION 0x1d (không có; phải là 0 ) encoded_annotation chú thích phụ, ở định dạng được chỉ định bởi "định dạng encoded_annotation " bên dưới. Kích thước của value được ngầm định trong mã hóa.
VALUE_NULL 0x1e (không có; phải là 0 ) (không ai) giá trị tham chiếu null
VALUE_BOOLEAN 0x1f boolean (0… 1) (không ai) giá trị một bit; 0 cho false1 cho true . Bit được biểu diễn trong value_arg .

định dạng encoded_array

Tên Định dạng Sự mô tả
kích thước uleb128 số phần tử trong mảng
giá trị encoded_value [size] một loạt encoded_value byte mã hóa giá trị size theo định dạng được chỉ định bởi phần này, được nối tuần tự.

định dạng encoded_annotation

Tên Định dạng Sự mô tả
type_idx uleb128 loại chú thích. Đây phải là một kiểu lớp (không phải mảng hoặc kiểu nguyên thủy).
kích thước uleb128 số ánh xạ tên-giá trị trong chú thích này
các yếu tố annotation_element [size] các phần tử của chú thích, được trình bày trực tiếp trong dòng (không phải là phần bù). Các phần tử phải được sắp xếp theo thứ tự tăng dần theo chỉ số string_id .

định dạng annotation_element

Tên Định dạng Sự mô tả
name_idx uleb128 tên phần tử, được biểu diễn dưới dạng chỉ mục trong phần string_ids . Chuỗi phải tuân theo cú pháp cho MemberName , được định nghĩa ở trên.
giá trị giá trị mã hóa giá trị phần tử

Cú pháp chuỗi

Có một số loại mục trong tệp .dex cuối cùng tham chiếu đến một chuỗi. Các định nghĩa kiểu BNF sau đây chỉ ra cú pháp được chấp nhận cho các chuỗi này.

SimpleName

Một SimpleName là cơ sở cho cú pháp tên của những thứ khác. Định dạng .dex cho phép rất nhiều vĩ độ ở đây (nhiều hơn nhiều so với hầu hết các ngôn ngữ nguồn phổ biến). Tóm lại, một cái tên đơn giản bao gồm bất kỳ ký tự hoặc chữ số ASCII thấp nào, một vài ký hiệu ASCII thấp cụ thể và hầu hết các điểm mã không phải ASCII không phải là ký tự điều khiển, dấu cách hoặc ký tự đặc biệt. Bắt đầu từ phiên bản 040 , định dạng cũng cho phép các ký tự khoảng trắng (danh mục Unicode Zs ). Lưu ý rằng các điểm mã thay thế (trong phạm vi U+d800U+dfff ) không được coi là các ký tự tên hợp lệ, nhưng các ký tự bổ sung Unicode hợp lệ (được đại diện bởi sự thay thế cuối cùng của quy tắc cho SimpleNameChar ) và chúng phải được biểu diễn trong một tệp dưới dạng các cặp điểm mã thay thế trong bảng mã MUTF-8.

SimpleName
SimpleNameChar ( SimpleNameChar ) *
SimpleNameChar
'A''Z'
| 'a''z'
| '0''9'
| ' ' kể từ phiên bản DEX 040
| '$'
| '-'
| '_'
| U+00a0 kể từ phiên bản DEX 040
| U+00a1U+1fff
| U+2000U+200a kể từ phiên bản DEX 040
| U+2010U+2027
| U+202f kể từ phiên bản DEX 040
| U+2030U+d7ff
| U+e000U+ffef
| U+10000U+10ffff

Tên thành viên

được sử dụng bởi field_id_item và method_id_item

MemberName là tên của một thành viên của một lớp, các thành viên là các trường, các phương thức và các lớp bên trong.

Tên thành viên
SimpleName
| '<' SimpleName '>'

FullClassName

FullClassName là một tên lớp đủ điều kiện, bao gồm một chỉ định gói tùy chọn theo sau là một tên bắt buộc.

FullClassName
Tùy chọnPackagePrefix SimpleName
Tùy chọnPackagePrefix
( SimpleName '/' ) *

TypeDescriptor

được sử dụng bởi type_id_item

TypeDescriptor là đại diện của bất kỳ kiểu nào, bao gồm các nguyên thủy, lớp, mảng và void . Xem bên dưới để biết ý nghĩa của các phiên bản khác nhau.

TypeDescriptor
'V'
| FieldTypeDescriptor
FieldTypeDescriptor
NonArrayFieldTypeDescriptor
| ( '[' * 1… 255) NonArrayFieldTypeDescriptor
NonArrayFieldTypeDescriptor
'Z'
| 'B'
| 'S'
| 'C'
| 'I'
| 'J'
| 'F'
| 'D'
| 'L' FullClassName ';'

ShortyDescriptor

được sử dụng bởi proto_id_item

ShortyDescriptor là biểu diễn dạng ngắn của nguyên mẫu phương thức, bao gồm các kiểu trả về và tham số, ngoại trừ việc không có sự phân biệt giữa các kiểu tham chiếu (lớp hoặc mảng) khác nhau. Thay vào đó, tất cả các loại tham chiếu được biểu thị bằng một ký tự 'L' .

ShortyDescriptor
ShortyReturnType ( ShortyFieldType ) *
ShortyReturnType
'V'
| ShortyFieldType
ShortyFieldType
'Z'
| 'B'
| 'S'
| 'C'
| 'I'
| 'J'
| 'F'
| 'D'
| 'L'

TypeDescriptor Semantics

Đây là ý nghĩa của từng biến thể của TypeDescriptor .

Cú pháp Nghĩa
V void ; chỉ hợp lệ cho các loại trả lại
Z boolean
B byte
S short
C char
Tôi int
J long
F float
D double
L đầy đủ / đủ điều kiện / Tên ; lớp học fully.qualified.Name
[ bộ mô tả mảng bộ descriptor , có thể sử dụng đệ quy cho mảng-mảng, mặc dù có nhiều hơn 255 thứ nguyên là không hợp lệ.

Các hạng mục và cấu trúc liên quan

Phần này bao gồm các định nghĩa cho từng mục cấp cao nhất có thể xuất hiện trong tệp .dex .

header_item

xuất hiện trong phần tiêu đề

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
ma thuật ubyte [8] = DEX_FILE_MAGIC giá trị kỳ diệu. Xem thảo luận ở trên trong " DEX_FILE_MAGIC " để biết thêm chi tiết.
tổng kiểm tra uint adler32 tổng kiểm tra của phần còn lại của tệp (mọi thứ trừ magic và trường này); được sử dụng để phát hiện tệp bị hỏng
Chữ ký ubyte [20] SHA-1 chữ ký (băm) của phần còn lại của tệp (mọi thứ trừ magic , checksum và trường này); được sử dụng để xác định duy nhất các tệp
Kích thước tập tin uint kích thước của toàn bộ tệp (bao gồm cả tiêu đề), tính bằng byte
header_size uint = 0x70 kích thước của tiêu đề (toàn bộ phần này), tính bằng byte. Điều này cho phép ít nhất một số lượng hạn chế khả năng tương thích ngược / chuyển tiếp mà không làm mất hiệu lực của định dạng.
endian_tag uint = ENDIAN_CONSTANT thẻ endianness. Xem thảo luận ở trên trong " ENDIAN_CONSTANTREVERSE_ENDIAN_CONSTANT " để biết thêm chi tiết.
link_size uint kích thước của phần liên kết hoặc 0 nếu tệp này không được liên kết tĩnh
link_off uint offset từ đầu tệp đến phần liên kết hoặc 0 nếu link_size == 0 . Phần bù, nếu khác 0, phải là phần bù vào phần link_data . Định dạng của dữ liệu được trỏ vào không được tài liệu này xác định; trường tiêu đề này (và trường trước đó) được để lại dưới dạng móc để triển khai thời gian chạy sử dụng.
map_off uint bù trừ từ đầu tệp đến mục bản đồ. Phần bù, phải khác 0, phải là phần bù vào phần data và dữ liệu phải ở định dạng được chỉ định bởi " map_list " bên dưới.
string_ids_size uint số chuỗi trong danh sách số nhận dạng chuỗi
string_ids_off uint offset từ đầu tệp đến danh sách số nhận dạng chuỗi hoặc 0 nếu string_ids_size == 0 (phải thừa nhận là trường hợp cạnh lạ). Phần bù, nếu khác 0, phải ở đầu phần string_ids .
type_ids_size uint số phần tử trong danh sách số nhận dạng loại, nhiều nhất là 65535
type_ids_off uint offset từ đầu tệp đến danh sách định danh kiểu hoặc 0 nếu type_ids_size == 0 (phải thừa nhận là trường hợp cạnh lạ). Phần bù, nếu khác 0, phải ở đầu phần type_ids .
proto_ids_size uint số phần tử trong danh sách số nhận dạng nguyên mẫu, nhiều nhất là 65535
proto_ids_off uint offset từ đầu tệp đến danh sách mã định danh nguyên mẫu hoặc 0 nếu proto_ids_size == 0 (phải thừa nhận là trường hợp cạnh lạ). Phần bù, nếu khác 0, phải ở đầu phần proto_ids .
field_ids_size uint số phần tử trong danh sách số nhận dạng trường
field_ids_off uint bù trừ từ đầu tệp đến danh sách số nhận dạng trường hoặc 0 nếu field_ids_size == 0 . Phần bù, nếu khác 0, phải ở đầu phần field_ids .
method_ids_size uint số phần tử trong danh sách số nhận dạng phương thức
method_ids_off uint offset từ đầu tệp đến danh sách mã định danh phương thức hoặc 0 nếu method_ids_size == 0 . Phần bù, nếu khác 0, phải ở đầu phần method_ids .
class_defs_size uint số phần tử trong danh sách định nghĩa lớp
class_defs_off uint offset từ đầu tệp đến danh sách định nghĩa lớp, hoặc 0 nếu class_defs_size == 0 (phải thừa nhận là trường hợp cạnh lạ). Phần bù, nếu khác 0, phải ở đầu phần class_defs .
kích thước dữ liệu uint Kích thước của phần data tính bằng byte. Phải là bội số chẵn của sizeof (uint).
data_off uint offset từ đầu tệp đến đầu phần data .

map_list

xuất hiện trong phần dữ liệu

được tham chiếu từ header_item

căn chỉnh: 4 byte

Đây là danh sách toàn bộ nội dung của một tệp, theo thứ tự. Nó chứa một số dư thừa liên quan đến header_item nhưng được thiết kế để trở thành một biểu mẫu dễ sử dụng để lặp lại trên toàn bộ tệp. Một loại nhất định phải xuất hiện nhiều nhất một lần trong bản đồ, nhưng không có giới hạn nào về loại thứ tự có thể xuất hiện, ngoại trừ các hạn chế được ngụ ý bởi phần còn lại của định dạng (ví dụ: phần header phải xuất hiện đầu tiên, theo sau là string_ids phần, v.v.). Ngoài ra, các mục bản đồ phải được sắp xếp theo độ lệch ban đầu và không được chồng chéo.

Tên Định dạng Sự mô tả
kích thước uint kích thước của danh sách, trong các mục nhập
danh sách map_item [kích thước] các yếu tố của danh sách

định dạng map_item

Tên Định dạng Sự mô tả
loại hình ushort loại của các mặt hàng; xem bảng dưới đây
không sử dụng ushort (không sử dụng)
kích thước uint đếm số lượng mục được tìm thấy ở mức bù được chỉ định
bù lại uint bù trừ từ đầu tệp đến các mục được đề cập

Loại mã

Loại sản phẩm Không thay đổi Giá trị Kích thước mục tính bằng byte
header_item TYPE_HEADER_ITEM 0x0000 0x70
string_id_item TYPE_STRING_ID_ITEM 0x0001 0x04
type_id_item TYPE_TYPE_ID_ITEM 0x0002 0x04
proto_id_item TYPE_PROTO_ID_ITEM 0x0003 0x0c
field_id_item TYPE_FIELD_ID_ITEM 0x0004 0x08
method_id_item TYPE_METHOD_ID_ITEM 0x0005 0x08
class_def_item TYPE_CLASS_DEF_ITEM 0x0006 0x20
call_site_id_item TYPE_CALL_SITE_ID_ITEM 0x0007 0x04
method_handle_item TYPE_METHOD_HANDLE_ITEM 0x0008 0x08
map_list TYPE_MAP_LIST 0x1000 4 + (item.size * 12)
type_list TYPE_TYPE_LIST 0x1001 4 + (item.size * 2)
annotation_set_ref_list TYPE_ANNOTATION_SET_REF_LIST 0x1002 4 + (item.size * 4)
annotation_set_item TYPE_ANNOTATION_SET_ITEM 0x1003 4 + (item.size * 4)
class_data_item TYPE_CLASS_DATA_ITEM 0x2000 ngầm hiểu; phải phân tích cú pháp
code_item TYPE_CODE_ITEM 0x2001 ngầm hiểu; phải phân tích cú pháp
string_data_item TYPE_STRING_DATA_ITEM 0x2002 ngầm hiểu; phải phân tích cú pháp
debug_info_item TYPE_DEBUG_INFO_ITEM 0x2003 ngầm hiểu; phải phân tích cú pháp
annotation_item TYPE_ANNOTATION_ITEM 0x2004 ngầm hiểu; phải phân tích cú pháp
encoded_array_item TYPE_ENCODED_ARRAY_ITEM 0x2005 ngầm hiểu; phải phân tích cú pháp
annotations_directory_item TYPE_ANNOTATIONS_DIRECTORY_ITEM 0x2006 ngầm hiểu; phải phân tích cú pháp
hiddenapi_class_data_item TYPE_HIDDENAPI_CLASS_DATA_ITEM 0xF000 ngầm hiểu; phải phân tích cú pháp

string_id_item

xuất hiện trong phần string_ids

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
string_data_off uint bù trừ từ đầu tệp đến dữ liệu chuỗi cho mục này. Phần bù phải nằm ở một vị trí trong phần data và dữ liệu phải ở định dạng được chỉ định bởi " string_data_item " bên dưới. Không có yêu cầu căn chỉnh cho phần bù.

string_data_item

xuất hiện trong phần dữ liệu

căn chỉnh: không (căn theo byte)

Tên Định dạng Sự mô tả
utf16_size uleb128 kích thước của chuỗi này, tính bằng đơn vị mã UTF-16 (là "độ dài chuỗi" trong nhiều hệ thống). Đó là, đây là độ dài được giải mã của chuỗi. (Độ dài được mã hóa được ngụ ý bởi vị trí của byte 0 )
dữ liệu ubyte [] một loạt các đơn vị mã MUTF-8 (hay còn gọi là octet, hay còn gọi là byte) theo sau là một byte có giá trị 0 . Xem "Mã hóa MUTF-8 (UTF-8) được sửa đổi" ở trên để biết chi tiết và thảo luận về định dạng dữ liệu.

Lưu ý: Có thể chấp nhận một chuỗi bao gồm (dạng được mã hóa của) đơn vị mã thay thế UTF-16 (nghĩa là, U+d800U+dfff ) tách biệt hoặc không theo thứ tự đối với mã hóa thông thường của Unicode thành UTF-16. Tùy thuộc vào việc sử dụng cấp cao hơn của các chuỗi để từ chối các mã hóa không hợp lệ như vậy, nếu thích hợp.

type_id_item

xuất hiện trong phần type_ids

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
descriptor_idx uint chỉ mục vào danh sách string_ids cho chuỗi mô tả của loại này. Chuỗi phải tuân theo cú pháp cho TypeDescriptor , được định nghĩa ở trên.

proto_id_item

xuất hiện trong phần proto_ids

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
shorty_idx uint chỉ mục vào danh sách string_ids cho chuỗi mô tả dạng ngắn của nguyên mẫu này. Chuỗi phải tuân theo cú pháp cho ShortyDescriptor , được định nghĩa ở trên và phải tương ứng với kiểu trả về và các tham số của mục này.
return_type_idx uint chỉ mục vào danh sách type_ids cho kiểu trả về của nguyên mẫu này
thông_số_chọn uint offset từ đầu tệp đến danh sách các loại tham số cho nguyên mẫu này hoặc 0 nếu nguyên mẫu này không có tham số. Phần bù này, nếu khác 0, phải nằm trong phần data và dữ liệu ở đó phải ở định dạng được chỉ định bởi "type_list" bên dưới. Ngoài ra, không nên tham chiếu đến kiểu void trong danh sách.

field_id_item

xuất hiện trong phần field_ids

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
class_idx ushort chỉ mục vào danh sách type_ids cho trình định nghĩa của trường này. Đây phải là một kiểu lớp chứ không phải kiểu mảng hoặc kiểu nguyên thủy.
type_idx ushort chỉ mục vào danh sách type_ids cho loại trường này
name_idx uint chỉ mục vào danh sách string_ids cho tên của trường này. Chuỗi phải tuân theo cú pháp cho MemberName , được định nghĩa ở trên.

method_id_item

xuất hiện trong phần method_ids

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
class_idx ushort chỉ mục vào danh sách type_ids cho trình định nghĩa của phương thức này. Đây phải là một kiểu lớp hoặc mảng, và không phải là một kiểu nguyên thủy.
proto_idx ushort chỉ mục vào danh sách proto_ids cho nguyên mẫu của phương pháp này
name_idx uint chỉ mục vào danh sách string_ids cho tên của phương thức này. Chuỗi phải tuân theo cú pháp cho MemberName , được định nghĩa ở trên.

class_def_item

xuất hiện trong phần class_defs

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
class_idx uint chỉ mục vào danh sách type_ids cho lớp này. Đây phải là một kiểu lớp chứ không phải kiểu mảng hoặc kiểu nguyên thủy.
access_flags uint cờ truy cập cho lớp ( public , final , v.v.). Xem "Định nghĩa access_flags " để biết chi tiết.
superclass_idx uint chỉ mục vào danh sách type_ids cho lớp cha hoặc giá trị không đổi NO_INDEX nếu lớp này không có lớp cha (nghĩa là nó là một lớp gốc chẳng hạn như Object ). Nếu có, đây phải là một kiểu lớp chứ không phải kiểu mảng hoặc kiểu nguyên thủy.
interface_off uint offset từ đầu tệp đến danh sách giao diện hoặc 0 nếu không có giao diện nào. Phần bù này phải nằm trong phần data và dữ liệu ở đó phải có định dạng được chỉ định bởi " type_list " bên dưới. Mỗi phần tử của danh sách phải là một kiểu lớp (không phải kiểu mảng hoặc kiểu nguyên thủy) và không được có bất kỳ bản sao nào.
source_file_idx uint chỉ mục vào danh sách string_ids cho tên của tệp chứa nguồn gốc cho (ít nhất là hầu hết) lớp này hoặc giá trị đặc biệt NO_INDEX để thể hiện sự thiếu thông tin này. debug_info_item của bất kỳ phương thức nhất định nào có thể ghi đè tệp nguồn này, nhưng kỳ vọng là hầu hết các lớp sẽ chỉ đến từ một tệp nguồn.
annotations_off uint offset từ đầu tệp đến cấu trúc chú thích cho lớp này hoặc 0 nếu không có chú thích nào trên lớp này. Phần bù này, nếu khác 0, phải nằm trong phần data và dữ liệu ở đó phải có định dạng được chỉ định bởi " annotations_directory_item " bên dưới, với tất cả các mục tham chiếu đến lớp này là trình định nghĩa.
class_data_off uint bù trừ từ đầu tệp đến dữ liệu lớp được liên kết cho mục này hoặc 0 nếu không có dữ liệu lớp cho lớp này. (Đây có thể là trường hợp, ví dụ: nếu lớp này là giao diện đánh dấu.) Phần bù, nếu khác 0, phải nằm trong phần data và dữ liệu ở đó phải có định dạng được chỉ định bởi " class_data_item " bên dưới, với tất cả các mục tham chiếu đến lớp này là trình định nghĩa.
static_values_off uint offset từ đầu tệp đến danh sách các giá trị ban đầu cho trường static hoặc 0 nếu không có trường nào (và tất cả các trường static phải được khởi tạo bằng 0 hoặc null ). Phần bù này phải nằm trong phần data và dữ liệu ở đó phải ở định dạng được chỉ định bởi " encoded_array_item " bên dưới. Kích thước của mảng không được lớn hơn số lượng trường static được khai báo bởi lớp này và các phần tử tương ứng với các trường static theo thứ tự như được khai báo trong field_list tương ứng. Kiểu của mỗi phần tử mảng phải khớp với kiểu đã khai báo của trường tương ứng. Nếu có ít phần tử trong mảng hơn là có các trường static , thì các trường còn lại được khởi tạo bằng 0 hoặc null phù hợp với kiểu.

call_site_id_item

xuất hiện trong phần call_site_ids

căn chỉnh: 4 byte

Tên Định dạng Sự mô tả
call_site_off uint bù đắp từ đầu tệp để gọi định nghĩa trang web. Phần bù phải nằm trong phần dữ liệu và dữ liệu ở đó phải ở định dạng được chỉ định bởi "call_site_item" bên dưới.

call_site_item

xuất hiện trong phần dữ liệu

căn chỉnh: không (căn lề theo byte)

Call_site_item là một encoded_array_item có các phần tử tương ứng với các đối số được cung cấp cho phương thức trình liên kết bootstrap. Ba đối số đầu tiên là:

  1. Một trình xử lý phương thức đại diện cho phương thức trình liên kết bootstrap (VALUE_METHOD_HANDLE).
  2. Tên phương thức mà trình liên kết bootstrap phải giải quyết (VALUE_STRING).
  3. Một loại phương thức tương ứng với loại tên phương thức sẽ được phân giải (VALUE_METHOD_TYPE).

Bất kỳ đối số bổ sung nào đều là các giá trị không đổi được chuyển đến phương thức trình liên kết bootstrap. Các đối số này được chuyển theo thứ tự và không có bất kỳ chuyển đổi kiểu nào.

Xử lý phương thức đại diện cho phương thức trình liên kết bootstrap phải có kiểu trả về java.lang.invoke.CallSite . Ba loại tham số đầu tiên là:

  1. java.lang.invoke.Lookup
  2. java.lang.String
  3. java.lang.invoke.MethodType

The parameter types of any additional arguments are determined from their constant values.

method_handle_item

appears in the method_handles section

alignment: 4 bytes

Tên Định dạng Sự mô tả
method_handle_type ushort type of the method handle; see table below
unused ushort (không sử dụng)
field_or_method_id ushort Field or method id depending on whether the method handle type is an accessor or a method invoker
unused ushort (không sử dụng)

Method Handle Type Codes

Constant Giá trị Sự mô tả
METHOD_HANDLE_TYPE_STATIC_PUT 0x00 Method handle is a static field setter (accessor)
METHOD_HANDLE_TYPE_STATIC_GET 0x01 Method handle is a static field getter (accessor)
METHOD_HANDLE_TYPE_INSTANCE_PUT 0x02 Method handle is an instance field setter (accessor)
METHOD_HANDLE_TYPE_INSTANCE_GET 0x03 Method handle is an instance field getter (accessor)
METHOD_HANDLE_TYPE_INVOKE_STATIC 0x04 Method handle is a static method invoker
METHOD_HANDLE_TYPE_INVOKE_INSTANCE 0x05 Method handle is an instance method invoker
METHOD_HANDLE_TYPE_INVOKE_CONSTRUCTOR 0x06 Method handle is a constructor method invoker
METHOD_HANDLE_TYPE_INVOKE_DIRECT 0x07 Method handle is a direct method invoker
METHOD_HANDLE_TYPE_INVOKE_INTERFACE 0x08 Method handle is an interface method invoker

class_data_item

referenced from class_def_item

appears in the data section

alignment: none (byte-aligned)

Tên Định dạng Sự mô tả
static_fields_size uleb128 the number of static fields defined in this item
instance_fields_size uleb128 the number of instance fields defined in this item
direct_methods_size uleb128 the number of direct methods defined in this item
virtual_methods_size uleb128 the number of virtual methods defined in this item
static_fields encoded_field[static_fields_size] the defined static fields, represented as a sequence of encoded elements. The fields must be sorted by field_idx in increasing order.
instance_fields encoded_field[instance_fields_size] the defined instance fields, represented as a sequence of encoded elements. The fields must be sorted by field_idx in increasing order.
direct_methods encoded_method[direct_methods_size] the defined direct (any of static , private , or constructor) methods, represented as a sequence of encoded elements. The methods must be sorted by method_idx in increasing order.
virtual_methods encoded_method[virtual_methods_size] the defined virtual (none of static , private , or constructor) methods, represented as a sequence of encoded elements. This list should not include inherited methods unless overridden by the class that this item represents. The methods must be sorted by method_idx in increasing order. The method_idx of a virtual method must not be the same as any direct method.

Note: All elements' field_id s and method_id s must refer to the same defining class.

encoded_field format

Tên Định dạng Sự mô tả
field_idx_diff uleb128 index into the field_ids list for the identity of this field (includes the name and descriptor), represented as a difference from the index of previous element in the list. The index of the first element in a list is represented directly.
access_flags uleb128 access flags for the field ( public , final , etc.). See " access_flags Definitions" for details.

encoded_method format

Tên Định dạng Sự mô tả
method_idx_diff uleb128 index into the method_ids list for the identity of this method (includes the name and descriptor), represented as a difference from the index of previous element in the list. The index of the first element in a list is represented directly.
access_flags uleb128 access flags for the method ( public , final , etc.). See " access_flags Definitions" for details.
code_off uleb128 offset from the start of the file to the code structure for this method, or 0 if this method is either abstract or native . The offset should be to a location in the data section. The format of the data is specified by " code_item " below.

type_list

referenced from class_def_item and proto_id_item

appears in the data section

alignment: 4 bytes

Tên Định dạng Sự mô tả
kích thước uint size of the list, in entries
list type_item[size] elements of the list

type_item format

Tên Định dạng Sự mô tả
type_idx ushort index into the type_ids list

code_item

referenced from encoded_method

appears in the data section

alignment: 4 bytes

Tên Định dạng Sự mô tả
registers_size ushort the number of registers used by this code
ins_size ushort the number of words of incoming arguments to the method that this code is for
outs_size ushort the number of words of outgoing argument space required by this code for method invocation
tries_size ushort the number of try_item s for this instance. If non-zero, then these appear as the tries array just after the insns in this instance.
debug_info_off uint offset from the start of the file to the debug info (line numbers + local variable info) sequence for this code, or 0 if there simply is no information. The offset, if non-zero, should be to a location in the data section. The format of the data is specified by " debug_info_item " below.
insns_size uint size of the instructions list, in 16-bit code units
insns ushort[insns_size] actual array of bytecode. The format of code in an insns array is specified by the companion document Dalvik bytecode . Note that though this is defined as an array of ushort , there are some internal structures that prefer four-byte alignment. Also, if this happens to be in an endian-swapped file, then the swapping is only done on individual ushort s and not on the larger internal structures.
padding ushort (optional) = 0 two bytes of padding to make tries four-byte aligned. This element is only present if tries_size is non-zero and insns_size is odd.
tries try_item[tries_size] (optional) array indicating where in the code exceptions are caught and how to handle them. Elements of the array must be non-overlapping in range and in order from low to high address. This element is only present if tries_size is non-zero.
handlers encoded_catch_handler_list (optional) bytes representing a list of lists of catch types and associated handler addresses. Each try_item has a byte-wise offset into this structure. This element is only present if tries_size is non-zero.

try_item format

Tên Định dạng Sự mô tả
start_addr uint start address of the block of code covered by this entry. The address is a count of 16-bit code units to the start of the first covered instruction.
insn_count ushort number of 16-bit code units covered by this entry. The last code unit covered (inclusive) is start_addr + insn_count - 1 .
handler_off ushort offset in bytes from the start of the associated encoded_catch_hander_list to the encoded_catch_handler for this entry. This must be an offset to the start of an encoded_catch_handler .

encoded_catch_handler_list format

Tên Định dạng Sự mô tả
kích thước uleb128 size of this list, in entries
list encoded_catch_handler[handlers_size] actual list of handler lists, represented directly (not as offsets), and concatenated sequentially

encoded_catch_handler format

Tên Định dạng Sự mô tả
kích thước sleb128 number of catch types in this list. If non-positive, then this is the negative of the number of catch types, and the catches are followed by a catch-all handler. For example: A size of 0 means that there is a catch-all but no explicitly typed catches. A size of 2 means that there are two explicitly typed catches and no catch-all. And a size of -1 means that there is one typed catch along with a catch-all.
handlers encoded_type_addr_pair[abs(size)] stream of abs(size) encoded items, one for each caught type, in the order that the types should be tested.
catch_all_addr uleb128 (optional) bytecode address of the catch-all handler. This element is only present if size is non-positive.

encoded_type_addr_pair format

Tên Định dạng Sự mô tả
type_idx uleb128 index into the type_ids list for the type of the exception to catch
addr uleb128 bytecode address of the associated exception handler

debug_info_item

referenced from code_item

appears in the data section

alignment: none (byte-aligned)

Each debug_info_item defines a DWARF3-inspired byte-coded state machine that, when interpreted, emits the positions table and (potentially) the local variable information for a code_item . The sequence begins with a variable-length header (the length of which depends on the number of method parameters), is followed by the state machine bytecodes, and ends with an DBG_END_SEQUENCE byte.

The state machine consists of five registers. The address register represents the instruction offset in the associated insns_item in 16-bit code units. The address register starts at 0 at the beginning of each debug_info sequence and must only monotonically increase. The line register represents what source line number should be associated with the next positions table entry emitted by the state machine. It is initialized in the sequence header, and may change in positive or negative directions but must never be less than 1 . The source_file register represents the source file that the line number entries refer to. It is initialized to the value of source_file_idx in class_def_item . The other two variables, prologue_end and epilogue_begin , are boolean flags (initialized to false ) that indicate whether the next position emitted should be considered a method prologue or epilogue. The state machine must also track the name and type of the last local variable live in each register for the DBG_RESTART_LOCAL code.

The header is as follows:

Tên Định dạng Sự mô tả
line_start uleb128 the initial value for the state machine's line register. Does not represent an actual positions entry.
parameters_size uleb128 the number of parameter names that are encoded. There should be one per method parameter, excluding an instance method's this , if any.
parameter_names uleb128p1[parameters_size] string index of the method parameter name. An encoded value of NO_INDEX indicates that no name is available for the associated parameter. The type descriptor and signature are implied from the method descriptor and signature.

The byte code values are as follows:

Tên Giá trị Định dạng Tranh luận Sự mô tả
DBG_END_SEQUENCE 0x00 (none) terminates a debug info sequence for a code_item
DBG_ADVANCE_PC 0x01 uleb128 addr_diff addr_diff : amount to add to address register advances the address register without emitting a positions entry
DBG_ADVANCE_LINE 0x02 sleb128 line_diff line_diff : amount to change line register by advances the line register without emitting a positions entry
DBG_START_LOCAL 0x03 uleb128 register_num
uleb128p1 name_idx
uleb128p1 type_idx
register_num : register that will contain local
name_idx : string index of the name
type_idx : type index of the type
introduces a local variable at the current address. Either name_idx or type_idx may be NO_INDEX to indicate that that value is unknown.
DBG_START_LOCAL_EXTENDED 0x04 uleb128 register_num
uleb128p1 name_idx
uleb128p1 type_idx
uleb128p1 sig_idx
register_num : register that will contain local
name_idx : string index of the name
type_idx : type index of the type
sig_idx : string index of the type signature
introduces a local with a type signature at the current address. Any of name_idx , type_idx , or sig_idx may be NO_INDEX to indicate that that value is unknown. (If sig_idx is -1 , though, the same data could be represented more efficiently using the opcode DBG_START_LOCAL .)

Note: See the discussion under " dalvik.annotation.Signature " below for caveats about handling signatures.

DBG_END_LOCAL 0x05 uleb128 register_num register_num : register that contained local marks a currently-live local variable as out of scope at the current address
DBG_RESTART_LOCAL 0x06 uleb128 register_num register_num : register to restart re-introduces a local variable at the current address. The name and type are the same as the last local that was live in the specified register.
DBG_SET_PROLOGUE_END 0x07 (none) sets the prologue_end state machine register, indicating that the next position entry that is added should be considered the end of a method prologue (an appropriate place for a method breakpoint). The prologue_end register is cleared by any special ( >= 0x0a ) opcode.
DBG_SET_EPILOGUE_BEGIN 0x08 (none) sets the epilogue_begin state machine register, indicating that the next position entry that is added should be considered the beginning of a method epilogue (an appropriate place to suspend execution before method exit). The epilogue_begin register is cleared by any special ( >= 0x0a ) opcode.
DBG_SET_FILE 0x09 uleb128p1 name_idx name_idx : string index of source file name; NO_INDEX if unknown indicates that all subsequent line number entries make reference to this source file name, instead of the default name specified in code_item
Special Opcodes 0x0a…0xff (none) advances the line and address registers, emits a position entry, and clears prologue_end and epilogue_begin . See below for description.

Special opcodes

Opcodes with values between 0x0a and 0xff (inclusive) move both the line and address registers by a small amount and then emit a new position table entry. The formula for the increments are as follows:

DBG_FIRST_SPECIAL = 0x0a  // the smallest special opcode
DBG_LINE_BASE   = -4      // the smallest line number increment
DBG_LINE_RANGE  = 15      // the number of line increments represented

adjusted_opcode = opcode - DBG_FIRST_SPECIAL

line += DBG_LINE_BASE + (adjusted_opcode % DBG_LINE_RANGE)
address += (adjusted_opcode / DBG_LINE_RANGE)

annotations_directory_item

referenced from class_def_item

appears in the data section

alignment: 4 bytes

Tên Định dạng Sự mô tả
class_annotations_off uint offset from the start of the file to the annotations made directly on the class, or 0 if the class has no direct annotations. The offset, if non-zero, should be to a location in the data section. The format of the data is specified by " annotation_set_item " below.
fields_size uint count of fields annotated by this item
annotated_methods_size uint count of methods annotated by this item
annotated_parameters_size uint count of method parameter lists annotated by this item
field_annotations field_annotation[fields_size] (optional) list of associated field annotations. The elements of the list must be sorted in increasing order, by field_idx .
method_annotations method_annotation[methods_size] (optional) list of associated method annotations. The elements of the list must be sorted in increasing order, by method_idx .
parameter_annotations parameter_annotation[parameters_size] (optional) list of associated method parameter annotations. The elements of the list must be sorted in increasing order, by method_idx .

Note: All elements' field_id s and method_id s must refer to the same defining class.

field_annotation format

Tên Định dạng Sự mô tả
field_idx uint index into the field_ids list for the identity of the field being annotated
annotations_off uint offset from the start of the file to the list of annotations for the field. The offset should be to a location in the data section. The format of the data is specified by " annotation_set_item " below.

method_annotation format

Tên Định dạng Sự mô tả
method_idx uint index into the method_ids list for the identity of the method being annotated
annotations_off uint offset from the start of the file to the list of annotations for the method. The offset should be to a location in the data section. The format of the data is specified by " annotation_set_item " below.

parameter_annotation format

Tên Định dạng Sự mô tả
method_idx uint index into the method_ids list for the identity of the method whose parameters are being annotated
annotations_off uint offset from the start of the file to the list of annotations for the method parameters. The offset should be to a location in the data section. The format of the data is specified by " annotation_set_ref_list " below.

annotation_set_ref_list

referenced from parameter_annotations_item

appears in the data section

alignment: 4 bytes

Tên Định dạng Sự mô tả
kích thước uint size of the list, in entries
list annotation_set_ref_item[size] elements of the list

annotation_set_ref_item format

Tên Định dạng Sự mô tả
annotations_off uint offset from the start of the file to the referenced annotation set or 0 if there are no annotations for this element. The offset, if non-zero, should be to a location in the data section. The format of the data is specified by " annotation_set_item " below.

annotation_set_item

referenced from annotations_directory_item, field_annotations_item, method_annotations_item, and annotation_set_ref_item

appears in the data section

alignment: 4 bytes

Tên Định dạng Sự mô tả
kích thước uint size of the set, in entries
entries annotation_off_item[size] elements of the set. The elements must be sorted in increasing order, by type_idx .

annotation_off_item format

Tên Định dạng Sự mô tả
annotation_off uint offset from the start of the file to an annotation. The offset should be to a location in the data section, and the format of the data at that location is specified by " annotation_item " below.

annotation_item

referenced from annotation_set_item

appears in the data section

alignment: none (byte-aligned)

Tên Định dạng Sự mô tả
visibility ubyte intended visibility of this annotation (see below)
annotation encoded_annotation encoded annotation contents, in the format described by " encoded_annotation format" under " encoded_value encoding" above.

Visibility values

These are the options for the visibility field in an annotation_item :

Tên Giá trị Sự mô tả
VISIBILITY_BUILD 0x00 intended only to be visible at build time (eg, during compilation of other code)
VISIBILITY_RUNTIME 0x01 intended to visible at runtime
VISIBILITY_SYSTEM 0x02 intended to visible at runtime, but only to the underlying system (and not to regular user code)

encoded_array_item

referenced from class_def_item

appears in the data section

alignment: none (byte-aligned)

Tên Định dạng Sự mô tả
value encoded_array bytes representing the encoded array value, in the format specified by " encoded_array Format" under " encoded_value Encoding" above.

hiddenapi_class_data_item

This section contains data on restricted interfaces used by each class.

Note: The hidden API feature was introduced in Android 10.0 and is only applicable to the DEX files of classes in the boot class path. The list of flags described below may be extended in the future releases of Android. For more information, see restrictions on non-SDK interfaces .

Tên Định dạng Sự mô tả
kích thước uint total size of the section
offsets uint[] array of offsets indexed by class_idx . A zero array entry at index class_idx means that either there is no data for this class_idx , or all hidden API flags are zero. Otherwise the array entry is non-zero and contains an offset from the beginning of the section to an array of hidden API flags for this class_idx .
flags uleb128[] concatenated arrays of hidden API flags for each class. Possible flag values are described in the table below. Flags are encoded in the same order as fields and methods are encoded in class data.

Restriction flag types:

Tên Giá trị Sự mô tả
whitelist 0 Interfaces that can be freely used and are supported as part of the officially documented Android framework Package Index .
greylist 1 Non-SDK interfaces that can be used regardless of the application's target API level .
blacklist 2 Non-SDK interfaces that cannot be used regardless of the application's target API level . Accessing one of these interfaces causes a runtime error .
greylist‑max‑o 3 Non-SDK interfaces that can be used for Android 8.x and below unless they are restricted.
greylist‑max‑p 4 Non-SDK interfaces that can be used for Android 9.x unless they are restricted.
greylist‑max‑q 5 Non-SDK interfaces that can be used for Android 10.x unless they are restricted.
greylist‑max‑r 6 Non-SDK interfaces that can be used for Android 11.x unless they are restricted.

System annotations

System annotations are used to represent various pieces of reflective information about classes (and methods and fields). This information is generally only accessed indirectly by client (non-system) code.

System annotations are represented in .dex files as annotations with visibility set to VISIBILITY_SYSTEM .

dalvik.annotation.AnnotationDefault

appears on methods in annotation interfaces

An AnnotationDefault annotation is attached to each annotation interface which wishes to indicate default bindings.

Tên Định dạng Sự mô tả
value Annotation the default bindings for this annotation, represented as an annotation of this type. The annotation need not include all names defined by the annotation; missing names simply do not have defaults.

dalvik.annotation.EnclosingClass

appears on classes

An EnclosingClass annotation is attached to each class which is either defined as a member of another class, per se, or is anonymous but not defined within a method body (eg, a synthetic inner class). Every class that has this annotation must also have an InnerClass annotation. Additionally, a class must not have both an EnclosingClass and an EnclosingMethod annotation.

Tên Định dạng Sự mô tả
value Class the class which most closely lexically scopes this class

dalvik.annotation.EnclosingMethod

appears on classes

An EnclosingMethod annotation is attached to each class which is defined inside a method body. Every class that has this annotation must also have an InnerClass annotation. Additionally, a class must not have both an EnclosingClass and an EnclosingMethod annotation.

Tên Định dạng Sự mô tả
value Method the method which most closely lexically scopes this class

dalvik.annotation.InnerClass

appears on classes

An InnerClass annotation is attached to each class which is defined in the lexical scope of another class's definition. Any class which has this annotation must also have either an EnclosingClass annotation or an EnclosingMethod annotation.

Tên Định dạng Sự mô tả
name String the originally declared simple name of this class (not including any package prefix). If this class is anonymous, then the name is null .
accessFlags int the originally declared access flags of the class (which may differ from the effective flags because of a mismatch between the execution models of the source language and target virtual machine)

dalvik.annotation.MemberClasses

appears on classes

A MemberClasses annotation is attached to each class which declares member classes. (A member class is a direct inner class that has a name.)

Tên Định dạng Sự mô tả
value Class[] array of the member classes

dalvik.annotation.MethodParameters

appears on methods

Note: This annotation was added after Android 7.1. Its presence on earlier Android releases will be ignored.

A MethodParameters annotation is optional and can be used to provide parameter metadata such as parameter names and modifiers.

The annotation can be omitted from a method or constructor safely when the parameter metadata is not required at runtime. java.lang.reflect.Parameter.isNamePresent() can be used to check whether metadata is present for a parameter, and the associated reflection methods such as java.lang.reflect.Parameter.getName() will fall back to default behavior at runtime if the information is not present.

When including parameter metadata, compilers must include information for generated classes such as enums, since the parameter metadata includes whether or not a parameter is synthetic or mandated.

A MethodParameters annotation describes only individual method parameters. Therefore, compilers may omit the annotation entirely for constructors and methods that have no parameters, for the sake of code-size and runtime efficiency.

The arrays documented below must be the same size as for the method_id_item dex structure associated with the method, otherwise a java.lang.reflect.MalformedParametersException will be thrown at runtime.

That is: method_id_item.proto_idx -> proto_id_item.parameters_off -> type_list.size must be the same as names().length and accessFlags().length .

Because MethodParameters describes all formal method parameters, even those not explicitly or implicitly declared in source code, the size of the arrays may differ from the Signature or other metadata information that is based only on explicit parameters declared in source code. MethodParameters will also not include any information about type annotation receiver parameters that do not exist in the actual method signature.

Tên Định dạng Sự mô tả
names String[] The names of formal parameters for the associated method. The array must not be null but must be empty if there are no formal parameters. A value in the array must be null if the formal parameter with that index has no name.
If parameter name strings are empty or contain '.', ';', '[' or '/' then a java.lang.reflect.MalformedParametersException will be thrown at runtime.
accessFlags int [] The access flags of the formal parameters for the associated method. The array must not be null but must be empty if there are no formal parameters.
The value is a bit mask with the following values:
  • 0x0010 : final, the parameter was declared final
  • 0x1000 : synthetic, the parameter was introduced by the compiler
  • 0x8000 : mandated, the parameter is synthetic but also implied by the language specification
If any bits are set outside of this set then a java.lang.reflect.MalformedParametersException will be thrown at runtime.

dalvik.annotation.Signature

appears on classes, fields, and methods

A Signature annotation is attached to each class, field, or method which is defined in terms of a more complicated type than is representable by a type_id_item . The .dex format does not define the format for signatures; it is merely meant to be able to represent whatever signatures a source language requires for successful implementation of that language's semantics. As such, signatures are not generally parsed (or verified) by virtual machine implementations. The signatures simply get handed off to higher-level APIs and tools (such as debuggers). Any use of a signature, therefore, should be written so as not to make any assumptions about only receiving valid signatures, explicitly guarding itself against the possibility of coming across a syntactically invalid signature.

Because signature strings tend to have a lot of duplicated content, a Signature annotation is defined as an array of strings, where duplicated elements naturally refer to the same underlying data, and the signature is taken to be the concatenation of all the strings in the array. There are no rules about how to pull apart a signature into separate strings; that is entirely up to the tools that generate .dex files.

Tên Định dạng Sự mô tả
value String[] the signature of this class or member, as an array of strings that is to be concatenated together

dalvik.annotation.Throws

appears on methods

A Throws annotation is attached to each method which is declared to throw one or more exception types.

Tên Định dạng Sự mô tả
value Class[] the array of exception types thrown