Skip to content

APK, manifest, resources, signing

The package side of an APK. See the APK metadata guide.

Apk

An Android package: the archive, its manifest, its resources and its signatures.

The code is a separate object. analysis() returns it. The manifest, the resources and the signing information are each decoded on the first read and kept.

manifest property

manifest: Optional[Manifest]

The decoded manifest, or None when the APK has none or it cannot be decoded.

resources property

resources: Optional[ResourceTable]

The decoded resource table, or None when the APK has none or it cannot be decoded.

signing property

signing: SigningInfo

The signature schemes and certificates. An unsigned APK gives an object with is_signed false.

open staticmethod

open(path: str) -> Apk

Open an APK file.

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.

from_bytes staticmethod

from_bytes(
    data: ReadableBuffer, label: str = "<apk>"
) -> Apk

Open an APK from its bytes in memory, for example bytes downloaded from the network. The bytes get the same checks as a file given to open, and everything else works the same.

The APK keeps its own copy of the bytes, so data may change or be freed afterwards.

Parameters:

Name Type Description Default
data ReadableBuffer

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

required
label str

A name for the bytes, used at the start of error messages where open puts the path, for example the name of the file the bytes came from. It is also used by the errors of analysis.

'<apk>'

Raises:

Type Description
InvalidApkError

The bytes are not a ZIP archive.

entries

entries() -> list[str]

The name of every entry in the archive, in the order of the archive's table of contents (the ZIP central directory).

Each call reads the whole table of contents again and makes a new list, so keep the result when you need it more than once.

read

read(name: str) -> Optional[bytes]

The uncompressed bytes of one archive entry, or None when there is no entry with this name.

resolve_string

resolve_string(reference_id: int) -> Optional[str]

The value of a string resource, through resources. None when there is no resource table or the id is not a string resource.

analysis

analysis(threads: int = 0) -> AnalysisContext

The code of the APK: an AnalysisContext over every classes*.dex entry, loaded from the bytes the APK already holds.

The context is built on the first call and kept, one for each value of threads. A later call with the same value returns the same context, so its call graph and other structures are built once. A call with another value builds a second context, which holds its own copy of the DEX files and gives the same answers. So use one value in a program.

Parameters:

Name Type Description Default
threads int

How many threads to use, as for AnalysisContext.from_apk. 0 lets the library choose. 1 keeps all work, the call graph included, on the calling thread.

0

Raises:

Type Description
InvalidApkError

The APK has no classes.dex, or an entry is damaged.

InvalidDexError

One of its DEX files is not valid.

Manifest

The decoded AndroidManifest.xml.

Attributes the manifest may leave out are None when they are not set.

A boolean or integer attribute can be written as a resource reference, such as android:debuggable="@bool/debug". Its property then holds the value the APK's resource table gives, in the default configuration, and the matching ..._reference property holds the reference id. When the reference cannot be resolved, the value is None and the ..._reference property is still set: the attribute is written, but its value is unknown. A ..._reference property is None for a plain value or a missing attribute.

package property

package: str

The app's package name, from the package attribute of <manifest>.

version_code property

version_code: Optional[int]

android:versionCode, or None.

version_code_reference property

version_code_reference: Optional[int]

The resource id when android:versionCode is a reference, or None.

version_name property

version_name: Optional[str]

android:versionName, or None.

min_sdk property

min_sdk: Optional[int]

android:minSdkVersion of <uses-sdk>, or None.

min_sdk_reference property

min_sdk_reference: Optional[int]

The resource id when android:minSdkVersion is a reference, or None.

target_sdk property

target_sdk: Optional[int]

android:targetSdkVersion of <uses-sdk>, or None.

target_sdk_reference property

target_sdk_reference: Optional[int]

The resource id when android:targetSdkVersion is a reference, or None.

permissions property

permissions: list[str]

The permissions the app asks for: the name of each <uses-permission>.

declared_permissions property

declared_permissions: list[str]

The permissions the app itself declares: the name of each <permission>.

components property

components: list[Component]

Every activity, service, receiver and provider the manifest declares.

launcher_activity property

launcher_activity: Optional[str]

The name of the activity the launcher starts, or None when there is none.

debuggable property

debuggable: Optional[bool]

android:debuggable of <application>, or None when it is not set.

debuggable_reference property

debuggable_reference: Optional[int]

The resource id when android:debuggable is a reference, or None.

allow_backup property

allow_backup: Optional[bool]

android:allowBackup of <application>, or None when it is not set.

allow_backup_reference property

allow_backup_reference: Optional[int]

The resource id when android:allowBackup is a reference, or None.

declared_permission_protection_level

declared_permission_protection_level(
    name: str,
) -> Optional[int]

The android:protectionLevel of a permission the app declares, as the raw Android value, or None when the app does not declare this permission.

It is also None when the level is a resource reference that could not be resolved. Such a permission does not count as a guard for guarded_by_signature_permission. A declaration without the attribute is normal (0).

The low four bits are the base level: 0 normal, 1 dangerous, 2 signature, 3 signatureOrSystem. Higher bits are flags.

components_of

components_of(kind: ComponentKind) -> list[Component]

The components of one kind.

Component

An activity, a service, a broadcast receiver or a content provider declared in the manifest.

kind property

Which of the four kinds of component this is.

name property

name: str

