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

HEETAE HEO·2023년 3월 17일
0
post-thumbnail

사건 개요

토이 프로젝트 UI 작업을 하려고 앱을 키는 순간 갑자기 앱이 켜지는 모션과 함께 바로 꺼져 버린다 이게... 무슨일이지...?

당황한 나는 Log를 봤는데 다음과 같은 이슈가 있었고 로그 전체는 아래와 같이 작성되어 있었습니다.

E/AndroidRuntime: FATAL EXCEPTION: pool-5-thread-1
    Process: "프로젝트 명", PID: 7415
    java.lang.IllegalArgumentException: "프로젝트 명": 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:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        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:1137)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
        at java.lang.Thread.run(Thread.java:1012)

발생 원인을 찾아본 결과 이유는 다음과 같았습니다.

발생 원인

Android 12(S버전, API 레벨 31이상)에서 PendingIntent를 생성할 때 FLAG_IMMUTABLE 또는 FLAG_MUTABLE 중 하나를 지정해야 하는 요구사항을 충족하지 못한 것입니다. 이로 인해 IllegalArgumentException이 발생했습니다.

PendingIntent 간단 설명

PendingIntent는 앱이 다른 구성요소(예: 알림, 앱 위젯 등)에게 실행할 수 있는 작업을 전달하는 데 사용되는 객체입니다. 이 객체는 인텐트(Intent)를 포함하며, 나중에 발생할 작업을 나타냅니다.

FLAG 설명

FLAG_IMMUTABLE: 이 플래그를 사용하면 생성된 PendingIntent가 변경되지 않도록 합니다. 이는 안전하고 예상 가능한 작동을 보장하기 위한 것입니다. 보안상 이유로 대부분의 경우 이 플래그를 사용하는 것이 좋습니다.

FLAG_MUTABLE: 이 플래그를 사용하면 생성된 PendingIntent를 변경할 수 있습니다. 이는 특정 상황에서 유연성이 필요할 때 유용할 수 있지만, 변경 가능성 때문에 보안상의 위험이 발생할 수 있습니다. 이 플래그는 신중하게 사용해야 합니다.

// FLAG_IMMUTABLE 사용 예
val pendingIntent = PendingIntent.getActivity(
    context,
    requestCode,
    intent,
    PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)

// FLAG_MUTABLE 사용 예
val pendingIntent = PendingIntent.getActivity(
    context,
    requestCode,
    intent,
    PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)

일반적인 해결방법

해결방법은 다음과 같습니다. PendingIntent를 생성할 때 FLAG_IMMUTABLE 또는 FLAG_MUTABLE 중 하나를 명시적으로 지정해야 합니다. 일반적으로는 FLAG_IMMUTABLE을 사용하는 것이 권장되며, PendingIntent가 변경 가능해야 하는 경우(예: 인라인 답장이나 버블과 같이 사용해야 하는 경우)에만 FLAG_MUTABLE을 사용합니다.

// 예시 코드

val intent = Intent(context, YourReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)

???: 일반적인 해결 방법이요?? 다른 해결방법은 뭔가요??

일반적이지 않은 해결방법

놀랍게도 저는 pendingIntent를 사용하지 않았습니다... 아니... 이게 무슨...

자료를 찾아보던 중 androidx.work 라이브러리의 버전이 낮으면 같이 이슈가 발생할 수 도 있다고 나와 라이브러리를 찾아보는데 두 번째 공포 implements가 되어 있지않습니다.... 어..라...?

그렇기에 설마 하며 최신 버전의 라이브러리를 implements를 해주니 정상작동하네요 휴~ 다행이다....

이번 글은 해당 이슈를 접할 수 있는 개발자가 있을 수 있기에 짧지만 이슈 글을 작성해보았습니다. 읽어주셔서 감사합니다!!!

profile
Android 개발 잘하고 싶어요!!!

0개의 댓글