getInstallerPackageName(String): String?' is deprecated. Deprecated in Java

Viewed 1768

I have written a simple Statment like this:

val installer = context.packageManager.getInstallerPackageName(context.packageName)

but it's now deprecated as shown in the picture:

enter image description here

Is there any alternative way available to get the package name of the app that has installed your app?

1 Answers

Here's how you can use the new one:

    fun getInstallerPackageName(context: Context, packageName: String): String? {
        kotlin.runCatching {
            if (VERSION.SDK_INT >= VERSION_CODES.R)
                return context.packageManager.getInstallSourceInfo(packageName).installingPackageName
            @Suppress("DEPRECATION")
            return context.packageManager.getInstallerPackageName(packageName)
        }
        return null
    }
Related