Added in API level 21

PackageInstaller


open class PackageInstaller
kotlin.Any
   ↳ android.content.pm.PackageInstaller

Offers the ability to install, upgrade, and remove applications on the device. This includes support for apps packaged either as a single "monolithic" APK, or apps packaged as multiple "split" APKs.

An app is delivered for installation through a PackageInstaller.Session, which any app can create. Once the session is created, the installer can stream one or more APKs into place until it decides to either commit or destroy the session. Committing may require user intervention to complete the installation, unless the caller falls into one of the following categories, in which case the installation will complete automatically.

  • the device owner
  • the affiliated profile owner

Sessions can install brand new apps, upgrade existing apps, or add new splits into an existing app.

Apps packaged as multiple split APKs always consist of a single "base" APK (with a null split name) and zero or more "split" APKs (with unique split names). Any subset of these APKs can be installed together, as long as the following constraints are met:

  • All APKs must have the exact same package name, version code, and signing certificates.
  • All APKs must have unique split names.
  • All installations must contain a single base APK.

The ApiDemos project contains examples of using this API: ApiDemos/src/com/example/android/apis/content/InstallApk*.java.

Summary

Nested classes

A class to encapsulate constraints for installation.

The callback result of checkInstallConstraints(java.util.List,android.content.pm.PackageInstaller.InstallConstraints,java.util.concurrent.Executor,java.util.function.Consumer).

Details for requesting the pre-commit install approval.

open

An installation that is being actively staged.

abstract

Events for observing session lifecycle.

open

Details for an active install session.

open

Parameters for creating a new PackageInstaller.Session.

Used to communicate the unarchival state in reportUnarchivalState.

Constants
static String

Broadcast Action: Explicit broadcast sent to the last known default launcher when a session for a new install is committed.

static String

Activity Action: Show details about a particular install session.

static String

Broadcast Action: Send information about a staged install session when its state is updated.

static String

The InstallConstraints object.

static String

The InstallConstraintsResult object.

static String

Another package name relevant to a status.

static String

Package name that an operation is working with.

static String

Indicate if the status is for a pre-approval request.

static String

SessionInfo that an operation is working with.

static String

An integer session ID that an operation is working with.

static String

Current status of an operation.

static String

Detailed string representation of the status, including raw details that are useful for debugging.

static String

Storage path relevant to a status.

static String

If true, the requestor of the unarchival has specified that the app should be unarchived for all users.

static String

Extra field for the unarchive ID.

static String

Extra field for the package name of a package that is requested to be unarchived.

static String

Current status of an unarchive operation.

static Int

Code indicating that the package being installed comes from a file that was downloaded to the device by the user.

static Int

Code indicating that the package being installed comes from a local file on the device.

static Int

Code indicating that the package being installed is from a source not reflected by any other package source constant.

static Int

Code indicating that the package being installed is from a store.

static Int

The installer did not call PackageInstaller.SessionParams#setPackageSource(int) to specify the package source.

static Int

The operation failed in a generic way.

static Int

The operation failed because it was actively aborted.

static Int

The operation failed because it was blocked.

static Int

The operation failed because it conflicts (or is inconsistent with) with another package already installed on the device.

static Int

The operation failed because it is fundamentally incompatible with this device.

static Int

The operation failed because one or more of the APKs was invalid.

static Int

The operation failed because of storage issues.

static Int

The operation failed because it didn't complete within the specified timeout.

static Int

User action is currently required to proceed.

static Int

The operation succeeded.

static Int

The installer responsible for the unarchival is disabled.

static Int

The installer responsible for the unarchival has been uninstalled

static Int

Not enough storage to unarchive the application.

static Int

The device is not connected to the internet

static Int

The user needs to interact with the installer to enable the installation.

static Int

Generic error: The app cannot be unarchived.

static Int

The unarchival is possible and will commence.

Public methods
open Unit
abandonSession(sessionId: Int)

Completely abandon the given session, destroying all staged data and rendering it invalid.

open Unit

Check if install constraints are satisfied for the given packages.

open Unit
commitSessionAfterInstallConstraintsAreMet(sessionId: Int, statusReceiver: IntentSender, constraints: PackageInstaller.InstallConstraints, timeoutMillis: Long)

Commit the session when all constraints are satisfied.

open Int

Create a new session using the given parameters, returning a unique ID that represents the session.

open PackageInstaller.SessionInfo?

Returns first active staged session, or null if there is none.

open MutableList<PackageInstaller.SessionInfo!>

Returns list of active staged sessions.

open MutableList<PackageInstaller.SessionInfo!>

Return list of all known install sessions, regardless of the installer.

open MutableList<PackageInstaller.SessionInfo!>

Return list of all known install sessions owned by the calling app.

open PackageInstaller.SessionInfo?
getSessionInfo(sessionId: Int)

