Pergunta

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

Foi útil?

Solução

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.

Outras dicas

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
    }

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top