I need to detect my application is installed from google play or other market, how could I get this information?
The PackageManager
class supplies the getInstallerPackageName method that will tell you the package name of whatever installed the package you specify. Side-loaded apps will not contain a value.
EDIT: Note @mttmllns' answer below regarding the Amazon app store.
And FYI apparently the latest version of the Amazon store finally sets PackageManager.getInstallerPackageName()
to "com.amazon.venezia"
as well to contrast with Google Play's "com.android.vending"
.
I use this code to check, if a build was downloaded from a store or sideloaded:
public static boolean isStoreVersion(Context context) {
boolean result = false;
try {
String installer = context.getPackageManager()
.getInstallerPackageName(context.getPackageName());
result = !TextUtils.isEmpty(installer);
} catch (Throwable e) {
}
return result;
}
Kotlin:
fun isStoreVersion(context: Context) =
try {
context.packageManager
.getInstallerPackageName(context.packageName)
.isNotEmpty()
} catch (e: Throwable) {
false
}
isNotEmpty()
–
Jess Update:
The feature is now obsolete on versions Android 10 or higher.
Installs with missing splits are now blocked on devices which have Play Protect active or run on Android 10.
Follow this on version lower than Android 10
If you are looking at identifying & restricting the side-loaded app. Google has come up with the solution to identify the issue.
You can follow as below
Project's build.gradle
:
buildscript {
dependencies {
classpath 'com.android.tools.build:bundletool:0.9.0'
}
}
App module's build.gradle
:
implementation 'com.google.android.play:core:1.6.1'
Class that extends Application:
public void onCreate() {
if (MissingSplitsManagerFactory.create(this).disableAppIfMissingRequiredSplits()) {
// Skip app initialization.
return;
}
super.onCreate();
.....
}
With this integration, google will automatically identifies if there are any missing split apks and shows a popup saying "Installation failed" and it also redirects to Play store download screen where user can properly install the app via the Google Play store.
Check this link for more info.
Hope this helps.
© 2022 - 2024 — McMap. All rights reserved.