0

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?

Jorn Rigter
  • 364
  • 1
  • 15

2 Answers2

1

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.

ephemient
  • 180,829
  • 34
  • 259
  • 378
  • 1
    There is also [``](https://developer.android.com/reference/android/Manifest.permission#QUERY_ALL_PACKAGES) if you need results from `pm.getInstalled*()`. But the intent query should be what a launcher actually needs. – ephemient Oct 21 '20 at 19:05
  • I actually had these declarations set in the manifest already, but your comment of using the permission was the solution for me. With that permission, it is working well again! Thanks a lot! – Jorn Rigter Oct 22 '20 at 09:42
0

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.

Jorn Rigter
  • 364
  • 1
  • 15