LoginSignup
34
9

More than 1 year has passed since last update.

【更新/解決】Android12にしたら、FLAG_IMMUTABLEまたはFLAG_MUTABLEエラーでアプリが起動しない

Last updated at Posted at 2021-10-21

概要

2021/10/20にGoogle Pixel系(Pixel3以降)にAndroid12(Android S、 API31)が配布されたので遅ればせながら、自作アプリの動作確認をしてみた。
が、、、、起動さえしないという恐怖の状況に陥ったことから、原因を調査してみる。

エラーの内容

手持ちのPixel3 XLにAndroid12をインストールして、開発者モードを有効にして、エラー原因を見ていく。
そうすると、どうもこんなエラーで落ちているようなのだ。

E/AndroidRuntime: FATAL EXCEPTION: pool-16-thread-1
    Process: com.sekai.tsukuru.app.quickmatome, PID: 25032
    java.lang.IllegalArgumentException: com.sekai.tsukuru.app.quickmatome: 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)

Android12_001.PNG

原因の調査

GooglePlayで配布している3つのアプリのいずれも同じ現象で起きることから、どうもコーディングが原因ではないようだ。
そこで、空のアプリを試しに作って同端末で動かしたところ、普通に起動した。

Firebaseか?

となると、3つに共通しているのは「Firebase」や「AdMob」といったところになる。

「【Android】Android 12対応をする前にplay-services-analyticsのバージョンを確認しよう」
https://qiita.com/HaSuzuki/items/eaacf1923c971bafb3ed

それっぽい記事はあるが、すでにFirebase Analyticsへ移行しているアプリなので、対象外のようだ。

AdMobか?

「Issue when targetting android 12 sdk 31」
https://support.google.com/admob/thread/122436670/issue-when-targetting-android-12-sdk-31?hl=en-GB

リプライはついていないが、どうも怪しい。

【10/21 13:00 Update】
Incompatible with Android S
https://groups.google.com/g/google-admob-ads-sdk/c/uCQkZcqOVtE

どうやら、「play-services-ads」に「androidx.work:work-runtime」への依存関係があり、Work側で今回の問題が起きているようだ。

■WorkManager:Release Note
https://developer.android.com/jetpack/androidx/releases/work
→上記によると、「Android 12(S)をターゲットとするアプリでは、WorkManager バージョン 2.7.0 が必要です。」という記述がある。

一方で「play-services-ads」のRelease Noteには、Work 2.7.0への依存関係に関する記述がない
https://developers.google.com/admob/android/rel-notes#20.4.0
→Android 11対応の際には、注記があるが、12に関してはない。ということから判断。

【11/14 Update】
https://developers.google.com/admob/android/rel-notes#20.4.0
ようやく、「play-services-ads」のRelease Noteに追記されましたね・・・
結局、回避手段として記載していた内容が、現状の「最適解」になってしまった・・・
20211114001.PNG

【2022/01/29 Update】
https://developers.google.com/admob/android/rel-notes#20.5.0
気づくのが遅かったのですが、20.5.0がリリースされていました。
20.5.0を指定すれば、前述の「Work 2.7.0」の追記は不要っぽいので、20.5.0へ記載を変更して「Work 2.7.0」の記載を消しました。
Android12での動作は問題なかったので、特段の事情で20.4.0以下を使用しないといけない場合以外は、20.5.0へ記載を更新すれば簡単解決ですね。

試してみる

Android12にしたPixel3 XLで動いた空のアプリに、AdMobを追加してみる。

E/AndroidRuntime: FATAL EXCEPTION: pool-3-thread-1
    Process: com.sekai.tsukuru.app.and12test, PID: 31767
    java.lang.IllegalArgumentException: com.sekai.tsukuru.app.and12test: 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)

発生した・・・・。そして同じように起動さえしない・・・

AdMobの利用において、指定しているバージョンは下記のように「20.4.0」
https://developers.google.com/admob/android/quick-start?hl=ja

dependencies {
  implementation 'com.google.android.gms:play-services-ads:20.4.0'
}

解決方法

実は、まだ調査中です。
情報あればください。

【10/21 13:00 Update】
回避策として、AdMobの項にて記載されていた方法をとることでいけそう。
play-service-adsが使用しているWorkのバージョンを2.7.0に明示することでAndroid12対応にする。

build.gradle(アプリ)
    implementation 'com.google.android.gms:play-services-ads:20.4.0'
    constraints {
        implementation('androidx.work:work-runtime:2.7.0') {
            because 'previous versions have a bug impacting this application'
        }
    }
34
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
9