Skip to content

Most visited

Recently visited

navigation

PackageInstaller.Session

public static class PackageInstaller.Session
extends Object implements Closeable

java.lang.Object
   ↳ android.content.pm.PackageInstaller.Session


An installation that is being actively staged. For an install to succeed, all existing and new packages must have identical package names, version codes, and signing certificates.

A session may contain any number of split packages. If the application does not yet exist, this session must include a base package.

If an APK included in this session is already defined by the existing installation (for example, the same split name), the APK in this session will replace the existing APK.

Summary

Public methods

void abandon()

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

void close()

Release this session object.

void commit(IntentSender statusReceiver)

Attempt to commit everything staged in this session.

void fsync(OutputStream out)

Ensure that any outstanding data for given stream has been committed to disk.

String[] getNames()

Return all APK names contained in this session.

InputStream openRead(String name)

Open a stream to read an APK file from the session.

OutputStream openWrite(String name, long offsetBytes, long lengthBytes)

Open a stream to write an APK file into the session.

void removeSplit(String splitName)

Removes a split.

void setStagingProgress(float progress)

Set current progress of staging this session.

void transfer(String packageName)

Transfer the session to a new owner.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public methods

abandon

added in API level 21
void abandon ()

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

close

added in API level 21
void close ()

Release this session object. You can open the session again if it hasn't been finalized.

commit

added in API level 21
void commit (IntentSender statusReceiver)

Attempt to commit everything staged in this session. This may require user intervention, and so it may not happen immediately. The final result of the commit will be reported through the given callback.

Once this method is called, the session is sealed and no additional mutations may be performed on the session. If the device reboots before the session has been finalized, you may commit the session again.

Parameters
statusReceiver IntentSender

This value must never be null.

Throws
SecurityException if streams opened through openWrite(String, long, long) are still open.

fsync

added in API level 21
void fsync (OutputStream out)

Ensure that any outstanding data for given stream has been committed to disk. This is only valid for streams returned from openWrite(String, long, long).

Parameters
out OutputStream

This value must never be null.

Throws
IOException

getNames

added in API level 21
String[] getNames ()

Return all APK names contained in this session.

This returns all names which have been previously written through openWrite(String, long, long) as part of this session.

Returns
String[]

This value will never be null.

Throws
SecurityException if called after the session has been committed or abandoned.
IOException

openRead

added in API level 21
InputStream openRead (String name)

Open a stream to read an APK file from the session.

This is only valid for names which have been previously written through openWrite(String, long, long) as part of this session. For example, this stream may be used to calculate a MessageDigest of a written APK before committing.

Parameters
name String

This value must never be null.

Returns
InputStream

This value will never be null.

Throws
SecurityException if called after the session has been committed or abandoned.
IOException

openWrite

added in API level 21
OutputStream openWrite (String name, 
                long offsetBytes, 
                long lengthBytes)

Open a stream to write an APK file into the session.

The returned stream will start writing data at the requested offset in the underlying file, which can be used to resume a partially written file. If a valid file length is specified, the system will preallocate the underlying disk space to optimize placement on disk. It's strongly recommended to provide a valid file length when known.

You can write data into the returned stream, optionally call fsync(OutputStream) as needed to ensure bytes have been persisted to disk, and then close when finished. All streams must be closed before calling commit(IntentSender).

Parameters
name String: arbitrary, unique name of your choosing to identify the APK being written. You can open a file again for additional writes (such as after a reboot) by using the same name. This name is only meaningful within the context of a single install session.

This value must never be null.

offsetBytes long: offset into the file to begin writing at, or 0 to start at the beginning of the file.

lengthBytes long: total size of the file being written, used to preallocate the underlying disk space, or -1 if unknown. The system may clear various caches as needed to allocate this space.

Returns
OutputStream

This value will never be null.

Throws
IOException if trouble opening the file for writing, such as lack of disk space or unavailable media.
SecurityException if called after the session has been sealed or abandoned

removeSplit

added in API level 24
void removeSplit (String splitName)

Removes a split.

Split removals occur prior to adding new APKs. If upgrading a feature split, it is not expected nor desirable to remove the split prior to upgrading.

When split removal is bundled with new APKs, the packageName must be identical.

Parameters
splitName String

This value must never be null.

Throws
IOException

setStagingProgress

added in API level 21
void setStagingProgress (float progress)

Set current progress of staging this session. Valid values are anywhere between 0 and 1.

Note that this progress may not directly correspond to the value reported by onProgressChanged(int, float), as the system may carve out a portion of the overall progress to represent its own internal installation work.

Parameters
progress float

transfer

added in API level 27
void transfer (String packageName)

Transfer the session to a new owner.

Only sessions that update the installing app can be transferred.

After the transfer to a package with a different uid all method calls on the session will cause SecurityExceptions.

Once this method is called, the session is sealed and no additional mutations beside committing it may be performed on the session.

Parameters
packageName String: The package of the new owner. Needs to hold the INSTALL_PACKAGES permission.

This value must never be null.

Throws
PackageManager.NameNotFoundException if the new owner could not be found.
SecurityException if called after the session has been committed or abandoned.
SecurityException if the session does not update the original installer
SecurityException if streams opened through {@link #openWrite(String, long, long) are still open.
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)