首页 > 解决方案 > 为什么 Android 应用内更新总是返回 UPDATE_NOT_AVAILABLE 并且版本代码为 0?

问题描述

我正在使用这些说明从 Android Studio 测试应用内更新。

为了测试,我删除了我的应用程序并在 gradle 中设置了一个次要版本代码,然后我将这个版本的应用程序从调试安装到我的设备中。

上面的代码总是返回UPDATE_NOT_AVAILABLE

代码:

  private void checkForUpdates() {
        // Creates instance of the manager.
        AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(MainActivity.this);

        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

        // Checks that the platform will allow the specified type of update.
        Log.d(TAG, "upd_1:" + appUpdateManager.getAppUpdateInfo());

        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            Log.d(TAG, "upd_2:" + appUpdateInfo.updateAvailability());
            Log.d(TAG, "upd_3:" + appUpdateInfo.availableVersionCode());

            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    // For a flexible update, use AppUpdateType.FLEXIBLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                // Request the update.
                try {
                    appUpdateManager.startUpdateFlowForResult(
                            // Pass the intent that is returned by 'getAppUpdateInfo()'.
                            appUpdateInfo,
                            // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                            AppUpdateType.IMMEDIATE,
                            // The current activity making the update request.
                            this,
                            // Include a request code to later monitor this update request.
                            MY_REQUEST_CODE);
                } catch (IntentSender.SendIntentException e) {
                    e.printStackTrace();
                }
            }
        });

    }

这是我的日志:

这是日志updateAvailability()

2019-12-05 17:00:39.436 3381-3381/org.my.app D/MainActivity: upd_2:1

这是日志availableVersionCode()

2019-12-05 17:00:39.436 3381-3381/org.my.app D/MainActivity: upd_3:0

我如何测试更新应用内 previos 以上传我的这个应用的新版本?

标签: androidupdates

解决方案


在这里聚会有点晚了,但希望这可以帮助将来的其他人:

您收到错误响应的原因是您尝试提供更新的新 APK 和您当前安装的 APK 必须由您的生产密钥(也称为发布版本)签名。使用调试版本将导致此调用每次都失败。

为了正确测试这一点,我建议将发布的 APK 上传到 Google Developer Console 上的内部测试轨道。像这样的东西:

  • 将版本上传x.x.1到内部测试轨道,并使用链接下载到您的设备
  • 将碰撞版本上传x.x.2到内部测试轨道
  • 在 Play 商店中导航到您的应用(应显示可用更新)
  • 进入您的应用程序并测试您期望的用户流

此处有更多详细信息:在调试设备上进行测试时显示 UPDATE_NOT_AVAILABLE 的应用内更新 API


推荐阅读