NameNotFoundException when calling getPackageInfo on Android 11
Asked Answered
K

3

45

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)
Khajeh answered 12/6, 2020 at 13:59 Comment(0)
K
95

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>
Khajeh answered 12/6, 2020 at 13:59 Comment(4)
If you public the app in the Play store, make sure your app qualifies for QUERY_ALL_PACKAGES: developer.android.com/preview/privacy/…Aparejo
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> this tag got helped. Thanks. Interesting <queries>bla bla</queries> code not worked. merge manifest error.Sporting
I am getting quite a lot NameNotFoundExceptions 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.Relief
A small portion of my users get this error when app requests info about its own package.Airline
A
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>
Ascarid answered 27/4, 2022 at 7:2 Comment(0)
A
2

It works for me on Android 12, thanks Roy & Rohit

<queries>
    <package android:name="com.example.store" />
    <package android:name="com.example.services" />
    <intent>
        <action android:name="android.intent.action.MAIN" />
    </intent>
</queries>
Alon answered 14/2 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.