8

Hey I am getting warning for pending intent. So I surrounded for check of checking sdk according to this question and this medium post. I am getting warning message Missing PendingIntent mutability flag

val pendingIntent: PendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
            } else {
                PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            }

enter image description here

How can I remove this warning message?

enter image description here

Zain
  • 37,492
  • 7
  • 60
  • 84
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127

2 Answers2

3

Your code seems OK, and I believe it's a bug in the Lint check as it's been
stated by @CommonsWare in comments. This could be fixed in the next releases of Android Studio

How can I remove this warning message?

If you just want to remove the annoying warning there is a hack in building the condition that clear the warning: by transferring the conditional to the flag:

val pendingIntent: PendingIntent = PendingIntent.getActivity(
    this,
    0,
    intent,
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    else PendingIntent.FLAG_UPDATE_CURRENT
)

Or at the worse case you'd suppress it by @SuppressLint("UnspecifiedImmutableFlag"), which I don't recommend.

Zain
  • 37,492
  • 7
  • 60
  • 84
2

Add pending intent like this

val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT )

If the error still remains and your targetSdkVersion = 31 then the error must caused because one of your dependencies is internally using WorkManager or your are directly using old version of WorkManager. To solve simply add this dependency

implementation 'androidx.work:work-runtime-ktx:2.7.0'
Muhammad Asad
  • 694
  • 6
  • 10
  • I already added this library in my project. – Kotlin Learner Jan 10 '22 at 15:52
  • 1
    @MuhammadAsad his `minSdkVersion` is 21 so a different linter warning comes up when using `FLAG_IMMUTABLE`. Also the linter warning is raised specifically on the lines of code in the question. – ksparrow Jun 10 '22 at 16:39