본문 바로가기
IT/ERROR

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

by TechTonic 2022. 7. 4.
반응형

오류 내용

Android 12(S) 에서 앱 실행 시 아래 에러가 발생됩니다.

2022-07-04 03:06:35.342 9455-9507/com.test.test E/AndroidRuntime: FATAL EXCEPTION: pool-3-thread-1
    Process: com.test.test, PID: 9455
    java.lang.IllegalArgumentException: com.test.test: 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:174)
        at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        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)

 

오류 원인

API 레벨 30 이전까지, FLAG_IMMUTABLE이 설정되지 않는 한 PendingIntent는 기본적으로 변경 가능했다. 하지만 API 레벨 31 부터는 PendingIntent를 사용하면 FLAG 변수로 FLAG_IMMUTABLE 또는 FLAG_MUTABLE을 사용하여 PendingIntent 사용시 변경 가능성을 명시적으로 지정해야 합니다.(꼭 필요한 경우가 아니라면 FLAG_IMMUTABLE을 사용하도록 개발자 문서에 명시되어있다).

 

수정 내용

Android Studio 기준 Ctrl + Shift + F 클릭 후 PendingIntent 검색하여 프로젝트 내 PendingIntent 를 사용하는 부분을 아래와 수정해 줍니다.

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent 관련 라이브러리 추가. Build.gradle <Module>

implementation 'androidx.work:work-runtime-ktx:2.7.1'

위 내용으로 수정하면 Android 12(S) 단말에서 정상적으로 실행됩니다.

 

저의 경우 PendingIntent 를 코드상에서 사용하지 않는데 해당 에러가 발생되었습니다.

확인 결과 외부 라이브러리에서 PendingIntent 를 사용하는 경우가 있어 이 부분은 PendingIntent 관련 라이브러리만 추가해주면 됩니다.

 

반응형

댓글