'How to check the app was installed by apk or not

I need to check the app was installed by apk or from google play.

I have an apk that was downloaded from google play, and also, the app was available on google play. I want to classify two cases.

I already read about this question below, but it is about which app store the application was installed by, And it was not I want.
How does android package manager know what to install from the market place web site?

Is there any method to check the app was installed by apk or not, programmatically by app itself? Any help will be appreciated.



Solution 1:[1]

First, find Installer package name, like:

String sourceId = getPackageManager()
    .getInstallerPackageName(getPackageName());

Then check the sourceId variable's value, like:

  • If App was installed from Google play store, said value should be:
com.android.vending
  • If installed from Google play store, then extracted APK, and installed again:
com.google.android.packageinstaller
  • If download from Google play store publish, App release list:
null

Solution 2:[2]

Try this one

PackageManager pm = getPackageManager();

if(pm.getInstallerPackageName(getPackageName()).equals("com.android.vending"){
   //App is from google play
   //do something
}

app with unknown source will return empty string

Solution 3:[3]

I use below code to check, if a apk was downloaded from a store or sideloaded:

private boolean isStoreVersion(Context context) {
    boolean isStoreVersion= false;

    try {
        String installer = context.getPackageManager()
                                    .getInstallerPackageName(context.getPackageName());
        isStoreVersion= !TextUtils.isEmpty(installer);
    } catch (Throwable e) {      
           e.printStackTrace();    
    }

    return isStoreVersion;

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Top-Master
Solution 2 Android_K.Doe
Solution 3 sm_