Here's a util function (in Kotlin) that you can add to your code:
/**
* Retrieves the name of the app responsible for the installation of this app.
* This can help in identifying which market this app was installed from or whether the user
* sideloaded it using an APK (Package Installer).
*/
fun getAppInstaller(context: Context): String {
val appContext = context.applicationContext
val installerPackageName = try {
val appPackageManager = appContext.packageManager
val appPackageName = appContext.packageName
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
appPackageManager.getInstallSourceInfo(appPackageName).installingPackageName
else
appPackageManager.getInstallerPackageName(appPackageName)
} catch (e: Exception) {
e.printStackTrace()
"--"
}
return when (installerPackageName) {
"com.android.vending" -> "Google Play Store"
"com.amazon.venezia" -> "Amazon AppStore"
"com.huawei.appmarket" -> "Huawei AppGallery"
"com.google.android.packageinstaller" -> "Package Installer"
else -> installerPackageName ?: "Unknown"
}
}
I used the answers of the great folks here in this StackOverflow question and others to create the above function. You can call this function passing it a context and then maybe set the return as a custom key in Crashlytics or record it for analytics or whatever you want.