Return details for a specific session.

open MutableList<PackageInstaller.SessionInfo!>

Return list of all staged install sessions.

open Unit
installExistingPackage(packageName: String, installReason: Int, statusReceiver: IntentSender?)

Install the given package, which already exists on the device, for the user for which this installer was created.

open Unit
installPackageArchived(archivedPackageInfo: ArchivedPackageInfo, sessionParams: PackageInstaller.SessionParams, statusReceiver: IntentSender)

Install package in an archived state.

open PackageInstaller.Session
openSession(sessionId: Int)

Open an existing session to actively perform work.

open Unit

Register to watch for session lifecycle events.

open Unit

Register to watch for session lifecycle events.

open Unit

Reports the state of an unarchival to the system.

open Unit
reportUnarchivalStatus(unarchiveId: Int, status: Int, requiredStorageBytes: Long, userActionIntent: PendingIntent?)

Reports the status of an unarchival to the system.

open Unit
requestArchive(packageName: String, statusReceiver: IntentSender)

Requests to archive a package which is currently installed.

open Unit
requestUnarchive(packageName: String, statusReceiver: IntentSender)

Requests to unarchive a currently archived package.

open Unit
uninstall(packageName: String, statusReceiver: IntentSender)

Uninstall the given package, removing it completely from the device.

open Unit
uninstall(versionedPackage: VersionedPackage, statusReceiver: IntentSender)

Uninstall the given package with a specific version code, removing it completely from the device.

open Unit
uninstall(versionedPackage: VersionedPackage, flags: Int, statusReceiver: IntentSender)

Uninstall the given package with a specific version code, removing it completely from the device.

open Unit
uninstallExistingPackage(packageName: String, statusReceiver: IntentSender?)

Uninstall the given package for the user for which this installer was created if the package will still exist for other users on the device.

open Unit

Unregister a previously registered callback.

open Unit
updateSessionAppIcon(sessionId: Int, appIcon: Bitmap?)

Update the icon representing the app being installed in a specific session.

open Unit
updateSessionAppLabel(sessionId: Int, appLabel: CharSequence?)

Update the label representing the app being installed in a specific session.

open Unit
waitForInstallConstraints(packageNames: MutableList<String!>, constraints: PackageInstaller.InstallConstraints, callback: IntentSender, timeoutMillis: Long)

Similar to checkInstallConstraints(java.util.List,android.content.pm.PackageInstaller.InstallConstraints,java.util.concurrent.Executor,java.util.function.Consumer), but the callback is invoked only when the constraints are satisfied or after timeout.

Constants

ACTION_SESSION_COMMITTED

Added in API level 26
static val ACTION_SESSION_COMMITTED: String

Broadcast Action: Explicit broadcast sent to the last known default launcher when a session for a new install is committed. For managed profile, this is sent to the default launcher of the primary profile. For user-profiles that have items restricted on home screen, this broadcast is sent to the default launcher of the primary profile, only if it has either Manifest.permission.ACCESS_HIDDEN_PROFILES_FULL or Manifest.permission.ACCESS_HIDDEN_PROFILES permission.

The associated session is defined in EXTRA_SESSION and the user for which this session was created in Intent#EXTRA_USER.

Value: "android.content.pm.action.SESSION_COMMITTED"

ACTION_SESSION_DETAILS

Added in API level 21
static val ACTION_SESSION_DETAILS: String

Activity Action: Show details about a particular install session. This may surface actions such as pause, resume, or cancel.

This should always be scoped to the installer package that owns the session. Clients should use SessionInfo#createDetailsIntent() to build this intent correctly.

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

The session to show details for is defined in EXTRA_SESSION_ID.

Value: "android.content.pm.action.SESSION_DETAILS"

ACTION_SESSION_UPDATED

Added in API level 29
static val ACTION_SESSION_UPDATED: String

Broadcast Action: Send information about a staged install session when its state is updated.

The associated session information is defined in EXTRA_SESSION.

Value: "android.content.pm.action.SESSION_UPDATED"

EXTRA_INSTALL_CONSTRAINTS

Added in API level 34
static val EXTRA_INSTALL_CONSTRAINTS: String

The InstallConstraints object.

Value: "android.content.pm.extra.INSTALL_CONSTRAINTS"

EXTRA_INSTALL_CONSTRAINTS_RESULT

Added in API level 34
static val EXTRA_INSTALL_CONSTRAINTS_RESULT: String

The InstallConstraintsResult object.

Value: "android.content.pm.extra.INSTALL_CONSTRAINTS_RESULT"

EXTRA_OTHER_PACKAGE_NAME

Added in API level 21
static val EXTRA_OTHER_PACKAGE_NAME: String

Another package name relevant to a status. This is typically the package responsible for causing an operation failure.

Value: "android.content.pm.extra.OTHER_PACKAGE_NAME"

