@Suppress("DEPRECATION")fun Context.isPackageInstalled(packageName: String): Boolean { return try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0)) } else { packageManager.getPackageInfo(packageName, 0) } true } catch (e: PackageManager.NameNotFoundException) { false }}
Usage
val context = LocalContext.currentval packageName = "com.instagram.android"if (context.isPackageInstalled(packageName)) {}
References: