Android parses the APK file on the SD card
public void parseApk(Context context, String apkFilePath){ PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageArchiveInfo(apkFilePath, 0); if(packageInfo != null){ packageInfo.applicationInfo.sourceDir = apkFilePath; packageInfo.applicationInfo.publicSourceDir = apkFilePath; String name = packageInfo.applicationInfo.loadLabel(packageManager).toString(); String packageName = packageInfo.packageName; String versionName = packageInfo.versionName; int versionCode = packageInfo.versionCode; Drawable iconDrawable = packageInfo.applicationInfo.loadIcon(packageManager); }}
Note that after obtaining packageInfo, you must set sourceDir and publicSourceDir of applicationInfo. Otherwise, you will output the following error message and throw an exception when reading the label.
Failure retrieving text 0x7f050000 in package com.taskkiller.demo
When you read the icon, the following error message is output and an exception is thrown.
Failure retrieving icon 0x7f020005 in package com.sample.radio
This is because both the label and icon are in the resource. If sourceDir and publicSourceDir are not set, the information in the resource cannot be read.