LoginSignup
4
2

More than 1 year has passed since last update.

targetSdkVersionを31にするときの3つの注意点

Posted at

2022年11月から、GooglePlayストアにアプリのアップデートを公開する場合、targetSdkVersionを31にしないと公開できなくなりました。

機能を追加したし、あとはtargetSdkVersionを上げるだけ・・・
ってビルドできない!動かない!

という事に今回遭遇したので、targetSdkVersionを31にした時の注意点3選を書いていきたいと思います。

1.Android 12端末でアプリ起動時のIllegalArgumentExceptionに対応する

Android 12端末で起動しようとすると以下のエラーが吐かれてアプリ落ちします。
Android 11以前であれば普通に起動します。

Fatal Exception: java.lang.IllegalArgumentException: com.highcom.todolog: 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.

エラー内容と紐づかないのですが調べてみると、app/build.gradleに以下のようにandroidx.workのバージョンを明示してあげる事で回避できました。

dependencies {
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
・・・
    constraints {
        implementation('androidx.work:work-runtime:2.7.0') {
            because 'previous versions have a bug impacting this application'
        }
    }
・・・
    testImplementation 'junit:junit:4.13.2'
}

2.RuntimeException: Manifest merger failed with multiple errorsに対応する

AndroidManifest.xmlに明示的に、android:exportedの設定をする必要がありました。
これは、インテントフィルタを設定していたり、ブロードキャストレシーバを受信するアクティビティやサービスには、明示的な設定をする必要があります。
自身のアプリ内ではなく、ブロードキャストなど外部から呼び出される場合には、trueを設定する必要があります。

なので、Widgetなどの機能がある場合には、trueを設定する必要があります。

    <application
        android:theme="@style/Theme.ToDoLog">
        <activity android:name=".LogChartActivity"/>
        <activity android:name=".widget.ToDoAppWidgetConfigure"
            android:exported="true">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

        <receiver android:name=".widget.ToDoAppWidgetProvider"
            android:exported="true">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/todo_appwidget_info" />
        </receiver>
    </application>

3.ウィジェットを配置する時のRuntimeException: Unable to start receiverに対応する

上記2つの対応で起動は出来るようになったのですが、いざウィジェットを配置すると以下のRuntimeExceptionが発生してしまいました。

 java.lang.RuntimeException: Unable to start receiver com.highcom.todolog.widget.ToDoAppWidgetProvider: java.lang.IllegalArgumentException: com.highcom.todolog: 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.

PendingIntentを利用する場合、明示的に、PendingIntent.FLAG_MUTABLEorPendingIntent.FLAG_IMMUTABLEを指定する必要がありました。
アプリを起動するためのintentputExtraする場合は、FLAG_MUTABLEにする必要があります。

    Intent titleIntent = new Intent(context, ToDoMainActivity.class);
    titleIntent.putExtra(ToDoAppWidgetConfigure.SELECT_WIDGET_GROUP_ID, selectGroupId);
    titleIntent.setData(Uri.parse(titleIntent.toUri(Intent.URI_INTENT_SCHEME)));
    PendingIntent titlePendingIntent = TaskStackBuilder.create(context)
            .addNextIntentWithParentStack(titleIntent)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);

以上、targetSdkVersionを31にした時の3つの注意点でした!

4
2
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
4
2