The Android Pony EXpress (APEX) container format was introduced in Android 10 and it's used in the install flow for lower-level system modules. This format facilitates the updates of system components that don't fit into the standard Android application model. Some example components are native services and libraries, hardware abstraction layers (HALs), runtime (ART), and class libraries.
The term "APEX" can also refer to an APEX file.
Background
Although Android supports updates of modules that fit within the standard app model (for example, services, activities) via package installer apps (such as the Google Play Store app), using a similar model for lower-level OS components has the following drawbacks:
- APK-based modules can't be used early in the boot sequence. The package manager is the central repository of information about apps and can only be started from the activity manager, which becomes ready in a later stage of the boot procedure.
- The APK format (particularly the manifest) is designed for Android apps and system modules aren't always a good fit.
Design
This section describes the high-level design of the APEX file format and the APEX manager, which is a service that manages APEX files.
For more information on why this design for APEX was selected, see Alternatives considered when developing APEX.
APEX format
This is the format of an APEX file.
Figure 1. APEX file format
At the top level, an APEX file is a zip file in which files are stored uncompressed and located at 4 KB boundaries.
The four files in an APEX file are:
apex_manifest.json
AndroidManifest.xml
apex_payload.img
apex_pubkey
The apex_manifest.json
file contains the package name and version, which
identify an APEX file. This is an
ApexManifest
protocol buffer in JSON format.
The AndroidManifest.xml
file allows the APEX file to use APK-related tools and
infrastructure such as ADB, PackageManager, and package installer apps (such as
Play Store). For example, the APEX file can use an existing tool such as aapt
to inspect basic metadata from the file. The file contains package name and
version information. This information is generally also available in
apex_manifest.json
.
apex_manifest.json
is recommended over AndroidManifest.xml
for new code and
systems that deal with APEX. AndroidManifest.xml
might contain additional
targeting information that can be used by the existing app publishing tools.
apex_payload.img
is an ext4 file system image backed by dm-verity. The image
is mounted at runtime via a loopback device. Specifically, the hash tree and
metadata block are created using the libavb
library. The file system payload
isn't parsed (because the image should be mountable in place). Regular files are
included inside the apex_payload.img
file.
apex_pubkey
is the public key used to sign the file system image. At runtime,
this key ensures that the downloaded APEX is signed with the same entity
that signs the same APEX in the built-in partitions.
APEX naming guidelines
To help prevent naming conflicts between new APEXes as the platform advances, use the following naming guidelines:
com.android.*
- Reserved for AOSP APEXes. Not unique to any company or device.
com.<companyname>.*
- Reserved for a company. Potentially used by multiple devices from that company.
com.<companyname>.<devicename>.*
- Reserved for APEXes unique to a specific device (or subset of devices).
APEX manager
The APEX manager (or apexd
) is a standalone native process responsible for
verifying, installing, and uninstalling APEX files. This process is launched and
is ready early in the boot sequence. APEX files are normally pre-installed on
the device under /system/apex
. The APEX manager defaults to using these
packages if no updates are available.
The update sequence of an APEX uses the PackageManager class and is as follows.
- An APEX file is downloaded via a package installer app, ADB, or other source.
- The package manager starts the installation procedure. Upon recognizing that the file is an APEX, the package manager transfers control to the APEX manager.
- The APEX manager verifies the APEX file.
- If the APEX file is verified, the internal database of the APEX manager is updated to reflect that the APEX file gets activated at next boot.
- The install requestor receives a broadcast upon successful package verification.
- To continue the installation, the system must be rebooted.
At next boot, the APEX manager starts, reads the internal database, and does the following for each APEX file listed:
- Verifies the APEX file.
- Creates a loopback device from the APEX file.
- Creates a device mapper block device on top of the loopback device.
- Mounts the device mapper block device onto a unique path (for example,
/apex/name@ver
).
When all APEX files listed in the internal database are mounted, the APEX manager provides a binder service for other system components to query information about the installed APEX files. For example, the other system components can query the list of APEX files installed in the device or query the exact path where a specific APEX is mounted, so the files can be accessed.
APEX files are APK files
APEX files are valid APK files because they're signed zip archives (using the
APK signature scheme) containing an AndroidManifest.xml
file. This allows APEX
files to use the infrastructure for APK files, such as a package installer app,
the signing utility, and the package manager.
The AndroidManifest.xml
file inside an APEX file is minimal, consisting of the
package name
, versionCode
, and optional targetSdkVersion
, minSdkVersion
,
and maxSdkVersion
for fine-grained targeting. This information allows APEX
files to be delivered via existing channels such as package installer apps and
ADB.
File types supported
The APEX format supports these file types:
- Native shared libs
- Native executables
- JAR files
- Data files
- Config files
This doesn't mean that APEX can update all of these file types. Whether a file type can be updated depends on the platform and how stable the definitions of the interfaces for the files types are.
Signing options
APEX files are signed in two ways. First, the apex_payload.img
(specifically,
the vbmeta descriptor appended to apex_payload.img
) file is signed with a key.
Then, the entire APEX is signed using the
APK signature scheme v3. Two different keys are used
in this process.
On the device side, a public key corresponding to the private key used to sign the vbmeta descriptor is installed. The APEX manager uses the public key to verify APEXes that are requested to be installed. Each APEX must be signed with different keys and is enforced both at build time and at runtime.
APEX in built-in partitions
APEX files can be located in built-in partitions such as /system
. The
partition is already over dm-verity, so the APEX files are mounted directly
over the loopback device.
If an APEX is present in a built-in partition, the APEX can be updated by
providing an APEX package with the same package name and a greater than or equal
to version code. The new APEX is stored in /data
and, similar to APKs, the
newly installed version shadows the version already present in the built-in
partition. But unlike APKs, the newly installed version of the APEX is only
activated after reboot.
Kernel requirements
To support APEX mainline modules on an Android device, the following Linux kernel features are required: the loopback driver and dm-verity. The loopback driver mounts the file system image in an APEX module and dm-verity verifies the APEX module.
The performance of the loopback driver and dm-verity is important in achieving good system performance when using APEX modules.
Supported kernel versions
APEX mainline modules are supported on devices using kernel versions 4.4 or higher. New devices launching with Android 10 or higher must use kernel version 4.9 or higher to support APEX modules.
Required kernel patches
The required kernel patches for supporting APEX modules are included in the Android common tree. To get the patches to support APEX, use the latest version of the Android common tree.
Kernel version 4.4
This version is only supported for devices that are upgraded from Android 9 to
Android 10 and want to support APEX modules. To get the
required patches, a down-merge from the android-4.4
branch is strongly
recommended. The following is a list of the required individual patches
for kernel version 4.4.
- UPSTREAM: loop: add ioctl for changing logical block size (4.4)
- BACKPORT: block/loop: set hw_sectors (4.4)
- UPSTREAM: loop: Add LOOP_SET_BLOCK_SIZE in compat ioctl (4.4)
- ANDROID: mnt: Fix next_descendent (4.4)
- ANDROID: mnt: remount should propagate to slaves of slaves (4.4)
- ANDROID: mnt: Propagate remount correctly (4.4)
- Revert "ANDROID: dm verity: add minimum prefetch size" (4.4)
- UPSTREAM: loop: drop caches if offset or block_size are changed (4.4)
Kernel versions 4.9/4.14/4.19
To get the required patches for kernel versions 4.9/4.14/4.19, down-merge from
the android-common
branch.
Required kernel configuration options
The following list shows the base configuration requirements for supporting APEX modules that were introduced in Android 10. The items with an asterisk (*) are existing requirements from Android 9 and lower.
(*) CONFIG_AIO=Y # AIO support (for direct I/O on loop devices)
CONFIG_BLK_DEV_LOOP=Y # for loop device support
CONFIG_BLK_DEV_LOOP_MIN_COUNT=16 # pre-create 16 loop devices
(*) CONFIG_CRYPTO_SHA1=Y # SHA1 hash for DM-verity
(*) CONFIG_CRYPTO_SHA256=Y # SHA256 hash for DM-verity
CONFIG_DM_VERITY=Y # DM-verity support
Kernel command-line parameter requirements
To support APEX, ensure the kernel command-line parameters meet the following requirements:
loop.max_loop
must NOT be setloop.max_part
must be <= 8
Build an APEX
This section describes how to build an APEX using the Android build system.
The following is an example of Android.bp
for an APEX named apex.test
.
apex {
name: "apex.test",
manifest: "apex_manifest.json",
file_contexts: "file_contexts",
// libc.so and libcutils.so are included in the apex
native_shared_libs: ["libc", "libcutils"],
binaries: ["vold"],
java_libs: ["core-all"],
prebuilts: ["my_prebuilt"],
compile_multilib: "both",
key: "apex.test.key",
certificate: "platform",
}
apex_manifest.json
example:
{
"name": "com.android.example.apex",
"version": 1
}
file_contexts
example:
(/.*)? u:object_r:system_file:s0
/sub(/.*)? u:object_r:sub_file:s0
/sub/file3 u:object_r:file3_file:s0
File types and locations in APEX
File type | Location in APEX |
---|---|
Shared libraries | /lib and /lib64 (/lib/arm for
translated arm in x86) |
Executables | /bin |
Java libraries | /javalib |
Prebuilts | /etc |
Transitive dependencies
APEX files automatically include transitive dependencies of native shared libs
or executables. For example, if libFoo
depends on libBar
, the two libs are
included when only libFoo
is listed in the native_shared_libs
property.
Handle multiple ABIs
Install the native_shared_libs
property for both primary and secondary
application binary interfaces (ABIs) of the device. If an APEX targets devices
with a single ABI (that is, 32 bit only or 64 bit only), only libraries with the
corresponding ABI are installed.
Install the binaries
property only for the primary ABI of the device as
described below:
- If the device is 32 bit only, only the 32-bit variant of the binary is installed.
- If the device is 64 bit only, then only the 64-bit variant of the binary is installed.
To add fine-grained control over the ABIs of the native libraries and binaries,
use the
multilib.[first|lib32|lib64|prefer32|both].[native_shared_libs|binaries]
properties.
first
: Matches the primary ABI of the device. This is the default for binaries.lib32
: Matches the 32-bit ABI of the device, if supported.lib64
: Matches the 64-bit ABI of the device, it supported.prefer32
: Matches the 32-bit ABI of the device, if supported. If the 32-bit ABI isn't supported, matches the 64-bit ABI.both
: Matches both ABIs. This is the default fornative_shared_libraries
.
The java
, libraries
, and prebuilts
properties are ABI-agnostic.
This example is for a device that supports 32/64 and doesn't prefer 32:
apex {
// other properties are omitted
native_shared_libs: ["libFoo"], // installed for 32 and 64
binaries: ["exec1"], // installed for 64, but not for 32
multilib: {
first: {
native_shared_libs: ["libBar"], // installed for 64, but not for 32
binaries: ["exec2"], // same as binaries without multilib.first
},
both: {
native_shared_libs: ["libBaz"], // same as native_shared_libs without multilib
binaries: ["exec3"], // installed for 32 and 64
},
prefer32: {
native_shared_libs: ["libX"], // installed for 32, but not for 64
},
lib64: {
native_shared_libs: ["libY"], // installed for 64, but not for 32
},
},
}
vbmeta signing
Sign each APEX with different keys. When a new key is required, create a
public-private key pair and make an apex_key
module. Use the key
property to
sign the APEX using the key. The public key is automatically included in the
APEX with the name avb_pubkey
.
# create an rsa key pairopenssl genrsa -out foo.pem 4096
# extract the public key from the key pairavbtool extract_public_key --key foo.pem --output foo.avbpubkey
# in Android.bpapex_key { name: "apex.test.key", public_key: "foo.avbpubkey", private_key: "foo.pem", }
In the above example, the name of the public key (foo
) becomes the ID of the
key. The ID of the key used to sign an APEX is written in the APEX. At runtime,
apexd
verifies the APEX using a public key with the same ID in the device.
APEX signing
Sign APEXes in the same way as you sign APKs. Sign APEXes twice; once for the
mini file system (apex_payload.img
file) and once for the entire file.
To sign an APEX at the file-level, set the certificate
property in one of
these three ways:
- Not set: If no value is set, the APEX is signed with the certificate located
at
PRODUCT_DEFAULT_DEV_CERTIFICATE
. If no flag is set, the path defaults tobuild/target/product/security/testkey
. <name>
: The APEX is signed with the<name>
certificate in the same directory asPRODUCT_DEFAULT_DEV_CERTIFICATE
.:<name>
: The APEX is signed with the certificate that is defined by the Soong module named<name>
. The certificate module can be defined as follows.
android_app_certificate {
name: "my_key_name",
certificate: "dir/cert",
// this will use dir/cert.x509.pem (the cert) and dir/cert.pk8 (the private key)
}
Install an APEX
To install an APEX, use ADB.
adb install apex_file_name
adb reboot
If supportsRebootlessUpdate
is set to true
in apex_manifest.json
and the
currently installed APEX is unused (for instance, any services it contains have
been stopped), then a new APEX can be installed without a reboot with the
--force-non-staged
flag.
adb install --force-non-staged apex_file_name
Use an APEX
After reboot, the APEX is mounted at the /apex/<apex_name>@<version>
directory. Multiple versions of the same APEX can be mounted at the same time.
Among the mount paths, the one that corresponds to the latest version is
bind-mounted at /apex/<apex_name>
.
Clients can use the bind-mounted path to read or execute files from APEX.
APEXes are typically used as follows:
- An OEM or ODM preloads an APEX under
/system/apex
when the device is shipped. - Files in the APEX are accessed via the
/apex/<apex_name>/
path. - When an updated version of the APEX is installed in
/data/apex
, the path points to the new APEX after reboot.
Update a service with an APEX
To update a service using an APEX:
Mark the service in the system partition as updatable. Add the option
updatable
to the service definition./system/etc/init/myservice.rc: service myservice /system/bin/myservice class core user system ... updatable
Create a new
.rc
file for the updated service. Use theoverride
option to redefine the existing service./apex/my.apex/etc/init.rc: service myservice /apex/my.apex/bin/myservice class core user system ... override
Service definitions can only be defined in the .rc
file of an APEX. Action
triggers aren't supported in APEXes.
If a service marked as updatable starts before the APEXes are activated, the start is delayed until the activation of the APEXes is complete.
Configure system to support APEX updates
Set the following system property to true
to support APEX file updates.
<device.mk>:
PRODUCT_PROPERTY_OVERRIDES += ro.apex.updatable=true
BoardConfig.mk:
TARGET_FLATTEN_APEX := false
or just
<device.mk>:
$(call inherit-product, $(SRC_TARGET_DIR)/product/updatable_apex.mk)
Flattened APEX
For legacy devices, it's sometimes impossible or infeasible to update the old
kernel to fully support APEX. For example, the kernel might have been built
without CONFIG_BLK_DEV_LOOP=Y
, which is crucial for mounting the file system
image inside an APEX.
Flattened APEX is a specially built APEX that can be activated on devices with
a legacy kernel. Files in a flattened APEX are directly installed to a directory
under the built-in partition. For example, lib/libFoo.so
in a flattened APEX
my.apex
is installed to /system/apex/my.apex/lib/libFoo.so
.
Activating a flattened APEX doesn't involve the loop device. The entire
directory /system/apex/my.apex
is directly bind-mounted to /apex/name@ver
.
Flattened APEXes can't be updated by downloading updated versions of the APEXes from network because the downloaded APEXes can't be flattened. Flattened APEXes can be updated only via a regular OTA.
Flattened APEX is the default configuration. This means that all APEXes are by default flattened unless you explicitly configure your device to build non-flattened APEXes to support APEX updates (as explained above).
Mixing flattened and non-flattened APEXes in a device is NOT
supported. APEXes in a device must either be all non-flattened or all flattened.
This is especially important when shipping pre-signed APEX prebuilts for
projects such as Mainline. APEXes that aren't presigned (that is, built from
the source) should also be non-flattened and signed with proper keys. The
device should inherit from updatable_apex.mk
as explained in
Updating a service with an APEX.
Compressed APEXes
Android 12 and later feature APEX compression for reducing the storage impact of updatable APEX packages. After an update to an APEX is installed, although its pre-installed version isn't used anymore, it still occupies the same amount of space. That occupied space remains unavailable.
APEX compression minimizes this storage impact by using a highly compressed set
of APEX files on read-only partitions (such as the /system
partition). Android
12 and later use a DEFLATE zip compression algorithm.
Compression doesn't provide optimization to the following:
Bootstrap APEXes that are required to be mounted very early in the boot sequence.
Nonupdatable APEXes. Compression is only beneficial if an updated version of an APEX is installed on the
/data
partition. A full list of updatable APEXes is available on the Modular System Components page.Dynamic shared libs APEXes. Since
apexd
always activates both versions of such APEXes (pre-installed and upgraded), compressing them doesn't add value.
Compressed APEX file format
This is the format of a compressed APEX file.
Figure 2. Compressed APEX file format
At the top level, a compressed APEX file is a zip file containing the original apex file in deflated form with a compression level of 9, and with other files stored uncompressed.
Four files comprise an APEX file:
original_apex
: deflated with compression level of 9 This is the original, uncompressed APEX file.apex_manifest.pb
: stored onlyAndroidManifest.xml
: stored onlyapex_pubkey
: stored only
The apex_manifest.pb
, AndroidManifest.xml
, and apex_pubkey
files are
copies of their corresponding files in original_apex
.
Build compressed APEX
Compressed APEX can be built using the apex_compression_tool.py
tool located at
system/apex/tools
.
Several parameters related to APEX compression are available in the build system.
In Android.bp
whether an APEX file is compressible is controlled by the
compressible
property:
apex {
name: "apex.test",
manifest: "apex_manifest.json",
file_contexts: "file_contexts",
compressible: true,
}
A PRODUCT_COMPRESSED_APEX
product flag controls whether a system image built
from source must contain compressed APEX files.
For local experimentation you can force a build to compress APEXes by setting
OVERRIDE_PRODUCT_COMPRESSED_APEX=
to true
.
Compressed APEX files generated by the build system have the .capex
extension.
The extension makes it easier to distinguish between compressed and uncompressed
versions of an APEX file.
Supported compression algorithms
Android 12 only supports deflate-zip compression.
Activate a compressed APEX file during boot
Before a compressed APEX can be activated, the original_apex
file inside it's
decompressed into the /data/apex/decompressed
directory. The resulting
decompressed APEX file is hard-linked to the /data/apex/active
directory.
Consider the following example as an illustration of the process described above.
Consider /system/apex/com.android.foo.capex
as a compressed APEX being
activated, with versionCode 37.
- The
original_apex
file inside/system/apex/com.android.foo.capex
is decompressed into/data/apex/decompressed/com.android.foo@37.apex
. restorecon /data/apex/decompressed/com.android.foo@37.apex
is performed to verify that it has a correct SELinux label.- Verification checks are performed on
/data/apex/decompressed/com.android.foo@37.apex
to ensure its validity:apexd
checks the public key bundled in/data/apex/decompressed/com.android.foo@37.apex
to verify that it's equal to the one bundled in/system/apex/com.android.foo.capex
. - The
/data/apex/decompressed/com.android.foo@37.apex
file is hard-linked to the/data/apex/active/com.android.foo@37.apex
directory. - The regular activation logic for uncompressed APEX files is performed on
/data/apex/active/com.android.foo@37.apex
.
Interaction with OTA
Compressed APEX files have implications on OTA delivery and application. Since an OTA update might contain a compressed APEX file with a higher version level than what's active on a device, a certain amount of free space must be reserved before a device is rebooted to apply an OTA update.
To support the OTA system, apexd
exposes these two binder APIs:
calculateSizeForCompressedApex
- calculates the size required to decompress APEX files in an OTA package. This can be used to verify that a device has enough space before an OTA gets downloaded.reserveSpaceForCompressedApex
- reserves space on the disk for future use byapexd
for decompressing compressed APEX files inside the OTA package.
In the case of an A/B OTA update, apexd
attempts decompression in the
background as part of the postinstall OTA routine. If decompression fails,
apexd
performs the decompression during the boot that applies the OTA
update.
Alternatives considered when developing APEX
Here are some options that AOSP considered when designing the APEX file format, and why they were either included or excluded.
Regular package management systems
Linux distributions have package management systems like dpkg
and rpm
,
which are powerful, mature, and robust. However, they weren't
adopted for APEX because they can't protect the packages after
installation. Verification is performed only when packages are being installed.
Attackers can break the integrity of the installed packages, unnoticed. This is
a regression for Android where all system components were stored in read-only
file systems whose integrity is protected by dm-verity for every I/O. Any
tampering to system components must either be prohibited, or be detectable so
that the device can refuse to boot if compromised.
dm-crypt for integrity
The files in an APEX container are from built-in partitions (for example, the
/system
partition) that are protected by dm-verity, where any modification to
the files are prohibited even after the partitions are mounted. To provide the
same level of security to the files, all files in an APEX are stored in a file
system image that is paired with a hash tree and a vbmeta descriptor. Without
dm-verity, an APEX in the /data
partition is vulnerable to unintended
modifications that are made after it's been verified and installed.
In fact, the /data
partition is also protected by encryption layers such as
dm-crypt. Although this provides some level of protection against tampering, its
primary purpose is privacy, not integrity. When an attacker gains access to the
/data
partition, there can be no further protection, and this again is a
regression compared to every system component being in the /system
partition.
The hash tree inside an APEX file together with dm-verity provides the same
level of content protection.
Redirect paths from /system to /apex
System component files packaged in an APEX are accessible via new paths like
/apex/<name>/lib/libfoo.so
. When the files were part of the /system
partition, they were accessible via paths such as /system/lib/libfoo.so
. A
client of an APEX file (other APEX files or the platform) must use the new
paths. You might need to update existing code as a result of the path change.
Although one way to avoid the path change is to overlay the file contents in an
APEX file onto the /system
partition, the Android team decided not to overlay
files on the /system
partition because this could impact performance as the
number of files being overlaid (possibly even stacked one after another)
increased.
Another option was to hijack file-access functions such as open
, stat
, and
readlink
, so that paths beginning with /system
were redirected to their
corresponding paths under /apex
. The Android team discarded this option
because it's infeasible to change all functions that accept paths.
For example, some apps statically link Bionic, which implements the functions.
In such cases, those apps don't get redirected.