The component's class name with its package, such as com.example.MainActivity. A short name in the manifest, such as .MainActivity, is completed with the app's package.

exported property

exported: Optional[bool]

The android:exported attribute: True or False as the manifest writes it, or as the resource it refers to says.

It is None when the attribute is missing, or when it is a resource reference that could not be resolved; then exported_reference is set. is_exported gives the effective answer.

exported_reference property

exported_reference: Optional[int]

The resource id when android:exported is written as a reference, such as @bool/is_exported (or a theme attribute, ?attr/name), whether it was resolved or not. None for a plain value or a missing attribute.

exported is None with exported_reference set means the value is unknown.

permission property

permission: Optional[str]

The permission a caller must hold to use the component, or None.

intent_filters property

intent_filters: list[IntentFilter]

The component's intent filters.

guarded_by_signature_permission property

guarded_by_signature_permission: bool

True when permission is a permission this app declares with the signature or signatureOrSystem protection level. Only apps signed with the same key can hold such a permission.

is_exported property

is_exported: bool

Whether other apps can address the component: the exported attribute when the manifest sets it, otherwise true when the component has an intent filter.

When the attribute is a reference that could not be resolved, its value is unknown and this is true: the component may be exported.

is_reachable property

is_reachable: bool

Whether an app from another developer can reach the component: it is exported, and it is not guarded by a signature-level permission that this app declares.

A component guarded by a normal or dangerous permission stays reachable, because any app can request those. So does a component guarded by a permission the manifest does not declare, because its protection level is not known.

IntentFilter

An <intent-filter> of a component.

actions property

actions: list[str]

The android:name of each <action>.

categories property

categories: list[str]

The android:name of each <category>.

is_launcher property

is_launcher: bool

True for the filter that puts an activity in the launcher: action android.intent.action.MAIN with category android.intent.category.LAUNCHER.

has_action

has_action(name: str) -> bool

True when the filter lists this action.

ComponentKind

Bases: Enum

The kind of a component declared in the manifest.

ACTIVITY class-attribute instance-attribute

ACTIVITY = 0

An <activity>: a screen of the app.

SERVICE class-attribute instance-attribute

SERVICE = 1

A <service>: work that runs without a screen.

RECEIVER class-attribute instance-attribute

RECEIVER = 2

A <receiver>: a broadcast receiver.

PROVIDER class-attribute instance-attribute

PROVIDER = 3

A <provider>: a content provider.

ResourceTable

The decoded resources.arsc table of an APK.

It maps resource ids to names and to string, boolean and integer values. When a resource has several variants, for example one per language, the value of the default configuration is used. A resource whose value refers to another resource is followed to the end.

empty property

empty: bool

True when the table has no entries.

name_of

name_of(id: int) -> Optional[ResourceName]

The name of the resource with this id, or None when the table has no such id.

resolve_string

resolve_string(id: int) -> Optional[str]

The value of a string resource, or None when the id is missing or its value is not a string.

resolve_bool

resolve_bool(id: int) -> Optional[bool]

The value of a resource as a boolean: a bool resource, or any integer-typed value (nonzero is true), as Android reads it. None when the id is missing or its value is of another type, such as a string.

resolve_int

resolve_int(id: int) -> Optional[int]

The value of a resource as an integer: an integer resource, or any other integer-typed value, read as its signed 32-bit data. None when the id is missing or its value is of another type.

id_of

id_of(type: str, entry: str) -> Optional[int]

The id of the resource with this type and entry name, such as ("string", "app_name"), or None. It scans the table, so it is meant for single lookups, not for loops.

ResourceName

The full name of a resource: package:type/entry.

It is a plain value: two are equal when their content is equal, and it can be a dictionary key or a set member.

package property

package: str

The package, such as com.example.app.

type property

type: str

The resource type, such as string or drawable.

entry property

entry: str

The entry name, such as app_name.

SigningInfo

Which signature schemes an APK carries, and the certificates in them.

This is identification only. The signatures and digests are not verified.

v1 property

v1: bool

True when the APK has a JAR signature: a META-INF/*.RSA, *.DSA or *.EC file.

v2 property

v2: bool

True when the APK has an APK Signature Scheme v2 block.

v3 property

v3: bool

True when the APK has an APK Signature Scheme v3 or v3.1 block.

certificates property

certificates: list[Certificate]

The signing certificates of all schemes. A certificate that appears in several schemes is listed once. Empty for an unsigned APK.

is_signed property

is_signed: bool

True when at least one scheme is present.

Certificate

An X.509 certificate that signed an APK.

der property

der: bytes

The certificate in its binary (DER) form.

subject property

subject: str

Who the certificate is for, such as C=US, O=Example, CN=App.

issuer property

issuer: str

Who issued the certificate, in the same form. Equal to subject for a self-signed one.

serial_hex property

serial_hex: str

The serial number, in lowercase hex.

not_before property

not_before: str

Start of the validity period, as YYYY-MM-DDTHH:MM:SSZ.

not_after property

not_after: str

End of the validity period, as YYYY-MM-DDTHH:MM:SSZ.

sha1_hex property

sha1_hex: str

SHA-1 fingerprint of the certificate, in lowercase hex.

sha256_hex property

sha256_hex: str

SHA-256 fingerprint of the certificate, in lowercase hex. This is the usual way to identify a signing certificate.

self_signed property

self_signed: bool

True when the subject and the issuer are the same.