5

I have a .java file:

 ....

 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;

 ....

   /* PackageInfo packageInfo = 
   getPackageManager().getPackageInfo(getPackageName(),0);
   curVersionCode = packageInfo.versionCode;*/

   PackageManager pm = getPackageManager();
   String pn = getPackageName();
   PackageInfo pi = pm.getPackageInfo(pn,0);
   curVersionCode = pi.versionCode;

   ....

To understand the error i wrote the code in four lines. The two line code is in the comment as you see. Now that, this code doesn't work and gives an error of PackageManager.NameNotFoundException here when i try to build. For clarity, I used this code in a different file and it caused no error there; so there is no need to worry about the package name. Now what is the cause behind this error? An image is also attached to make clear the error.

enter image description here

To solve this issue i also tried like these: this.getPackageManager(), getBaseContext().getPackageManager() and getApplicationContext().getPackageManager(); But all in vain.

Fechang Fuchang
  • 131
  • 1
  • 1
  • 11
  • 1
    This is a checked exception - read the source code docs : `Throws {@link NameNotFoundException} if a package with the given name can not be found on the system.` - I suspect you have the other code in a try/catch block already. – Mark Jun 12 '18 at 00:39
  • Yes , you are right. The other code is in a try/catch. Thank you so much. – Fechang Fuchang Jun 12 '18 at 00:46

3 Answers3

6

Originally answered here

There is one behaviour change in Android API level 30 because of which we get NameNotFoundException when we call getPackageInfo on Android 11 even if the app/package app is installed on user's device. From Android API 30, we need to specify the package name in manifest in order to check if that package is installed on user's device.

For example we need to specify Youtube package in app manifest file before checking if youtube app is installed on user's device,

<?xml version="1.0" encoding="utf-8"?>
<manifest 
 ...>
    <queries>
        <package android:name="com.google.android.youtube" />
    </queries>

    <application
       android:name=".Notes App"
    .../>

</manifest>
Ramakrishna Joshi
  • 1,442
  • 17
  • 22
3

pm.getPackageInfo throws PackageManager.NameNotFoundException which needs to be handled. Just wrap it with try catch as follows:

    final PackageManager pm = getPackageManager();
    final String pn = getPackageName();
    PackageInfo pi = null;
    try {
        pi = pm.getPackageInfo(pn, 0);
    } catch (NameNotFoundException e) {
        //LOG the exception
    }
    curVersionCode = pi!=null ? pi.versionCode:-1;
Sagar
  • 23,903
  • 4
  • 62
  • 62
0

When calling some methods that will throw exceptions, you need to use try catch to handle possible exceptions. At the same time, when the target api version >= 30, you need to add the packagename of the app to be queried in the androidmanifest as follows, otherwise even if the corresponding app is installed on the device will also return exception

<queries>
    <package android:name="com.whatsapp"/>
</queries>