Android 12 メモ - Android 12 SDKでビルドしたものがクラッシュした時の対応について(Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.)

compileSdkVersion 'android-S'
buildToolsVersion '31-rc4'
targetSdkVersion 'S'

build.gradleに↑を設定してビルドしてみた

ビルドできたけど、アプリを起動するとクラッシュして↓のログがでる。

java.lang.IllegalArgumentException: 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.

PendingIntentにFLAG_IMMUTABLEかFLAG_MUTABLEを付けろということだったので、PendingIntentを使ってるところにFLAG_IMMUTABLEを設定してみた。しかし、同じエラーがでる。

原因がわからないので、新規プロジェクトを作ってPendingIntentにFLAG_IMMUTABLEかFLAG_MUTABLEを付けずにわざと、エラーを出してみた。

Caused by: java.lang.IllegalArgumentException: xxx.myapplication: 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:386)
       at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:657)
       at android.app.PendingIntent.getBroadcast(PendingIntent.java:644)
       at xxx.myapplication.MainActivity.createDeletePendingIntent(MainActivity.kt:32)
       at xxx.myapplication.MainActivity.onCreate(MainActivity.kt:18)
       at android.app.Activity.performCreate(Activity.java:8006)
       at android.app.Activity.performCreate(Activity.java:7986)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3547)

↑のログをみると、発生箇所がちゃんと出力されている。

よくわからないので、PendingIntentを使ってる箇所を全部コメントアウトしてみた。けど、エラーがでて起動できず。

諦めてたら、同僚がこれをbuild.gradleに追加したら起動できたと教えてくれた。

implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha03'

これ追加したら、ほんとにビルドできた。



このドキュメントは "Chatwork Tech Tips" の一環です。
その他の Tips も含めて @Chatwork_dev にて確認することができます 👨‍🏫

いいなと思ったら応援しよう!