Dmitry Ginzburg
Dmitry Ginzburg

Reputation: 7461

Check if an application is installed from Google Play

I work on an application and I add a feature which is specifically prohibited by Google Play rules. So I want this specific feature to be disabled for those who installed the application from Google Play without creating a different apk. That means that I need to determine from the code whether the application is installed from Google Play or just by downloading the apk.

Is there a way this be achieved?

Upvotes: 4

Views: 3559

Answers (1)

GoRoS
GoRoS

Reputation: 5375

This should work for that purpose, taken from HockeyAppSDK for Android Github library:

protected static boolean installedFromMarket(WeakReference<? extends Context> weakContext) {
    boolean result = false;

    Context context = weakContext.get();
    if (context != null) {
        try {
            String installer = context.getPackageManager().getInstallerPackageName(context.getPackageName());
            // if installer string is not null it might be installed by market
            if (!TextUtils.isEmpty(installer)) {
                result = true;

                // on Android Nougat and up when installing an app through the package installer (which HockeyApp uses itself), the installer will be
                // "com.google.android.packageinstaller" or "com.android.packageinstaller" which is also not to be considered as a market installation
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT) || TextUtils.equals(installer, INSTALLER_PACKAGE_INSTALLER_NOUGAT2))) {
                    result = false;
                }

                // on some devices (Xiaomi) the installer identifier will be "adb", which is not to be considered as a market installation
                if (TextUtils.equals(installer, INSTALLER_ADB)) {
                    result = false;
                }
            }

        } catch (Throwable ignored) {
        }
    }

    return result;
}

However, I'd suggest to build two different apps for that purpose. If you're including forbidden code in Google Play, although it won't be executed helped by this method, it will be part of your bytecode and it's very likely to be banned by Google. It's much more cleaner to strip that out of your code by creating another flavour.

Upvotes: 3

Related Questions