EXTRA_PACKAGE_NAME

Added in API level 21
static val EXTRA_PACKAGE_NAME: String

Package name that an operation is working with.

Value: "android.content.pm.extra.PACKAGE_NAME"

EXTRA_PRE_APPROVAL

Added in API level 34
static val EXTRA_PRE_APPROVAL: String

Indicate if the status is for a pre-approval request. If callers use the same IntentSender for both Session#requestUserPreapproval(PreapprovalDetails, IntentSender) and Session#commit(IntentSender), they can use this to differentiate between them.

Value: "android.content.pm.extra.PRE_APPROVAL"

EXTRA_SESSION

Added in API level 26
static val EXTRA_SESSION: String

SessionInfo that an operation is working with.

Value: "android.content.pm.extra.SESSION"

EXTRA_SESSION_ID

Added in API level 21
static val EXTRA_SESSION_ID: String

An integer session ID that an operation is working with.

Value: "android.content.pm.extra.SESSION_ID"

EXTRA_STATUS

Added in API level 21
static val EXTRA_STATUS: String

Current status of an operation. Will be one of STATUS_PENDING_USER_ACTION, STATUS_SUCCESS, STATUS_FAILURE, STATUS_FAILURE_ABORTED, STATUS_FAILURE_BLOCKED, STATUS_FAILURE_CONFLICT, STATUS_FAILURE_INCOMPATIBLE, STATUS_FAILURE_INVALID, STATUS_FAILURE_STORAGE, or STATUS_FAILURE_TIMEOUT.

More information about a status may be available through additional extras; see the individual status documentation for details.

Value: "android.content.pm.extra.STATUS"

EXTRA_STATUS_MESSAGE

Added in API level 21
static val EXTRA_STATUS_MESSAGE: String

Detailed string representation of the status, including raw details that are useful for debugging.

Value: "android.content.pm.extra.STATUS_MESSAGE"

EXTRA_STORAGE_PATH

Added in API level 21
static val EXTRA_STORAGE_PATH: String

Storage path relevant to a status.

Value: "android.content.pm.extra.STORAGE_PATH"

EXTRA_UNARCHIVE_ALL_USERS

Added in API level 35
static val EXTRA_UNARCHIVE_ALL_USERS: String

If true, the requestor of the unarchival has specified that the app should be unarchived for all users. Sent as part of the android.content.Intent#ACTION_UNARCHIVE_PACKAGE intent.

Value: "android.content.pm.extra.UNARCHIVE_ALL_USERS"

EXTRA_UNARCHIVE_ID

Added in API level 35
static val EXTRA_UNARCHIVE_ID: String

Extra field for the unarchive ID. Sent as part of the android.content.Intent#ACTION_UNARCHIVE_PACKAGE intent.

Value: "android.content.pm.extra.UNARCHIVE_ID"

EXTRA_UNARCHIVE_PACKAGE_NAME

Added in API level 35
static val EXTRA_UNARCHIVE_PACKAGE_NAME: String

Extra field for the package name of a package that is requested to be unarchived. Sent as part of the android.content.Intent#ACTION_UNARCHIVE_PACKAGE intent.

Value: "android.content.pm.extra.UNARCHIVE_PACKAGE_NAME"

EXTRA_UNARCHIVE_STATUS

Added in API level 35
static val EXTRA_UNARCHIVE_STATUS: String

Current status of an unarchive operation. Will be one of UNARCHIVAL_OK, UNARCHIVAL_ERROR_USER_ACTION_NEEDED, UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE, UNARCHIVAL_ERROR_NO_CONNECTIVITY, UNARCHIVAL_GENERIC_ERROR, UNARCHIVAL_ERROR_INSTALLER_DISABLED or UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED.

If the status is not UNARCHIVAL_OK, then Intent#EXTRA_INTENT will be set with an intent for a corresponding follow-up action (e.g. storage clearing dialog) or a failure dialog.

Used as part of requestUnarchive to return the status of the unarchival through the IntentSender.

Value: "android.content.pm.extra.UNARCHIVE_STATUS"

PACKAGE_SOURCE_DOWNLOADED_FILE

Added in API level 33
static val PACKAGE_SOURCE_DOWNLOADED_FILE: Int

Code indicating that the package being installed comes from a file that was downloaded to the device by the user. For use in place of PACKAGE_SOURCE_LOCAL_FILE when the installer knows the package was downloaded.

Value: 4

PACKAGE_SOURCE_LOCAL_FILE

Added in API level 33
static val PACKAGE_SOURCE_LOCAL_FILE: Int

Code indicating that the package being installed comes from a local file on the device. A file manager that is facilitating the installation of an APK file would use this.

Value: 3

PACKAGE_SOURCE_OTHER

Added in API level 33
static val PACKAGE_SOURCE_OTHER: Int

