リリース前レポートでエラー「Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or ~」の対処メモ

ゲーム制作

アドセンスが閉鎖になったことに伴いAdmobも使えなくなったので、AdmobからNendに広告サービスを変更してアップデートしました。その時、リリース前レポートでエラーが出ていたのでその対処法をメモしておきます。

結論から言うとbuild.gradleに一行記載を追加する必要があります。
同じような状況になっている人の参考になれば幸いです。

エラー内容

スタックトレース

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.
日本語翻訳:
FLAG_IMMUTABLE の使用を強く推奨します。FLAG_MUTABLE は、PendingIntent が
mutable
であることに依存する機能がある場合にのみ使用してください。例えば、インラインの返信やバブルと一緒に使用する必要がある場合などです。
「もっと見る」をクリックして詳細を見てみます。

詳細

Exception java.lang.IllegalArgumentException: com.xxx.xxxxx: 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)
日本語翻訳:
Exception java.lang.IllegalArgumentException: com.xxx.xxxxx: S+
(バージョン 31 以降) をターゲットとする場合、PendingIntent の作成時にFLAG_IMMUTABLE または FLAG_MUTABLE のいずれかを指定する必要があります。

Unityでの直し方

Unityでプレイヤー設定から「gradleTemplate.properties」と「mainTemplate.gradle」を追加して「mainTemplate.gradle」を書き換えます。

カスタムメインGradleテンプレートの追加

Unityの「編集」から「プロジェクト設定」を選択しウィンドウを開いて「Player」タブを開く
「公開設定」のビルド欄にある「カスタムメインGradleテンプレート」と「カスタムGradleプロパティーテンプレート」にチェックを入れる

プロパティファイルが無いとExternalDependencyManagerのAndroidResolverで呼び出すForce
Resolveが失敗してしまいます。

「mainTemplate.gradle」の書き換え

mainTemplate.gladleファイルを開くと以下の記述があります。

dependencies {
          implementation fileTree(dir: 'libs', include: ['*.jar'])
      }

dependencies内に以下の1行を追加します。

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

Android ResolverでForce Resolveを行う

Unityの「アセット」から「ExternalDependencyManager」にある「AndroidResolver」内にある「Force Resolve」を選択する。

 

Force Resolveに成功すると、mainTemplate.gradleに書き込みされてビルドが通るようになります。
あとはビルドでファイル生成してGoogleコンソールにアップしてください。
エラーが表示されなくなります。

さいごに

以上がリリース前レポートエラー「Targeting S+ (version 31 and above) requires
that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a
PendingIntent. 」の直し方になります。
理屈などは他のサイトで解説されていると思うのでそちらを探してみてください。
この記事ではあくまで自分用のメモという形で残しておきますが、同じような境遇の人に少しでも参考になれば幸いです。

コメント

タイトルとURLをコピーしました