【问题标题】:Android API 31 - WorkManager - PendingIntent - flag IMMUTABLE errorAndroid API 31 - WorkManager - PendingIntent - 标记 IMMUTABLE 错误
【发布时间】:2021-07-27 14:15:40
【问题描述】:

例外: 定位 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

导致异常的代码: WorkManager.getInstance(context).createCancelPendingIntent(id)

build.gradle 选项

compileSdkVersion 31

buildToolsVersion '30.0.3'(版本 31.0.0 出现错误:安装的构建工具修订版 31.0.0 已损坏。使用 SDK 管理器删除并重新安装)。

模拟器: API 31(使用 API S 一切正常)

依赖项

// 工作管理器

实现“androidx.work:work-runtime-ktx:2.7.0-alpha05”

实现 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'

我认为问题可能出在 BuildCompat.java 的这种方法中:

/**
 * Checks if the device is running on a pre-release version of Android S or a release version of
 * Android S or newer.
 * <p>
 * <strong>Note:</strong> When Android S is finalized for release, this method will be
 * deprecated and all calls should be replaced with {@code Build.VERSION.SDK_INT >=
 * Build.VERSION_CODES.S}.
 *
 * @return {@code true} if S APIs are available for use, {@code false} otherwise
 */
@ChecksSdkIntAtLeast(codename = "S")
public static boolean isAtLeastS() {
    return VERSION.CODENAME.equals("S");
}

那是因为当我在模拟器上运行应用程序并记录我得到的版本名称时: 版本名称:REL

【问题讨论】:

    标签: android android-pendingintent flags android-workmanager


    【解决方案1】:

    基于this bug,似乎修复是添加对androidx.core:core:1.7.0-alpha01 的依赖,其中包括对BuildCompat.isAtLeastS() 的修复。

    看起来您缺少的是对android.core 最新版本的依赖。这包括 this 更改,其中对 BuildCompat.isAtLeastS() 进行了重要的错误修复。

    【讨论】:

    • 是的,这就是修复。谢谢。
    • 在我的情况下,这有帮助:implementation 'androidx.work:work-runtime:2.7.0',虽然我根本不使用 WorkManager,但有一个依赖库使用它。
    【解决方案2】:

    如下设置您的待处理意图

    val updatedPendingIntent = PendingIntent.getActivity(
       applicationContext,
       NOTIFICATION_REQUEST_CODE,
       updatedIntent,
       PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
    )

    根据本文档

    https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2023-02-22
      • 1970-01-01
      相关资源
      最近更新 更多