Skip to content

Editable DEX model

Read a DEX file, change it a whole class at a time, and write it again. See the editing guide.

Model

An editable DEX file in memory.

Read a DEX file into a model, change it, and write a DEX file again. From Python the model works on whole classes: inspect, copy, move, remove and rename. Changing the code inside a method is only possible from C++.

A model is separate from the analysis API. It is not an AnalysisContext.

len(model) is the number of classes, and descriptor in model tests for a class.

version instance-attribute

version: int

The DEX format version write uses. write never raises it by itself; set it when required_version() is higher.

__init__

__init__(version: int = ...) -> None

Make an empty model.

Parameters:

Name Type Description Default
version int

The DEX format version: 35, 37, 38, 39 or 40.

...

read staticmethod

read(data: ReadableBuffer, debug_info: bool = ...) -> Model

Read DEX bytes into a model.

Parameters:

Name Type Description Default
data ReadableBuffer

The content of a DEX file: bytes, bytearray, memoryview, or any other object that gives its bytes through the buffer protocol.

required
debug_info bool

Keep line numbers and local variable names. False leaves them out, which makes the model smaller.

...

Raises:

Type Description
InvalidDexError

The data is not a valid DEX file.

UnsupportedDexError

The data uses something the model does not support, such as the version 41 container format.

read_file staticmethod

read_file(path: str, debug_info: bool = ...) -> Model

Read a DEX file into a model.

Parameters:

Name Type Description Default
path str

The DEX file.

required
debug_info bool

Keep line numbers and local variable names. False leaves them out, which makes the model smaller.

...

Raises:

Type Description
FileNotFoundError

The file does not exist.

OSError

The file exists but cannot be read.

InvalidDexError

The file is not a valid DEX file.

UnsupportedDexError

The file uses something the model does not support, such as the version 41 container format.

clone

clone() -> Model

A full copy of the model. Changing one does not change the other.

required_version

required_version() -> int

The lowest DEX version that has every feature the model uses.

It runs the same checks as validate, so each call costs about as much.

pool_stats

pool_stats() -> PoolStats

How many entries each table of the written file would have.

It walks every class and every instruction of the model to collect the strings, types, prototypes, fields and methods they use, so each call takes time in proportion to the size of the model.

validate

validate() -> list[Problem]

Every rule of the DEX format the model breaks. An empty list means write can encode the model. Otherwise write raises InvalidModelError with the same list.

class_descriptors

class_descriptors() -> list[str]

The descriptor of every class, in model order.

Each call copies every descriptor into a new list. The list does not change when the model changes, so it is safe to loop over it while you move or remove classes.

class_info

class_info(descriptor: str) -> Optional[ClassInfo]

A summary of the class with this descriptor, or None when the model has no such class.

move_class

move_class(src: Model, descriptor: str) -> None

Take a class out of another model and put it into this one. The class is not copied.

Raises:

Type Description
ModelError

src has no such class, or this model already has one.

copy_class

copy_class(src: Model, descriptor: str) -> None

Copy a class from another model into this one. The other model is not changed.

Raises:

Type Description
ModelError

src has no such class, or this model already has one.

remove_class

remove_class(descriptor: str) -> bool

Remove the class with this descriptor. Returns whether a class was removed.

rename_types

rename_types(mapping: dict[str, str]) -> None

Rename classes everywhere they are referenced.

The mapping goes from old descriptor to new descriptor. Every reference to a class in it is changed: the class itself, superclasses and interfaces, method and field identities, array types, instructions, catch types, annotations, and the class names inside generic signatures. A class that is not in the mapping is left as it is.

String constants are never changed, so a name passed to Class.forName keeps its old value.

write

write(debug_info: bool = ...) -> bytes

The model as the bytes of a DEX file.

The model is checked first. The same model always gives the same bytes.

Parameters:

Name Type Description Default
debug_info bool

Write line numbers and local variable names. False strips them.

...

Raises:

Type Description
InvalidModelError

The model breaks rules of the format. Its problems lists them.

PoolOverflowError

A table has more entries than the format allows.

VersionTooLowError

A feature needs a higher version.

write_file

write_file(path: str, debug_info: bool = ...) -> None

Write the model to a DEX file.

Parameters:

Name Type Description Default
path str

The file to write.

required
debug_info bool

Write line numbers and local variable names. False strips them.

...

Raises:

Type Description
OSError

The file cannot be created or written.

InvalidModelError

The model breaks rules of the format. Its problems lists them.

PoolOverflowError

A table has more entries than the format allows.

VersionTooLowError

A feature needs a higher version.

ClassInfo

A summary of one class in a Model.

descriptor property

descriptor: str

The class descriptor.

access_flags property

access_flags: AccessFlags

The class's access flags.

superclass property

superclass: Optional[str]

Descriptor of the superclass, or None for java.lang.Object.

source_file property

source_file: Optional[str]

The source file name recorded for the class, or None.

interfaces property

interfaces: list[str]

Descriptors of the interfaces the class implements directly, in declaration order. For an interface, these are the interfaces it extends.

field_count property

field_count: int

Number of fields the class declares.

method_count property

method_count: int

Number of methods the class defines.

PoolStats

How many entries each table of the written DEX file would have.

A DEX file can hold at most 65,535 types and prototypes, and at most 65,536 fields, methods, method handles and call sites. Use these numbers to decide when a file is full.

strings property

strings: int

Number of strings.

types property

types: int

Number of types. At most 65,535.

protos property

protos: int

Number of method prototypes. At most 65,535.

fields property

fields: int

Number of field identities. At most 65,536.

methods property

methods: int

Number of method identities. At most 65,536.

method_handles property

method_handles: int

Number of method handles.

call_sites property

call_sites: int

Number of call sites.

Problem

One rule of the DEX format that a model breaks.

where property

where: str

Where the problem is, such as Lcom/a/B;->run()V, item 12. Empty for a problem with the whole file.

message property

message: str

What is wrong.