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: |
required |
label
|
str
|
A name for the bytes, used at the start of error messages
where |
'<apk>'
|
Raises:
| Type | Description |
|---|---|
InvalidApkError
|
The bytes are not a ZIP archive. |
entries
¶
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
¶
The uncompressed bytes of one archive entry, or None when there is no entry
with this name.
resolve_string
¶
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 |
0
|
Raises:
| Type | Description |
|---|---|
InvalidApkError
|
The APK has no |
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.
version_code_reference
property
¶
The resource id when android:versionCode is a reference, or None.
min_sdk_reference
property
¶
The resource id when android:minSdkVersion is a reference, or None.
target_sdk_reference
property
¶
The resource id when android:targetSdkVersion is a reference, or None.
permissions
property
¶
The permissions the app asks for: the name of each <uses-permission>.
declared_permissions
property
¶
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
¶
The name of the activity the launcher starts, or None when there is none.
debuggable
property
¶
android:debuggable of <application>, or None when it is not set.
debuggable_reference
property
¶
The resource id when android:debuggable is a reference, or None.
allow_backup
property
¶
android:allowBackup of <application>, or None when it is not set.
allow_backup_reference
property
¶
The resource id when android:allowBackup is a reference, or None.
declared_permission_protection_level
¶
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.
Component
¶
An activity, a service, a broadcast receiver or a content provider declared in the manifest.
name
property
¶
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
¶
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
¶
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
¶
The permission a caller must hold to use the component, or None.
guarded_by_signature_permission
property
¶
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
¶
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
¶
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.
is_launcher
property
¶
True for the filter that puts an activity in the launcher: action
android.intent.action.MAIN with category
android.intent.category.LAUNCHER.
ComponentKind
¶
Bases: Enum
The kind of a component declared in the manifest.
SERVICE
class-attribute
instance-attribute
¶
A <service>: work that runs without a screen.
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.
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
¶
The value of a string resource, or None when the id is missing or its value
is not a string.
resolve_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
¶
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
¶
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.
SigningInfo
¶
Which signature schemes an APK carries, and the certificates in them.
This is identification only. The signatures and digests are not verified.
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.