Code indicating that the package being installed is from a source not reflected by any other package source constant.

Value: 1

PACKAGE_SOURCE_STORE

Added in API level 33
static val PACKAGE_SOURCE_STORE: Int

Code indicating that the package being installed is from a store. An app store that installs an app for the user would use this.

Value: 2

PACKAGE_SOURCE_UNSPECIFIED

Added in API level 33
static val PACKAGE_SOURCE_UNSPECIFIED: Int

The installer did not call PackageInstaller.SessionParams#setPackageSource(int) to specify the package source.

Value: 0

STATUS_FAILURE

Added in API level 21
static val STATUS_FAILURE: Int

The operation failed in a generic way. The system will always try to provide a more specific failure reason, but in some rare cases this may be delivered.

Value: 1

STATUS_FAILURE_ABORTED

Added in API level 21
static val STATUS_FAILURE_ABORTED: Int

The operation failed because it was actively aborted. For example, the user actively declined requested permissions, or the session was abandoned.

Value: 3

STATUS_FAILURE_BLOCKED

Added in API level 21
static val STATUS_FAILURE_BLOCKED: Int

The operation failed because it was blocked. For example, a device policy may be blocking the operation, a package verifier may have blocked the operation, or the app may be required for core system operation.

The result may also contain EXTRA_OTHER_PACKAGE_NAME with the specific package blocking the install.

Value: 2

STATUS_FAILURE_CONFLICT

Added in API level 21
static val STATUS_FAILURE_CONFLICT: Int

The operation failed because it conflicts (or is inconsistent with) with another package already installed on the device. For example, an existing permission, incompatible certificates, etc. The user may be able to uninstall another app to fix the issue.

The result may also contain EXTRA_OTHER_PACKAGE_NAME with the specific package identified as the cause of the conflict.

Value: 5

STATUS_FAILURE_INCOMPATIBLE

Added in API level 21
static val STATUS_FAILURE_INCOMPATIBLE: Int

The operation failed because it is fundamentally incompatible with this device. For example, the app may require a hardware feature that doesn't exist, it may be missing native code for the ABIs supported by the device, or it requires a newer SDK version, etc. Starting in Build.VERSION_CODES#UPSIDE_DOWN_CAKE, an app with only 32-bit native code can still be installed on a device that supports both 64-bit and 32-bit ABIs. However, a warning dialog will be displayed when the app is launched.

Value: 7

STATUS_FAILURE_INVALID

Added in API level 21
static val STATUS_FAILURE_INVALID: Int

The operation failed because one or more of the APKs was invalid. For example, they might be malformed, corrupt, incorrectly signed, mismatched, etc.

Value: 4

STATUS_FAILURE_STORAGE

Added in API level 21
static val STATUS_FAILURE_STORAGE: Int

The operation failed because of storage issues. For example, the device may be running low on space, or external media may be unavailable. The user may be able to help free space or insert different external media.

The result may also contain EXTRA_STORAGE_PATH with the path to the storage device that caused the failure.

Value: 6

STATUS_FAILURE_TIMEOUT

Added in API level 34
static val STATUS_FAILURE_TIMEOUT: Int

The operation failed because it didn't complete within the specified timeout.

Value: 8

STATUS_PENDING_USER_ACTION

Added in API level 21
static val STATUS_PENDING_USER_ACTION: Int

User action is currently required to proceed. You can launch the intent activity described by Intent#EXTRA_INTENT to involve the user and continue.

You may choose to immediately launch the intent if the user is actively using your app. Otherwise, you should use a notification to guide the user back into your app before launching.

Value: -1

STATUS_SUCCESS

Added in API level 21
static val STATUS_SUCCESS: Int

The operation succeeded.

Value: 0

UNARCHIVAL_ERROR_INSTALLER_DISABLED

Added in API level 35
static val UNARCHIVAL_ERROR_INSTALLER_DISABLED: Int

The installer responsible for the unarchival is disabled.

The system will return this status if appropriate. Installers do not need to verify for this error.

Value: 4

UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED

Added in API level 35
static val UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED: Int

The installer responsible for the unarchival has been uninstalled

The system will return this status if appropriate. Installers do not need to verify for this error.

Value: 5

UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE

Added in API level 35
static val UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE: Int

Not enough storage to unarchive the application.

The installer can optionally provide a userActionIntent for a space-clearing dialog. If no action is provided, then a generic intent android.os.storage.StorageManager#ACTION_MANAGE_STORAGE is started instead.

Value: 2

UNARCHIVAL_ERROR_NO_CONNECTIVITY

Added in API level 35
static val UNARCHIVAL_ERROR_NO_CONNECTIVITY: Int

The device is not connected to the internet

Value: 3

UNARCHIVAL_ERROR_USER_ACTION_NEEDED

Added in API level 35
static val UNARCHIVAL_ERROR_USER_ACTION_NEEDED: Int

The user needs to interact with the installer to enable the installation.

An example use case for this could be that the user needs to login to allow the download for a paid app.

Value: 1

UNARCHIVAL_GENERIC_ERROR

Added in API level 35
static val UNARCHIVAL_GENERIC_ERROR: Int

Generic error: The app cannot be unarchived.

Value: 100

UNARCHIVAL_OK

Added in API level 35
static val UNARCHIVAL_OK: Int

The unarchival is possible and will commence.

Note that this does not mean that the unarchival has completed. This status should be sent before any longer asynchronous action (e.g. app download) is started.

Value: 0

Public methods

abandonSession

Added in API level 21
open fun abandonSession(sessionId: Int): Unit

Completely abandon the given session, destroying all staged data and rendering it invalid. Abandoned sessions will be reported to SessionCallback listeners as failures. This is equivalent to opening the session and calling Session#abandon().

Exceptions
java.lang.SecurityException when the caller does not own the session, or the session is invalid.

checkInstallConstraints

Added in API level 34
open fun checkInstallConstraints(
    packageNames: MutableList<String!>,
    constraints: PackageInstaller.InstallConstraints,
    executor: Executor,
    callback: Consumer<PackageInstaller.InstallConstraintsResult!>
): Unit

Check if install constraints are satisfied for the given packages. Note this query result is just a hint and subject to race because system states could change anytime in-between this query and committing the session. The result is returned by a callback because some constraints might take a long time to evaluate.

Parameters
packageNames MutableList<String!>: a list of package names to check the constraints for installation This value cannot be null.
constraints PackageInstaller.InstallConstraints: the constraints for installation. This value cannot be null.
executor Executor: the Executor on which to invoke the callback This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread.
callback Consumer<PackageInstaller.InstallConstraintsResult!>: called when the InstallConstraintsResult is ready This value cannot be null.
Exceptions
java.lang.SecurityException if the given packages' installer of record doesn't match the caller's own package name or the installerPackageName set by the caller doesn't match the caller's own package name.

commitSessionAfterInstallConstraintsAreMet

Added in API level 34
open fun commitSessionAfterInstallConstraintsAreMet(
    sessionId: Int,
    statusReceiver: IntentSender,
    constraints: PackageInstaller.InstallConstraints,
    timeoutMillis: Long
): Unit

Commit the session when all constraints are satisfied. This is a convenient method to combine waitForInstallConstraints(java.util.List,android.content.pm.PackageInstaller.InstallConstraints,android.content.IntentSender,long) and Session#commit(IntentSender).

Once this method is called, the session is sealed and no additional mutations may be performed on the session. In the case of timeout, you may commit the session again using this method or Session#commit(IntentSender) for retries.

Parameters
sessionId Int: the session ID to commit when all constraints are satisfied.
statusReceiver IntentSender: Called when the state of the session changes. Intents sent to this receiver contain EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value cannot be null.
constraints PackageInstaller.InstallConstraints: The requirements to satisfy before committing the session. This value cannot be null.
timeoutMillis Long: The maximum time to wait, in milliseconds until the constraints are satisfied. The caller will be notified via statusReceiver if timeout happens before commit. Value is a non-negative duration in milliseconds.
Exceptions
java.lang.IllegalArgumentException if the statusReceiver from an immutable android.app.PendingIntent when caller has a target SDK of API 35 or above.

createSession

Added in API level 21
open fun createSession(params: PackageInstaller.SessionParams): Int

Create a new session using the given parameters, returning a unique ID that represents the session. Once created, the session can be opened multiple times across multiple device boots.

The system may automatically destroy sessions that have not been finalized (either committed or abandoned) within a reasonable period of time, typically on the order of a day.

Parameters
params PackageInstaller.SessionParams: This value cannot be null.
Return
Int positive, non-zero unique ID that represents the created session. This ID remains consistent across device reboots until the session is finalized. IDs are not reused during a given boot.
Exceptions
java.io.IOException if parameters were unsatisfiable, such as lack of disk space or unavailable media.
java.lang.SecurityException when installation services are unavailable, such as when called from a restricted user.
java.lang.IllegalArgumentException when SessionParams is invalid.

getActiveStagedSession

Added in API level 29
Deprecated in API level 30
open fun getActiveStagedSession(): PackageInstaller.SessionInfo?

Deprecated: Use getActiveStagedSessions as there can be more than one active staged session

Returns first active staged session, or null if there is none.

For more information on what sessions are considered active see SessionInfo#isStagedSessionActive().

getActiveStagedSessions

Added in API level 30
open fun getActiveStagedSessions(): MutableList<PackageInstaller.SessionInfo!>

Returns list of active staged sessions. Returns empty list if there is none.

For more information on what sessions are considered active see * SessionInfo#isStagedSessionActive().

