개발/Android

안드로이드12 대응 - PendingIntent 빌드 오류

귀염둥이 팡무 2022. 7. 11. 02:11

Build Error Log

2022-02-25 13:43:50.714 3448-3498/com.autocrypt.mi.bf2u.barrierfree E/AndroidRuntime: FATAL EXCEPTION: pool-23-thread-1
Process: com.autocrypt.mi.bf2u.barrierfree, PID: 3448
java.lang.IllegalArgumentException: com.autocrypt.mi.bf2u.barrierfree: Targeting S+ (version 31 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:382)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:660)
at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:196)
at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:128)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:93)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)

원인

안드로이드 12부터 PendingIntent 사용 시 FLAG_IMMUTABLE 또는 FLAG_MUTABLE 를 정의해주어야 한다.

FLAG_MUTABLE: 변경 가능
ex) Direct Reply Action, Bubble, etc….
FLAG_IMMUTABLE: 변경 불가능

해결

1. PendingIntent를 사용하지 않는 경우 간단하게 dependency를 추가하여 해결할 수 있다.

implementation("androidx.work:work-runtime-ktx:2.7.1") // 2022. 02. 25 기준 최신 버전

2. 만일 PendingIntent를 사용하고 있는 경우 아래처럼 FLAG를 추가해주면 된다.

Before

val pendingIntent = PendingIntent.getActivity(this, 0, resume, 0)

After 1) 변경 가능한 경우

val pendingIntent = PendingIntent.getActivity(this, 0, resume, PendingIntent.FLAG_MUTABLE)

After 2) 변경 불가능한 경우

val pendingIntent = PendingIntent.getActivity(this, 0, resume, PendingIntent.FLAG_IMMUTABLE)

최초 작성일: 2022. 02. 25