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

Add FLAG_IMMUTABLE for Android 12 #2079

Closed
pyricau opened this issue Feb 27, 2021 · 8 comments
Closed

Add FLAG_IMMUTABLE for Android 12 #2079

pyricau opened this issue Feb 27, 2021 · 8 comments
Milestone

Comments

@pyricau
Copy link
Member

pyricau commented Feb 27, 2021

Reported here: #2074 (comment)

E/AndroidRuntime: FATAL EXCEPTION: IntentService[HeapAnalyzerService]
    Process: net.imknown.android.forefrontinfo.debug:leakcanary, PID: 6854
    java.lang.IllegalArgumentException: net.imknown.android.forefrontinfo.debug: 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:367)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:452)
        at android.app.PendingIntent.getActivity(PendingIntent.java:437)
        at android.app.PendingIntent.getActivity(PendingIntent.java:401)
        at leakcanary.internal.activity.LeakActivity$Companion.createPendingIntent(LeakActivity.kt:181)
        at leakcanary.DefaultOnHeapAnalyzedListener.showNotification(DefaultOnHeapAnalyzedListener.kt:74)
        at leakcanary.DefaultOnHeapAnalyzedListener.onHeapAnalyzed(DefaultOnHeapAnalyzedListener.kt:66)
        at leakcanary.internal.HeapAnalyzerService.onHandleIntentInForeground(HeapAnalyzerService.kt:73)
        at leakcanary.internal.ForegroundService.onHandleIntent(ForegroundService.kt:55)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:78)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.os.HandlerThread.run(HandlerThread.java:67)

fun createPendingIntent(
context: Context,
screens: ArrayList<Screen>
): PendingIntent {
val intent = Intent(context, LeakActivity::class.java)
intent.putExtra("screens", screens)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
return PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

Fix:

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or PendingIntent.FLAG_IMMUTABLE
@igor-khrupin-life360
Copy link

Hello everyone. We also had the same issue. Please fix it.

@igor-khrupin-life360
Copy link

@pyricau Any update on it?

ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 18, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 18, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 18, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 18, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 19, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 19, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 19, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 19, 2021
ihrupin added a commit to ihrupin/leakcanary that referenced this issue Mar 19, 2021
pyricau pushed a commit that referenced this issue Mar 24, 2021
@pyricau
Copy link
Member Author

pyricau commented Mar 24, 2021

Fixed by #2090

@pyricau pyricau closed this as completed Mar 24, 2021
@pyricau pyricau added this to the 2.7 milestone Mar 24, 2021
@ihrupin
Copy link
Contributor

ihrupin commented Mar 25, 2021

@pyricau Thank you for merging my PR. It would be great to have a 2.7-alpha version for testing. Is it possible?

@pyricau
Copy link
Member Author

pyricau commented Mar 25, 2021

There should be a new snapshot version published as soon as a change is merged: https://square.github.io/leakcanary/faq/#how-do-i-use-the-snapshot-version

Let me know if the crashes are gone or if anything else remains, thx!

@fm-norton
Copy link

fm-norton commented Apr 14, 2021

@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
 }

@pyricau
Copy link
Member Author

pyricau commented Apr 15, 2021

@fm-norton thx! Can you open a PR?

@matiash
Copy link
Contributor

matiash commented May 11, 2021

Also ran into this issue. Opened #2124 with (I think) a better fix.

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

5 participants