Return
MutableList<PackageInstaller.SessionInfo!> This value cannot be null.

getAllSessions

Added in API level 21
open fun getAllSessions(): MutableList<PackageInstaller.SessionInfo!>

Return list of all known install sessions, regardless of the installer. Callers need to either declare <queries> element with the specific package name in the app's manifest, have the android.permission.QUERY_ALL_PACKAGES, or be the session owner to retrieve these details.

Return
MutableList<PackageInstaller.SessionInfo!> This value cannot be null.

getMySessions

Added in API level 21
open fun getMySessions(): MutableList<PackageInstaller.SessionInfo!>

Return list of all known install sessions owned by the calling app.

Return
MutableList<PackageInstaller.SessionInfo!> This value cannot be null.

getSessionInfo

Added in API level 21
open fun getSessionInfo(sessionId: Int): PackageInstaller.SessionInfo?

Return details for a specific session. Callers need to either declare <queries> element with the specific package name in the app's manifest, have the android.permission.QUERY_ALL_PACKAGES, or be the session owner to retrieve these details.

Return
PackageInstaller.SessionInfo? details for the requested session, or null if the session does not exist.

getStagedSessions

Added in API level 29
open fun getStagedSessions(): MutableList<PackageInstaller.SessionInfo!>

Return list of all staged install sessions. Callers need to either declare <queries> element with the specific package name in the app's manifest, have the android.permission.QUERY_ALL_PACKAGES, or be the session owner to retrieve these details.

Return
MutableList<PackageInstaller.SessionInfo!> This value cannot be null.

installExistingPackage

Added in API level 29
open fun installExistingPackage(
    packageName: String,
    installReason: Int,
    statusReceiver: IntentSender?
): Unit

Install the given package, which already exists on the device, for the user for which this installer was created.

This will all restricted permissions.
Requires android.Manifest.permission#INSTALL_PACKAGES and android.Manifest.permission.INSTALL_EXISTING_PACKAGES

Parameters
packageName String: The package to install. This value cannot be null.
installReason Int: Reason for install. Value is android.content.pm.PackageManager#INSTALL_REASON_UNKNOWN, android.content.pm.PackageManager#INSTALL_REASON_POLICY, android.content.pm.PackageManager#INSTALL_REASON_DEVICE_RESTORE, android.content.pm.PackageManager#INSTALL_REASON_DEVICE_SETUP, android.content.pm.PackageManager#INSTALL_REASON_USER, or android.content.pm.PackageManager.INSTALL_REASON_ROLLBACK
statusReceiver IntentSender?: Where to deliver the result of the operation indicated by the extra EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value may be null.

installPackageArchived

Added in API level 35
open fun installPackageArchived(
    archivedPackageInfo: ArchivedPackageInfo,
    sessionParams: PackageInstaller.SessionParams,
    statusReceiver: IntentSender
): Unit

Install package in an archived state.
Requires android.Manifest.permission#INSTALL_PACKAGES

Parameters
archivedPackageInfo ArchivedPackageInfo: archived package data such as package name, signature etc. This value cannot be null.
sessionParams PackageInstaller.SessionParams: used to create an underlying installation session This value cannot be null.
statusReceiver IntentSender: Called when the state of the session changes. Intents sent to this receiver contain EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value cannot be null.

openSession

Added in API level 21
open fun openSession(sessionId: Int): PackageInstaller.Session

Open an existing session to actively perform work. To succeed, the caller must be the owner of the install session.

Return
PackageInstaller.Session This value cannot be null.
Exceptions
java.io.IOException if parameters were unsatisfiable, such as lack of disk space or unavailable media.
java.lang.SecurityException when the caller does not own the session, or the session is invalid.

registerSessionCallback

Added in API level 21
open fun registerSessionCallback(callback: PackageInstaller.SessionCallback): Unit

Register to watch for session lifecycle events. The callers need to be the session owner or have the android.permission.QUERY_ALL_PACKAGES to watch for these events.

Parameters
callback PackageInstaller.SessionCallback: This value cannot be null.

registerSessionCallback

Added in API level 21
open fun registerSessionCallback(
    callback: PackageInstaller.SessionCallback,
    handler: Handler
): Unit

Register to watch for session lifecycle events. No special permissions are required to watch for these events.

Parameters
handler Handler: to dispatch callback events through, otherwise uses calling thread. This value cannot be null.
callback PackageInstaller.SessionCallback: This value cannot be null.

reportUnarchivalState

Added in API level 35
open fun reportUnarchivalState(unarchivalState: PackageInstaller.UnarchivalState): Unit

Reports the state of an unarchival to the system.
Requires android.Manifest.permission#INSTALL_PACKAGES or android.Manifest.permission#REQUEST_INSTALL_PACKAGES

