Context, classes, methods, fields¶
The code side: loading DEX files and walking classes and their members. See the code guide.
AnalysisContext
¶
The code of a program: one or more DEX files, loaded together.
It is the starting point for code analysis. Loading does little work. Class data, bytecode and the whole-program structures are built the first time they are read, and kept.
Every object that comes from a context keeps it alive, so you do not have to keep the context yourself.
call_graph
property
¶
call_graph: CallGraph
The call graph of the whole program. Built on the first read and kept.
class_hierarchy
property
¶
class_hierarchy: ClassHierarchy
The class hierarchy of the whole program. Built on the first read and kept.
xrefs
property
¶
xrefs: Xrefs
The cross-reference index of the whole program. Built on the first read and kept.
from_dex
staticmethod
¶
from_dex(path: str) -> AnalysisContext
Load one DEX file.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
The path does not exist. |
OSError
|
The path exists but cannot be read, for example a directory. |
InvalidDexError
|
The file is not a valid DEX file. |
from_dex_files
staticmethod
¶
from_dex_files(paths: list[str]) -> AnalysisContext
Load several DEX files into one context, for example the files of a multidex app that are already unpacked.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
A path does not exist. |
OSError
|
A path exists but cannot be read, for example a directory. |
InvalidDexError
|
A file is not a valid DEX file. |
from_apk
staticmethod
¶
from_apk(path: str, threads: int = 0) -> AnalysisContext
Load every top-level classes*.dex entry of an APK, in multidex order:
classes.dex, classes2.dex, classes3.dex and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The APK file. |
required |
threads
|
int
|
How many threads to use. 0 lets the library choose. 1 keeps all work on the calling thread; use it when your program already analyses several APKs in parallel. The threads are used to decompress the DEX files of a large APK, and later to build the call graph. |
0
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
The path does not exist. |
OSError
|
The path exists but cannot be read, for example a directory. |
InvalidApkError
|
The file is not a ZIP archive, or it has no |
InvalidDexError
|
One of its DEX files is not valid. |
classes
¶
classes() -> list[Class]
Every class of every loaded DEX file, in file order.
Each call makes a new list with a new Class object for every class, so keep
the result when you need it more than once. To get one class by its
descriptor, use find_class.
strings
¶
Every string of every loaded DEX file's string table.
This includes names of classes, methods and fields, not only the strings the
code loads. A string that is in two DEX files appears twice. The first call
decodes the whole table, and every call copies all of it into a new list, so
it is one of the more expensive calls: keep the result when you need it more
than once. For only the strings the code loads, use
xrefs.referenced_strings().
Class
¶
A class defined in a loaded DEX file.
Two Class objects are equal when they are the same class of the same analysis,
so a class can be a dictionary key or a set member. The same file loaded twice
gives two analyses, and their classes are not equal.
descriptor
property
¶
The class descriptor, such as Lcom/example/Foo;. A class has no simple name
here: Method.name and Field.name are simple names, and a class is named by
its descriptor.
package
property
¶
The package in Java source form, with . between its parts, such as
com.example: the same form as Manifest.package, so the two can be
compared directly. Empty for a class in the default package.
superclass
property
¶
superclass: Optional[ClassRef]
The direct superclass. None only for java.lang.Object itself.
interfaces
property
¶
interfaces: list[ClassRef]
The interfaces the class implements directly, in declaration order.
methods
property
¶
methods: list[Method]
The methods the class defines: first the direct methods (constructors, static and private methods), then the virtual methods. Inherited methods are not included.
fields
property
¶
fields: list[Field]
The fields the class declares: first the static fields, then the instance fields. Inherited fields are not included.
access_flags
property
¶
access_flags: AccessFlags
The access flags. The is_... properties read single flags of it.
source_file
property
¶
The source file name recorded for the class, such as Foo.java, or None.
ClassRef
¶
A reference to a class by its descriptor.
The class may or may not be in the loaded files. Superclasses and interfaces are given as references because they are often framework classes, which an app does not contain.
Two references are equal when they name the same class and come from the same
analysis, so a reference can be a dictionary key or a set member. A ClassRef
is never equal to a Class.
Method
¶
A method defined by a class in a loaded DEX file.
Two Method objects are equal when they are the same method of the same analysis,
so a method can be a dictionary key or a set member. The same file loaded twice
gives two analyses, and their methods are not equal.
name
property
¶
The method name. A constructor is <init>, and the static initializer is
<clinit>.
id
property
¶
id: MethodId
The method's identity: its class, name and prototype. It is the identity the
call graph and the cross-references use, so ctx.call_graph.find(method.id)
finds the method's node.
proto
property
¶
The prototype: the parameter types inside parentheses, then the return type,
such as "(Ljava/lang/String;I)V". It is the proto of id.
parameters
property
¶
Descriptors of the declared parameter types, in order. this is not
included.
access_flags
property
¶
access_flags: AccessFlags
The access flags. The is_... properties read single flags of it.
has_code
property
¶
True when the method has bytecode. False for abstract and native methods.
instructions
property
¶
instructions: InstructionView
The decoded bytecode, in order, as a read-only sequence. It is empty for a method without code.
The bytecode is decoded once and kept. The sequence copies nothing up front: each instruction is built when it is read.
cfg
property
¶
cfg: Cfg
The control-flow graph. It is built once and kept. Empty for a method without code.
register_count
property
¶
Number of registers in the method's frame. 0 for a method without code.
parameter_registers
property
¶
parameter_registers: list[ParameterRegister]
The incoming arguments and the registers they arrive in, with this first
for an instance method. Empty for a method without code.
try_blocks
property
¶
try_blocks: list[TryBlock]
The try ranges of the method and their handlers. Empty when it has none.
calls
¶
calls() -> list[MethodCall]
Every call in the method's code, in order. The targets are as the bytecode names them.
Each call goes through the method's bytecode again and makes a new list, so keep the result when you need it more than once.
field_accesses
¶
field_accesses() -> list[FieldAccess]
Every field read and write in the method's code, in order.
Each call goes through the method's bytecode again and makes a new list, so keep the result when you need it more than once.
string_loads
¶
string_loads() -> list[StringLoad]
Every const-string in the method's code, in order.
Each call goes through the method's bytecode again and makes a new list, so keep the result when you need it more than once.
Field
¶
A field declared by a class in a loaded DEX file.
Two Field objects are equal when they are the same field of the same analysis,
so a field can be a dictionary key or a set member. The same file loaded twice
gives two analyses, and their fields are not equal.
id
property
¶
id: FieldId
The field's identity: its class, name and type. It is the identity the
cross-references use, so ctx.xrefs.field_refs(field.id) finds the code that
reads or writes this field.
access_flags
property
¶
access_flags: AccessFlags
The access flags. The is_... properties read single flags of it.
initial_value
property
¶
initial_value: AnnotationValue
The constant a static field starts with.
None when the field is not static or has no recorded value. The field then
starts at the default of its type: zero, false or null.
AccessFlags
¶
Bases: IntFlag
The access flags of a class, a method or a field, as the DEX format stores them.
It is an enum.IntFlag, so it is also an int: code that tests bits by value
keeps working. Test a flag with in:
AccessFlags.PUBLIC in cls.access_flags.
Two pairs of flags share a bit, because the DEX format gives them the same
value: VOLATILE and BRIDGE (0x40), and TRANSIENT and VARARGS (0x80).
The first of each pair is for fields, the second for methods. In Python the
second name is an alias of the first, so AccessFlags.BRIDGE is
AccessFlags.VOLATILE.
VOLATILE
class-attribute
instance-attribute
¶
volatile, on a field. The same bit as BRIDGE.
BRIDGE
class-attribute
instance-attribute
¶
A bridge method the compiler made. The same bit as VOLATILE.
TRANSIENT
class-attribute
instance-attribute
¶
transient, on a field. The same bit as VARARGS.
VARARGS
class-attribute
instance-attribute
¶
A method whose last parameter is .... The same bit as TRANSIENT.
NATIVE
class-attribute
instance-attribute
¶
native, on a method: its code is not in the DEX file.
SYNTHETIC
class-attribute
instance-attribute
¶
Made by the compiler; not in the source code.
ENUM
class-attribute
instance-attribute
¶
The class is an enum, or the field is one of its constants.
CONSTRUCTOR
class-attribute
instance-attribute
¶
A constructor, <init>, or the static initializer, <clinit>.
DECLARED_SYNCHRONIZED
class-attribute
instance-attribute
¶
A method declared synchronized.
MethodId
¶
The identity of a method: its class, its name and its prototype.
It is a plain value. Two identities with the same three strings are equal, and an identity can be a dictionary key or a set member. An identity also exists for a method that is not in the loaded files, such as a framework method.
The prototype is part of the identity, so overloads of one name are different methods.
class_descriptor
property
¶
Descriptor of the class the method belongs to, such as Ljava/lang/Object;.
proto
property
¶
The prototype: the parameter types inside parentheses with nothing between
them, then the return type. String f(int, long) is
(IJ)Ljava/lang/String;.
__init__
¶
Make an identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_descriptor
|
str
|
The class, such as |
required |
name
|
str
|
The method name, such as |
required |
proto
|
str
|
The prototype, such as |
required |
FieldId
¶
The identity of a field: its class, its name and its type.
It is a plain value. Two identities with the same three strings are equal, and an identity can be a dictionary key or a set member.
__init__
¶
Make an identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_descriptor
|
str
|
The class, such as |
required |
name
|
str
|
The field name, such as |
required |
type
|
str
|
Descriptor of the field's type, such as |
required |
MethodCall
¶
FieldAccess
¶
One read or write of a field in a method's code.
code_offset
property
¶
Offset of the instruction in the method's code, in code units.
StringLoad
¶
TypeUse
¶
One instruction in a method's code that names a type: const-class,
check-cast, instance-of, new-instance, new-array or filled-new-array.
code_offset
property
¶
Offset of the instruction in the method's code, in code units.
TryBlock
¶
A range of a method's code that is covered by exception handlers.
handlers
property
¶
handlers: list[CatchHandler]
The typed catch clauses, in the order they are tried.
catch_all_offset
property
¶
Offset of the handler that catches every exception, or None when there is
none. A finally block is compiled to such a handler.
CatchHandler
¶
ParameterRegister
¶
An incoming argument of a method and the register it arrives in.
Dalvik puts the arguments in the last registers of the method's frame. For an
instance method the implicit this comes first, then the declared parameters in
order.
type
property
¶
Descriptor of the argument's type. For this it is the class that defines
the method.