Skip to content

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 classes.dex.

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

strings() -> list[str]

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().

find_class

find_class(descriptor: str) -> Optional[Class]

The class with this descriptor, such as Lcom/example/Foo;, or None when no loaded file defines it.

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

descriptor: str

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

package: str

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.

annotations property

annotations: list[Annotation]

The annotations on the class.

access_flags property

access_flags: AccessFlags

The access flags. The is_... properties read single flags of it.

source_file property

source_file: Optional[str]

The source file name recorded for the class, such as Foo.java, or None.

is_public property

is_public: bool

True when the class is public.

is_abstract property

is_abstract: bool

True when the class is abstract.

is_interface property

is_interface: bool

True when the class is an interface.

is_final property

is_final: bool

True when the class is final.

is_enum property

is_enum: bool

True when the class is an enum.

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.

descriptor property

descriptor: str

Descriptor of the class, such as Ljava/lang/Object;.

is_resolved property

is_resolved: bool

True when the class is defined in a loaded file.

resolve

resolve() -> Optional[Class]

The Class, or None when the class is not defined in a loaded file.

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

name: str

The method name. A constructor is <init>, and the static initializer is <clinit>.

declaring_class property

declaring_class: ClassRef

The class that defines the method.

id property

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

proto: str

The prototype: the parameter types inside parentheses, then the return type, such as "(Ljava/lang/String;I)V". It is the proto of id.

return_type property

return_type: str

Descriptor of the return type. V means void.

parameters property

parameters: list[str]

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

has_code: bool

True when the method has bytecode. False for abstract and native methods.

is_constructor property

is_constructor: bool

True for <init> and <clinit>.

is_static property

is_static: bool

True when the method is static.

is_public property

is_public: bool

True when the method is public.

is_private property

is_private: bool

True when the method is private.

is_abstract property

is_abstract: bool

True when the method is abstract.

is_native property

is_native: bool

True when the method is native.

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

register_count: int

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.

annotations property

annotations: list[Annotation]

The annotations on the method.

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.

type_uses

type_uses() -> list[TypeUse]

Every instruction in the method's code that names a type, 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.

name property

name: str

The field name.

declaring_class property

declaring_class: ClassRef

The class that declares the field.

type property

type: str

Descriptor of the field's type.

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.

is_public property

is_public: bool

True when the field is public.

is_static property

is_static: bool

True when the field is static.

is_final property

is_final: bool

True when the field is final.

annotations property

annotations: list[Annotation]

The annotations on the field.

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.

PUBLIC class-attribute instance-attribute

PUBLIC = 1

public.

PRIVATE class-attribute instance-attribute

PRIVATE = 2

private.

PROTECTED class-attribute instance-attribute

PROTECTED = 4

protected.

STATIC class-attribute instance-attribute

STATIC = 8

static.

FINAL class-attribute instance-attribute

FINAL = 16

final.

SYNCHRONIZED class-attribute instance-attribute

SYNCHRONIZED = 32

synchronized, on a method.

VOLATILE class-attribute instance-attribute

VOLATILE = 64

volatile, on a field. The same bit as BRIDGE.

BRIDGE class-attribute instance-attribute

BRIDGE = 64

A bridge method the compiler made. The same bit as VOLATILE.

TRANSIENT class-attribute instance-attribute

TRANSIENT = 128

transient, on a field. The same bit as VARARGS.

VARARGS class-attribute instance-attribute

VARARGS = 128

A method whose last parameter is .... The same bit as TRANSIENT.

NATIVE class-attribute instance-attribute

NATIVE = 256

native, on a method: its code is not in the DEX file.

INTERFACE class-attribute instance-attribute

INTERFACE = 512

The class is an interface.

ABSTRACT class-attribute instance-attribute

ABSTRACT = 1024

abstract.

STRICT class-attribute instance-attribute

STRICT = 2048

strictfp, on a method.

SYNTHETIC class-attribute instance-attribute

SYNTHETIC = 4096

Made by the compiler; not in the source code.

ANNOTATION class-attribute instance-attribute

ANNOTATION = 8192

The class is an annotation type.

ENUM class-attribute instance-attribute

ENUM = 16384

The class is an enum, or the field is one of its constants.

CONSTRUCTOR class-attribute instance-attribute

CONSTRUCTOR = 65536

A constructor, <init>, or the static initializer, <clinit>.

DECLARED_SYNCHRONIZED class-attribute instance-attribute

DECLARED_SYNCHRONIZED = 131072

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

class_descriptor: str

Descriptor of the class the method belongs to, such as Ljava/lang/Object;.

name property

name: str

The method name, such as toString. A constructor is <init>.

proto property

proto: str

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__

__init__(
    class_descriptor: str, name: str, proto: str
) -> None

Make an identity.

Parameters:

Name Type Description Default
class_descriptor str

The class, such as Ljava/lang/Object;.

required
name str

The method name, such as toString or <init>.

required
proto str

The prototype, such as ()Ljava/lang/String;.

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.

class_descriptor property

class_descriptor: str

Descriptor of the class the field belongs to.

name property

name: str

The field name.

type property

type: str

Descriptor of the field's type.

__init__

__init__(
    class_descriptor: str, name: str, type: str
) -> None

Make an identity.

Parameters:

Name Type Description Default
class_descriptor str

The class, such as Ljava/lang/System;.

required
name str

The field name, such as out.

required
type str

Descriptor of the field's type, such as Ljava/io/PrintStream;.

required

MethodCall

One call in a method's code.

code_offset property

code_offset: int

Offset of the invoke instruction in the method's code, in code units.

target property

target: MethodId

The called method, exactly as the bytecode names it. Inheritance and virtual dispatch are not applied; the call graph does that.

kind property

kind: InvokeKind

The kind of invoke instruction.

FieldAccess

One read or write of a field in a method's code.

code_offset property

code_offset: int

Offset of the instruction in the method's code, in code units.

field property

field: FieldId

The field that is read or written.

is_write property

is_write: bool

True for a write, false for a read.

is_static property

is_static: bool

True when the instruction is a static field access.

StringLoad

One const-string instruction in a method's code.

code_offset property

code_offset: int

Offset of the instruction in the method's code, in code units.

value property

value: str

The string that is loaded.

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

code_offset: int

Offset of the instruction in the method's code, in code units.

descriptor property

descriptor: str

Descriptor of the type the instruction names.

kind property

What the instruction does with the type.

TryBlock

A range of a method's code that is covered by exception handlers.

start_offset property

start_offset: int

Offset of the first covered instruction, in code units.

end_offset property

end_offset: int

Offset just past the covered range, in code units.

handlers property

handlers: list[CatchHandler]

The typed catch clauses, in the order they are tried.

catch_all_offset property

catch_all_offset: Optional[int]

Offset of the handler that catches every exception, or None when there is none. A finally block is compiled to such a handler.

CatchHandler

One typed catch clause of a try block.

type_descriptor property

type_descriptor: str

Descriptor of the exception class the clause catches.

handler_offset property

handler_offset: int

Offset of the handler's first instruction, in code units.

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.

reg property

reg: int

The register that holds the argument. A wide argument also uses reg + 1.

type property

type: str

Descriptor of the argument's type. For this it is the class that defines the method.

is_this property

is_this: bool

True for the implicit this of an instance method.

is_wide property

is_wide: bool

True for long and double, which take two registers.