检查应用是否从Google Play安装

4

我正在开发一个应用程序,并添加了一个被谷歌Play规则明确禁止的功能。因此,我希望对那些从Google Play下载并安装应用程序的用户禁用此特定功能,而不需要创建一个不同的apk来实现此目的。这意味着我需要从代码中确定应用程序是从Google Play安装还是通过下载apk安装。

是否有一种方法可以实现这个目标?


1
为什么不发布两个不同版本的应用程序,一个没有该功能(放在Play上),另一个包含它(放在其他地方)? - Joe C
@JoeC 在我这样做之前,我想确保我真的需要这样做。 - Dmitry Ginzburg
我猜那个被禁用的功能会被发现,因为它是你的代码库的一部分,无论你是否执行它。 - Marged
根据我理解,一个禁用的功能并不会使应用程序违反规则。@Marged - Dmitry Ginzburg
1个回答

3

这应该适用于那个目的,取材自HockeyAppSDK for Android Github :

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;
}

然而,我建议为此目的构建两个不同的应用程序。如果您在Google Play中包含禁止使用的代码,尽管它不会通过这种方法执行,但它将成为您的字节码的一部分,并且很可能被Google禁止。通过创建另一个版本来清除代码会更加干净。


2
这似乎正是我所寻找的。即使我选择第二个选项,拥有这个选项仍然很好。 - Dmitry Ginzburg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接