Android API 31 - WorkManager - PendingIntent - flag IMMUTABLE error

Viewed 2722

Exception: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

Code causing the exception: WorkManager.getInstance(context).createCancelPendingIntent(id)

build.gradle options:

compileSdkVersion 31

buildToolsVersion '30.0.3' (version 31.0.0 gives error: Installed Build Tools revision 31.0.0 is corrupted. Remove and install again using the SDK Manager).

Emulator: API 31 (with API S everything works fine)

dependencies:

// WorkManager

implementation "androidx.work:work-runtime-ktx:2.7.0-alpha05"

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

I think the problem could be in this method of 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");
}

That's because when I run the app on the emulator and log the version name I get: version name: REL

2 Answers

Based on this bug, it seems the fix is to add the dependency on androidx.core:core:1.7.0-alpha01 which includes a fix for BuildCompat.isAtLeastS().

It looks like what you are missing is a dependency on the latest version of android.core. That includes this change which has an important bug fix for BuildCompat.isAtLeastS().

Related