74

I need to detect my application is installed from google play or other market, how could I get this information?

Lin-Art
  • 4,933
  • 2
  • 26
  • 31
ttom
  • 863
  • 1
  • 7
  • 7

4 Answers4

62

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.

Community
  • 1
  • 1
MattC
  • 12,285
  • 10
  • 54
  • 78
  • @AbhishekB what version of Android are you on? Are you in an emulator? – MattC Dec 14 '12 at 19:47
  • 3
    @AbhishekB I do not know of a way to find the origination of side-loaded apps. I'm not sure how that would even be tracked since technically it's just an APK file that's loaded onto the device somewhere and then presumably installed via PackageManager. – MattC Dec 17 '12 at 19:46
  • The *getinstallerPackageName* link is returning a server error. So link appears to be down. – HasH_BrowN Dec 20 '14 at 08:13
  • we can set installer package name via adb. is there any other api by which we can check that app is installed via playstore. – Nikhil bohra Apr 27 '17 at 12:26
  • @MattC I have same app installed in Baidu Store in China and Playstore. How to identify in this situation since package name is same. – Anand Mar 20 '18 at 07:41
  • Google has come up with new Sideload Prevention API: https://developer.android.com/guide/app-bundle/sideload-check?hl=en-419 – Sreejith B Naick Jun 17 '19 at 08:54
  • How to know that the app is installed from the Huawei store? – Fazal Hussain Jul 29 '19 at 08:21
28

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".

mttmllns
  • 1,740
  • 1
  • 15
  • 13
  • Cannot make a static reference to the non-static method getInstallerPackageName(String) from the type PackageManager – Vivek Warde Apr 12 '15 at 05:31
  • Every appstore has their own marketapp on Playstore that in turn installs their apks (packages). The packagemanager getInstaller...() returns the package name of the marketapp which installed a given apk. Eg. com.amazon.venezia is the package name of the amazon marketapp. Pretty clever, because then PlayStore can control which marketapps are trusted. This is designed to eventually prevent ALL untrusted sideloads. Noticing that uptake of new android OS versions is falling dramatically. KitKat might become the defacto standard. – Dominic Cerisano Jul 24 '15 at 22:57
  • getInstallerPackageName() method returns correct value on Redmi Note 3 but returns null in HTC. Is this device dependant ? – Anand Apr 16 '18 at 12:17
  • 1
    If App is Pre installed in device lets say Camera, Calculator and other system app. You will get null value. – Chirag Savsani Nov 26 '18 at 13:20
  • Does anyone know how amazon app store sets the InstallerPackageName to heir package without root permissions? In latest Android versions (nougat/oreo/pie) the conventional way doesn't seem to work and you get the PackageInstaller as the app that installed the app. – jdandradex Feb 20 '19 at 17:14
19

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
    }
Philipp E.
  • 3,296
  • 3
  • 34
  • 52
9

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.

Naveen T P
  • 6,955
  • 2
  • 22
  • 29
  • 1
    I come across about this [blog](https://objectbox.io/app-bundle-and-sideloading-how-to-prevent-crashes/) and understood that this is only useful if using split apk feature. And in that this only work if split apk failed to read some depedeny. Is that correct ? – CoDe Dec 18 '19 at 09:02
  • This is useful only if your app support App bundle, if you app support traditional single APK delivery from play store. This won't work. – Zoombie Apr 06 '20 at 07:47
  • @Naveen T P Hi how can I test this if this actually works? It lets me install the app through android studio. – Adam Varhegyi Dec 13 '20 at 12:39
  • 2
    This is not available anymore. Google has removed those APIs. Check this: https://stackoverflow.com/questions/60524290/what-happened-to-googles-sideload-check-documentation – Abdallah Abdelazim Aug 21 '22 at 17:38