The question:
I need to detect my application is installed from google play or other market, how could I get this information?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
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.
Method 2
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"
.
Method 3
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
}
Method 4
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0