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
¶
The DEX format version write uses. write never raises it by itself; set it
when required_version() is higher.
__init__
¶
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: |
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. |
required_version
¶
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
¶
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
|
|
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
|
|
remove_class
¶
Remove the class with this descriptor. Returns whether a class was removed.
rename_types
¶
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
¶
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 |
PoolOverflowError
|
A table has more entries than the format allows. |
VersionTooLowError
|
A feature needs a higher |
write_file
¶
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 |
PoolOverflowError
|
A table has more entries than the format allows. |
VersionTooLowError
|
A feature needs a higher |
ClassInfo
¶
A summary of one class in a Model.
superclass
property
¶
Descriptor of the superclass, or None for java.lang.Object.
source_file
property
¶
The source file name recorded for the class, or None.
interfaces
property
¶
Descriptors of the interfaces the class implements directly, in declaration order. For an interface, these are the interfaces it extends.
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.