How can I find if a particular package exists on my Android device?

AndroidAndroid Applicationinfo

Android Problem Overview


How can I find whether a particular package or application, say: com.android.abc, exists on my Android device?

Android Solutions


Solution 1 - Android

Call any of the below method with the package name.

import android.content.pm.PackageManager;

// ...

    public boolean isPackageExisted(String targetPackage){
        List<ApplicationInfo> packages;
        PackageManager pm;
        
        pm = getPackageManager();        
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if(packageInfo.packageName.equals(targetPackage))
                return true;
        }
        return false;
    }

 import android.content.pm.PackageManager;

 public boolean isPackageExisted(String targetPackage){
   PackageManager pm=getPackageManager();
   try {
	 PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
   } catch (PackageManager.NameNotFoundException e) {
	 return false;
   }  
   return true;
 }

Solution 2 - Android

Without using a try-catch block or iterating through a bunch of packages:

public static boolean isPackageInstalled(Context context, String packageName) {
    final PackageManager packageManager = context.getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(packageName);
    if (intent == null) {
        return false;
    }
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

Solution 3 - Android

Kotlin

fun isPackageExist(context: Context, target: String): Boolean {
     return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}

Edit: Extension Function

fun Context.isPackageExist(target: String): Boolean {
     return packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}

Solution 4 - Android

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;

Solution 5 - Android

We can check like this:

 if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) {
            if (Constant.isNetworkConnected(Activity.this)) {
                 //Your Intent 
            } else {
                Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
            }
        }   else
            {
                Constant.showDialog(Activity.this,"Please install the webview");
            }
    }

Make method for package check ! this credit goes to "Kavi" https://stackoverflow.com/a/30708227/6209105

public boolean isPackageExisted(String targetPackage) {
    List<ApplicationInfo> packages;
    PackageManager pm;

    pm = getPackageManager();
    packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo : packages) {
        if(packageInfo.packageName.equals(targetPackage))
        {
            return true;
        }
}
return false;

}

Solution 6 - Android

You should use PackageManager's function called getInstalledPackages() to get the list of all installed packages and the search for the one you are interested in. Note that package name is located in PackageInfo.packageName field.

Solution 7 - Android

If you just want to use adb:

adb shell "pm list packages"|cut -f 2 -d ":"

it will list all installed packages.

Solution 8 - Android

You can use pm.getPackageUid() instead of iterating over the pm.getInstalledApplications()

 boolean isPackageInstalled;
 PackageManager pm = getPackageManager();	
 int flags = 0;	
		try	
        {	
	          pm.getPackageUid(packageName,flags);				 
              isPackageInstalled = true;	
        }	
        catch (final PackageManager.NameNotFoundException nnfe)	
        {	
            isPackageInstalled = false;	
        }					
 return isPackageInstalled;

Solution 9 - Android

Since some devices have reported that the "getInstalledPackages" can cause TransactionTooLargeException (check here, here and here), I think you should also have a fallback like I did below.

This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.

public static List<String> getInstalledPackages(final Context context) {
    List<String> result = new ArrayList<>();
    final PackageManager pm = context.getPackageManager();
    try {
        List<PackageInfo> apps = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : apps)
            result.add(packageInfo.packageName);
        return result;
    } catch (Exception ignored) {
        //we don't care why it didn't succeed. We'll do it using an alternative way instead
    }
    // use fallback:
    BufferedReader bufferedReader = null;
    try {
        Process process = Runtime.getRuntime().exec("pm list packages");
        bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            final String packageName = line.substring(line.indexOf(':') + 1);
            result.add(packageName);
        }
        closeQuietly(bufferedReader);
        process.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeQuietly(bufferedReader);
    }
    return result;
}

public static void closeQuietly(final Closeable closeable) {
    if (closeable == null)
        return;
    try {
        closeable.close();
    } catch (final IOException e) {
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrigView Question on Stackoverflow
Solution 1 - AndroidRaselView Answer on Stackoverflow
Solution 2 - AndroidKaviView Answer on Stackoverflow
Solution 3 - AndroidRezaView Answer on Stackoverflow
Solution 4 - Androiduser3078812View Answer on Stackoverflow
Solution 5 - AndroidShaikh MohibView Answer on Stackoverflow
Solution 6 - AndroidinazarukView Answer on Stackoverflow
Solution 7 - AndroidIrynaView Answer on Stackoverflow
Solution 8 - AndroidGowthamView Answer on Stackoverflow
Solution 9 - Androidandroid developerView Answer on Stackoverflow