Parameters
unarchivalState PackageInstaller.UnarchivalState: This value cannot be null.
Exceptions
android.content.pm.PackageManager.NameNotFoundException if no unarchival with unarchiveId exists

reportUnarchivalStatus

Added in API level 35
open fun reportUnarchivalStatus(
    unarchiveId: Int,
    status: Int,
    requiredStorageBytes: Long,
    userActionIntent: PendingIntent?
): Unit

Reports the status of an unarchival to the system.
Requires android.Manifest.permission#INSTALL_PACKAGES or android.Manifest.permission#REQUEST_INSTALL_PACKAGES

Parameters
unarchiveId Int: the ID provided by the system as part of the intent.action.UNARCHIVE broadcast with EXTRA_UNARCHIVE_ID.
status Int: is used for the system to provide the user with necessary follow-up steps or errors. Value is android.content.pm.PackageInstaller.UNARCHIVAL_STATUS_UNSET, android.content.pm.PackageInstaller#UNARCHIVAL_OK, android.content.pm.PackageInstaller#UNARCHIVAL_ERROR_USER_ACTION_NEEDED, android.content.pm.PackageInstaller#UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE, android.content.pm.PackageInstaller#UNARCHIVAL_ERROR_NO_CONNECTIVITY, android.content.pm.PackageInstaller#UNARCHIVAL_ERROR_INSTALLER_DISABLED, android.content.pm.PackageInstaller#UNARCHIVAL_ERROR_INSTALLER_UNINSTALLED, or android.content.pm.PackageInstaller#UNARCHIVAL_GENERIC_ERROR
requiredStorageBytes Long: If the error is UNARCHIVAL_ERROR_INSUFFICIENT_STORAGE this field should be set to specify how many additional bytes of storage are required to unarchive the app.
userActionIntent PendingIntent?: Optional intent to start a follow up action required to facilitate the unarchival flow (e.g. user needs to log in). This value may be null.
Exceptions
android.content.pm.PackageManager.NameNotFoundException if no unarchival with unarchiveId exists

requestArchive

Added in API level 35
open fun requestArchive(
    packageName: String,
    statusReceiver: IntentSender
): Unit

Requests to archive a package which is currently installed.

During the archival process, the apps APKs and cache are removed from the device while the user data is kept. Through the requestUnarchive call, apps can be restored again through their responsible installer.

Archived apps are returned as displayable apps through the LauncherApps APIs and will be displayed to users with UI treatment to highlight that said apps are archived. If a user taps on an archived app, the app will be unarchived and the restoration process is communicated.
Requires android.Manifest.permission#DELETE_PACKAGES or android.Manifest.permission#REQUEST_DELETE_PACKAGES

Parameters
statusReceiver IntentSender: Callback used to notify when the operation is completed. This value cannot be null.
packageName String: This value cannot be null.
Exceptions
android.content.pm.PackageManager.NameNotFoundException If packageName isn't found or not available to the caller or isn't archived.

requestUnarchive

Added in API level 35
open fun requestUnarchive(
    packageName: String,
    statusReceiver: IntentSender
): Unit

Requests to unarchive a currently archived package.

Sends a request to unarchive an app to the responsible installer. The installer is determined by InstallSourceInfo#getUpdateOwnerPackageName(), or InstallSourceInfo#getInstallingPackageName() if the former value is null.

The installation will happen asynchronously and can be observed through android.content.Intent#ACTION_PACKAGE_ADDED.
Requires android.Manifest.permission#INSTALL_PACKAGES or android.Manifest.permission#REQUEST_INSTALL_PACKAGES

Parameters
statusReceiver IntentSender: Callback used to notify whether the installer has accepted the unarchival request or an error has occurred. The status update will be sent though EXTRA_UNARCHIVE_STATUS. Only one status will be sent. This value cannot be null.
packageName String: This value cannot be null.
Exceptions
android.content.pm.PackageManager.NameNotFoundException If packageName isn't found or not visible to the caller or if the package has no installer on the device anymore to unarchive it.
java.io.IOException If parameters were unsatisfiable, such as lack of disk space.

uninstall

Added in API level 21
open fun uninstall(
    packageName: String,
    statusReceiver: IntentSender
): Unit

Uninstall the given package, removing it completely from the device. This method is available to:

  • the current "installer of record" for the package
  • the device owner
  • the affiliated profile owner

Requires android.Manifest.permission#DELETE_PACKAGES or android.Manifest.permission#REQUEST_DELETE_PACKAGES

Parameters
packageName String: The package to uninstall. This value cannot be null.
statusReceiver IntentSender: Where to deliver the result of the operation indicated by the extra EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value cannot be null.

uninstall

Added in API level 26
open fun uninstall(
    versionedPackage: VersionedPackage,
    statusReceiver: IntentSender
): Unit

