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

2

5

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?

Bandoline answered 31/3, 2021 at 22:33 Comment(6)
Did you read the deprecation message on the method?Bibliopole
Call requires API level RBandoline
@Bibliopole What should be passed to getInstallSourceInfo(String) ?Tungstite
@androiddeveloper the package nameBandoline
@MohammadSommakia Sorry I mean : How can I get the same result as the original "getInstallerPackageName" ? Of which installer package name ?Tungstite
as they said in the docs it will return a string value it should be the package name that installed your app i didn't try itBandoline
T
11

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
    }
Tungstite answered 28/6, 2021 at 23:3 Comment(0)
T
0

In my case,

package you_package_name

import android.content.pm.PackageManager
import android.view.View
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.ReactShadowNode
import com.facebook.react.uimanager.ViewManager
import android.util.Log
import android.os.Build
import android.content.Context

class InstallerCheckerModule(reactContext: ReactApplicationContext) : 
ReactContextBaseJavaModule(reactContext), ReactPackage {

override fun getName(): String {
    return "InstallerChecker"
}

// Function to check if the app was installed from Google Play Store
@ReactMethod
fun isInstalledFromGooglePlay(promise: Promise) {
    try {
        val installerPackageName = getInstallerPackageName(reactApplicationContext, reactApplicationContext.packageName)
        Log.d("InstallerChecker", "Installer package name: $installerPackageName")
        if (installerPackageName != null && installerPackageName == "com.android.vending") {
            promise.resolve(true) // App was installed from Google Play Store
        } else {
            promise.resolve(false) // App was not installed from Google Play Store
        }
    } catch (e: Exception) {
        promise.reject("ERROR", e.message)
    }
}

// Get the installer package name
private fun getInstallerPackageName(context: Context, packageName: String): String? {
    return kotlin.runCatching {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
            context.packageManager.getInstallSourceInfo(packageName).installingPackageName
        else
            @Suppress("DEPRECATION")
            context.packageManager.getInstallerPackageName(packageName)
    }.getOrNull()
}

override fun createNativeModules(p0: ReactApplicationContext): MutableList<NativeModule> {
    return mutableListOf()
}

override fun createViewManagers(p0: ReactApplicationContext): MutableList<ViewManager<View, ReactShadowNode<*>>> {
    return mutableListOf()
}
}
Thoroughpaced answered 28/3 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.