I have a crash at several users (running android 7.0/7.1) which I can't reproduce.
The crash happens in a module in which I try to get the list of installed app. I tested the app on real hw and all sorts of emulators without issues.
I show below a simplified version of my code.
The line with the crash is:
ApplicationInfo ai = packageManager.getApplicationInfo(info.packageName, 0);
in the function getInstalledAppList.
ListApptest is a class instantiated from a fragment:
ListApptest applications = new ListApp(getActivity());
I initialize packageManager as activity.getPackageManager() and it looks like getInstalledApplications() runs fine (I could add a check for the returned value?) so what could be the reason for the PackageManager$NameNotFoundException?
How could getInstalledApplications returns an unknown package name?
Here is my code:
class ListApptest {
private static final int flags = PackageManager.GET_META_DATA |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_UNINSTALLED_PACKAGES;
private static Context context;
private static Activity activity;
private static PackageManager packageManager;
ListApptest(Activity a, UsageStatsManager u, ListView l, ProgressBar p){
activity = a;
packageManager = activity.getPackageManager();
}
static class AppsGetInfo extends AsyncTask<Void, Void, Void> {
private List<AppsItem> listAppsItem;
@Override
protected Void doInBackground(Void... arg0) {
getInstalledApp();
return null;
}
void getInstalledApp() {
List<String> installedApps = getInstalledAppList();
}
private List<String> getInstalledAppList() {
List<ApplicationInfo> infos = packageManager.getInstalledApplications(flags);
List<String> installedApps = new ArrayList<>();
for (ApplicationInfo info : infos) {
installedApps.add(info.packageName);
try {
// CRASH HERE
ApplicationInfo ai = packageManager.getApplicationInfo(info.packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return installedApps;
}
}
}
Here are the associated trace:
java.lang.RuntimeException:
at android.os.AsyncTask$3.done (AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:354)
at java.util.concurrent.FutureTask.setException (FutureTask.java:223)
at java.util.concurrent.FutureTask.run (FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run (AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
at java.lang.Thread.run (Thread.java:762)
Caused by: java.lang.IllegalArgumentException:
at com.narb.listapp.Applications.ListApp$AppsGetInfo.fromUsageStat (ListApp.java:214)
at com.narb.listapp.Applications.ListApp$AppsGetInfo.buildUsageStatsWrapper (ListApp.java:201)
at com.narb.listapp.Applications.ListApp$AppsGetInfo.getInstalledApp (ListApp.java:163)
at com.narb.listapp.Applications.ListApp$AppsGetInfo.doInBackground (ListApp.java:72)
at com.narb.listapp.Applications.ListApp$AppsGetInfo.doInBackground (ListApp.java:60)
at android.os.AsyncTask$2.call (AsyncTask.java:305)
at java.util.concurrent.FutureTask.run (FutureTask.java:237)
Caused by: android.content.pm.PackageManager$NameNotFoundException:
at android.app.ApplicationPackageManager.getApplicationInfoAsUser (ApplicationPackageManager.java:484)
at android.app.ApplicationPackageManager.getApplicationInfo (ApplicationPackageManager.java:466)
at com.narb.listapp.Applications.ListApp$AppsGetInfo.fromUsageStat (ListApp.java:210)
EDIT
I have a friendly tester (not in the same city) who has the crash. I was able to get some data:
after infos = packageManager.getInstalledApplications:
ApplicationInfo{69419b2 com.sec.factory}
ApplicationInfo{e8f5d03 com.samsung.knox.knoxtrustagent}
ApplicationInfo{4b3b480 com.android.htmlviewer},
Before the crash:
com.sec.factory
com.samsung.knox.knoxtrustagent
so it looks like it is crashing on com.samsung.knox.knoxtrustagent?
What could be the reason? How do you workaround this?