Uninstall the given package with a specific version code, removing it completely from the device. If the version code of the package does not match the one passed in the versioned package argument this method is a no-op. Use PackageManager#VERSION_CODE_HIGHEST to uninstall the latest version of the package.

This method is available to:

  • the current "installer of record" for the package
  • the device owner
  • the affiliated profile owner

Requires android.Manifest.permission#DELETE_PACKAGES or android.Manifest.permission#REQUEST_DELETE_PACKAGES
Parameters
versionedPackage VersionedPackage: The versioned package to uninstall. This value cannot be null.
statusReceiver IntentSender: Where to deliver the result of the operation indicated by the extra EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value cannot be null.

uninstall

Added in API level 34
open fun uninstall(
    versionedPackage: VersionedPackage,
    flags: Int,
    statusReceiver: IntentSender
): Unit

Uninstall the given package with a specific version code, removing it completely from the device. This method is only available to the current "installer of record" for the package. If the version code of the package does not match the one passed in the versioned package argument this method is a no-op. Use PackageManager#VERSION_CODE_HIGHEST to uninstall the latest version of the package.
Requires android.Manifest.permission#DELETE_PACKAGES or android.Manifest.permission#REQUEST_DELETE_PACKAGES

Parameters
versionedPackage VersionedPackage: The versioned package to uninstall. This value cannot be null.
flags Int: Flags for uninstall. Value is either 0 or a combination of android.content.pm.PackageManager.DELETE_KEEP_DATA, android.content.pm.PackageManager.DELETE_ALL_USERS, android.content.pm.PackageManager.DELETE_SYSTEM_APP, android.content.pm.PackageManager.DELETE_DONT_KILL_APP, and android.content.pm.PackageManager.DELETE_CHATTY
statusReceiver IntentSender: Where to deliver the result of the operation indicated by the extra EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value cannot be null.

uninstallExistingPackage

Added in API level 31
open fun uninstallExistingPackage(
    packageName: String,
    statusReceiver: IntentSender?
): Unit

Uninstall the given package for the user for which this installer was created if the package will still exist for other users on the device.
Requires android.Manifest.permission#DELETE_PACKAGES

Parameters
packageName String: The package to uninstall. This value cannot be null.
statusReceiver IntentSender?: Where to deliver the result of the operation indicated by the extra EXTRA_STATUS. Refer to the individual status codes on how to handle them. This value may be null.

unregisterSessionCallback

Added in API level 21
open fun unregisterSessionCallback(callback: PackageInstaller.SessionCallback): Unit

Unregister a previously registered callback.

Parameters
callback PackageInstaller.SessionCallback: This value cannot be null.

updateSessionAppIcon

Added in API level 21
open fun updateSessionAppIcon(
    sessionId: Int,
    appIcon: Bitmap?
): Unit

Update the icon representing the app being installed in a specific session. This should be roughly ActivityManager#getLauncherLargeIconSize() in both dimensions.

Parameters
appIcon Bitmap?: This value may be null.
Exceptions
java.lang.SecurityException when the caller does not own the session, or the session is invalid.

updateSessionAppLabel

Added in API level 21
open fun updateSessionAppLabel(
    sessionId: Int,
    appLabel: CharSequence?
): Unit

Update the label representing the app being installed in a specific session.

Parameters
appLabel CharSequence?: This value may be null.
Exceptions
java.lang.SecurityException when the caller does not own the session, or the session is invalid.

waitForInstallConstraints

Added in API level 34
open fun waitForInstallConstraints(
    packageNames: MutableList<String!>,
    constraints: PackageInstaller.InstallConstraints,
    callback: IntentSender,
    timeoutMillis: Long
): Unit

Similar to checkInstallConstraints(java.util.List,android.content.pm.PackageInstaller.InstallConstraints,java.util.concurrent.Executor,java.util.function.Consumer), but the callback is invoked only when the constraints are satisfied or after timeout.

Note: the device idle constraint might take a long time to evaluate. The system will ensure the constraint is evaluated completely before handling timeout.

Parameters
packageNames MutableList<String!>: a list of package names to check the constraints for installation This value cannot be null.
constraints PackageInstaller.InstallConstraints: the constraints for installation. This value cannot be null.
callback IntentSender: Called when the constraints are satisfied or after timeout. Intents sent to this callback contain: Intent#EXTRA_PACKAGES for the input package names, EXTRA_INSTALL_CONSTRAINTS for the input constraints, EXTRA_INSTALL_CONSTRAINTS_RESULT for the result. This value cannot be null.
timeoutMillis Long: The maximum time to wait, in milliseconds until the constraints are satisfied. Valid range is from 0 to one week. 0 means the callback will be invoked immediately no matter constraints are satisfied or not. Value is a non-negative duration in milliseconds.
Exceptions
java.lang.SecurityException if the given packages' installer of record doesn't match the caller's own package name or the installerPackageName set by the caller doesn't match the caller's own package name.