Android paho mqtt 崩溃 Android 12 - 针对 S+(版本 31 及以上)需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

Android paho mqtt crashes Android 12 - Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

我正在使用 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' 作为 mqtt 服务,应用程序在 android 12 台设备上不断崩溃,崩溃日志如下

java.lang.IllegalArgumentException: app id: 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:382)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:660)
        at org.eclipse.paho.android.service.AlarmPingSender.start(AlarmPingSender.java:76)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.connected(ClientState.java:1214)
        at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:1050)
        at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:151)

这是我正在使用的库:

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

更新firebase库:

实施平台('com.google.firebase:firebase-bom:29.1.0') 实施 'com.google.firebase:firebase-messaging'

并删除实施 'com.google.firebase:firebase-messaging:23.0.0'

您需要将“PendingIntent.FLAG_ONE_SHOT”或“PendingIntent.FLAG_UPDATE_CURRENT”替换为“PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE”

示例:

alarmIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AutostartReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

如果您使用的是 MQTT 库,它们尚未针对 Android 12 进行更新。因此,当您使用 Android 12 作为目标版本时,它会在 PendingIntent 中引发错误。 作为临时解决方案,我找到了一个库,他们已升级为与 Android 12 兼容。 MQTT service library

下载“serviceLibrary-release.aar”并将其添加到您的项目中。然后从 Gradle 中删除“'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1” 依赖项。在任何地方使用“import info.mqtt.android.service.MqttAndroidClient”。

How to import aar file

这解决了我的 MQTT 库问题。

Eclipse Paho MQTT library is not updated for Android 12 pending Intents. Until then, we can use this MQTT client代替。 我建议使用 gradle 依赖关系,而不是使用 中建议的 jar 文件。

在应用Gradle中,评论这个eclipse服务依赖:

implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

& 在 3.3.5 是当前版本的地方添加这些:

//new mqtt library that supports android 12
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.hannesa2:paho.mqtt.android:3.3.5'

不要删除 eclipse 客户端依赖,

implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5

对于导入,删除这个 eclipse 导入

import org.eclipse.paho.android.service.MqttAndroidClient;

& 将其替换为新的:

import info.mqtt.android.service.MqttAndroidClient;

对于 MQTT android 客户端,像这样添加最后一个 Ack 参数

client = new MqttAndroidClient(context, serverURI, clientId, Ack.AUTO_ACK);

此外,如果您将 try catch 与 MqttException 一起使用,那么您可以对其进行评论,因为新库不需要相同的内容。