Android API 31 - WorkManager - PendingIntent - 标记 IMMUTABLE 错误

iCl*_*ude 4 flags android android-pendingintent android-workmanager

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

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

build.gradle 选项

编译SdkVersion 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");
}
Run Code Online (Sandbox Code Playgroud)

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

Nic*_*lli 6

基于这个错误,修复似乎是添加依赖项,androidx.core:core:1.7.0-alpha01其中包括修复BuildCompat.isAtLeastS().

看来您缺少的是对最新版本的依赖android.core。其中包括更改,其中修复了BuildCompat.isAtLeastS().

  • 就我而言,这有帮助:`implementation 'androidx.work:work-runtime:2.7.0'`,虽然我根本不使用 WorkManager,但依赖库之一使用它。 (5认同)