39

After setting targetSdkVersion to 30 (Android 11) I'm getting android.content.pm.PackageManager$NameNotFoundException when doing packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS) on packages that I know exists.

The stacktrace is as follows:

android.content.pm.PackageManager$NameNotFoundException: com.google.android.apps.maps
        at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:202)
        at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:174)
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76

2 Answers2

86

As stated in https://developer.android.com/preview/privacy/package-visibility:

Android 11 changes how apps can query and interact with other apps that the user has installed on a device. Using the new element, apps can define the set of other apps that they can access. This element helps encourage the principle of least privilege by telling the system which other apps to make visible to your app, and it helps app stores like Google Play assess the privacy and security that your app provides for users.

If your app targets Android 11, you might need to add the element in your app's manifest file. Within the element, you can specify apps by package name or by intent signature.

So you either have to stop what you are doing, or request to access information about certain packages, or - if you have reasons for it - use the permission QUERY_ALL_PACKAGES.

Query and interact with specific packages

To query and interact with specific packages you would update your AndroidManifest.xml like this:

<manifest ...>
    ...
    <queries>
        <package android:name="com.example.store" />
        <package android:name="com.example.services" />
    </queries>
    ...
     <application ...>
    ...
</manifest>

Query and interact with all apps

I have an app that needs to be able to ask for information for all apps. All you have to do is to add the following to AndroidManifest.xml:

<manifest ...>
     ...
     <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
     ...
     <application ...>
     ...
</manifest>
Roy Solberg
  • 18,133
  • 12
  • 49
  • 76
  • If you public the app in the Play store, make sure your app qualifies for QUERY_ALL_PACKAGES: https://developer.android.com/preview/privacy/package-visibility#all-apps – M66B Jun 14 '20 at 07:56
  • 1
    this tag got helped. Thanks. Interesting bla bla code not worked. merge manifest error. – Nijat Aliyev Mar 20 '22 at 15:38
  • I am getting quite a lot `NameNotFoundException`s for users with Android version below 11, e.g. for Android 7.1.1 on OnePlus. I am not able to reproduce this myself so I am wondering if that could be caused by brand-specific package visibility protection. – mieszko4 Oct 05 '22 at 10:15
  • A small portion of my users get this error when app requests info about its own package. – Lingviston Jul 13 '23 at 07:36
9

In AndroidManifest.xml needed to add this both for Android 11 and it works for me

<permission android:name="android.permission.QUERY_ALL_PACKAGES" />


    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN" />
        </intent>
    </queries>
Rohit S
  • 714
  • 5
  • 7