Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing FLAG_IMMUTABLE #2123

Closed
pyricau opened this issue May 10, 2021 · 3 comments
Closed

Missing FLAG_IMMUTABLE #2123

pyricau opened this issue May 10, 2021 · 3 comments

Comments

@pyricau
Copy link
Member

pyricau commented May 10, 2021

From #2079 (comment)

@pyricau I'm getting a similar crash related to PendingIntent.FLAG_IMMUTABLE when I'm launching my project on an Android 12 emulator. The crash occurs when LeakCanary is trying to make the first heap dump. I caught this both in 2.7 release and 2.8-SNAPSHOT versions.

E/AndroidRuntime: FATAL EXCEPTION: LeakCanary-Heap-Dump
    Process: com.example.myapp, PID: 7806
    java.lang.IllegalArgumentException: com.example.myapp: Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:648)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:634)
        at leakcanary.internal.NotificationReceiver$Companion.pendingIntent(NotificationReceiver.kt:48)
        at leakcanary.internal.HeapDumpTrigger.showRetainedCountNotification(HeapDumpTrigger.kt:377)
        at leakcanary.internal.HeapDumpTrigger.checkRetainedCount(HeapDumpTrigger.kt:309)
        at leakcanary.internal.HeapDumpTrigger.checkRetainedCount$default(HeapDumpTrigger.kt:255)
        at leakcanary.internal.HeapDumpTrigger.checkRetainedObjects(HeapDumpTrigger.kt:132)
        at leakcanary.internal.HeapDumpTrigger.access$checkRetainedObjects(HeapDumpTrigger.kt:29)
        at leakcanary.internal.HeapDumpTrigger$scheduleRetainedObjectCheck$1.run(HeapDumpTrigger.kt:334)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.os.HandlerThread.run(HandlerThread.java:67)

Since this comes through NotificationReceiver's pendingIntent() where the flags are set, I suggest fixing the if statement in the line 43 and other same if statements (use >= sign because SDK_INT constant shows us the release version code while Android 12 is in Developer Preview status at the moment).

 val flags = if (Build.VERSION.SDK_INT >= 30) {
        PendingIntent.FLAG_IMMUTABLE
 }
@imknown
Copy link

imknown commented Jul 1, 2021

Please try:

import androidx.core.os.BuildCompat

val flags = if (BuildCompat.isAtLeastS()) {
        PendingIntent.FLAG_IMMUTABLE
}

Ref: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-main/core/core/src/main/java/androidx/core/os/BuildCompat.java#158

/**
* Checks if the device is running on a pre-release version of Android S or a release version of
* Android S or newer.
*
* @return {@code true} if S APIs are available for use, {@code false} otherwise
*/
@ChecksSdkIntAtLeast(api = 31, codename = "S")
public static boolean isAtLeastS() {
    return VERSION.SDK_INT >= 31 || isAtLeastPreReleaseCodename("S", VERSION.CODENAME);
}

@ZacSweers
Copy link
Contributor

This is fixed on main already by #2124

@pyricau
Copy link
Member Author

pyricau commented Dec 15, 2021

Confirmed this is already fixed on main.

@pyricau pyricau closed this as completed Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants