PackageManager not returning correct packages with targetSdkVersion 30

1.9k views Asked by At

I am using the PackageManager to get a list of all packages installed on the user's device. This is working perfectly fine, until I switch from targetSdkVersion 29 to 30.

When I increase the targetSdkVersion from 29 to 30, the PackageManager is not returning the correct list of packages anymore (I'm making a launcher and in fact, it is barely returning any packages that can be launched).

I tried pm.getInstalledPackages(0), pm.getInstalledApplications(0) and the method for retrieving apps as indicated here. None of them worked, and all of them were working previously.

The build.gradle version settings:

compileSdkVersion 30
defaultConfig {
    minSdkVersion 23
    targetSdkVersion 29
}

Does anyone have an idea of what is happening here?

2

There are 2 answers

0
Jorn Rigter On BEST ANSWER

As @ephemient pointed out in his comment of his answer, using <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/> solved it.

This is done because of the restricted access apps have from Android 11 onwards, as this article explains well.

4
ephemient On

You need to add a declaration to your manifest in order to see other packages when targeting Android 11 (API 30): https://developer.android.com/about/versions/11/privacy/package-visibility

In particular, if you're building a launcher,

<manifest>
  <queries>
    <intent>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent>
  </queries>
  ...
</manifest>

would allow all results that

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.getPackageManager().queryIntentActivities(intent, 0);

could match to be returned.