Android 12 Developer Preview is here! Try it out, and give us your feedback!

PackageInfoCompat

class PackageInfoCompat
kotlin.Any
   ↳ androidx.core.content.pm.PackageInfoCompat

Helper for accessing features in PackageInfo.

Summary

Public methods
static Long

Return android.R.attr#versionCode and android.R.attr#versionCodeMajor combined together as a single long value.

static MutableList<Signature!>
getSignatures(@NonNull packageManager: PackageManager, @NonNull packageName: String)

Retrieve the Signature array for the given package.

static Boolean
hasSignatures(@NonNull packageManager: PackageManager, @NonNull packageName: String, @Size(1) @NonNull certificatesAndType: MutableMap<ByteArray!, Int!>, matchExact: Boolean)

Check if a package on device contains set of a certificates.

Public methods

getLongVersionCode

static fun getLongVersionCode(@NonNull info: PackageInfo): Long

Return android.R.attr#versionCode and android.R.attr#versionCodeMajor combined together as a single long value. The versionCodeMajor is placed in the upper 32 bits on Android P or newer, otherwise these bits are all set to 0.

getSignatures

@NonNull static fun getSignatures(
    @NonNull packageManager: PackageManager,
    @NonNull packageName: String
): MutableList<Signature!>

Retrieve the Signature array for the given package. This returns some of certificates, depending on whether the package in question is multi-signed or has signing history.

Security/identity verification should not be done with this method. This is only intended to return some array of certificates that correspond to a package.

If verification if required, either use hasSignatures(PackageManager, String, Map, boolean) or manually verify the set of certificates using PackageManager#GET_SIGNING_CERTIFICATES or PackageManager#GET_SIGNATURES.

Parameters
packageManager PackageManager: The PackageManager instance to query against.
packageName String: The package to query the {@param packageManager} for. Query by app UID is only supported by manually choosing a package name returned in PackageManager#getPackagesForUid(int).
Return
MutableList<Signature!> an array of certificates the app is signed with
Exceptions
PackageManager.NameNotFoundException if the package cannot be found through the provided {@param packageManager}

hasSignatures

static fun hasSignatures(
    @NonNull packageManager: PackageManager,
    @NonNull packageName: String,
    @Size(1) @NonNull certificatesAndType: MutableMap<ByteArray!, Int!>,
    matchExact: Boolean
): Boolean

Check if a package on device contains set of a certificates. Supported types are raw X509 or SHA-256 bytes.

Parameters
packageManager PackageManager: The PackageManager instance to query against.
packageName String: The package to query the {@param packageManager} for. Query by app UID is only supported by manually choosing a package name returned in PackageManager#getPackagesForUid(int).
certificatesAndType MutableMap<ByteArray!, Int!>: The bytes of the certificate mapped to the type, either PackageManager#CERT_INPUT_RAW_X509 or PackageManager#CERT_INPUT_SHA256. A single or multiple certificates may be included.
matchExact Boolean: Whether or not to check for presence of all signatures exactly. If false, then the check will succeed if the query contains a subset of the package certificates. Matching exactly is strongly recommended when running on devices below Build.VERSION_CODES#LOLLIPOP due to the fake ID vulnerability that allows a package to be modified to include an unverified signature.
Return
Boolean true if the package is considered signed by the given certificate set, or false otherwise
Exceptions
PackageManager.NameNotFoundException if the package cannot be found through the provided {@param packageManager}