简体   繁体   中英

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

I am using 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' for mqtt service and the app keeps crashing on android 12 devices with the following crash logs

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)

This is the library I am using:

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

If you are using the MQTT library they have not updated for the Android 12. So when you use Android 12 as the target version it throws an error in PendingIntent. For a temporary solution, I had found a library they had upgraded for compatibility with Android 12. MQTT service library

Download the "serviceLibrary-release.aar" and add it to your project. Then remove the "'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1" dependency from Gradle. Use "import info.mqtt.android.service.MqttAndroidClient" wherever you are using.

How to import aar file

This solved my MQTT library issues.

You need to replace "PendingIntent.FLAG_ONE_SHOT" or "PendingIntent.FLAG_UPDATE_CURRENT" with "PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE"

example:

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

The Eclipse Paho MQTT library is not updated for Android 12 pending Intents. Until then, we can use this MQTT client instead. Instead of using a jar file which was advised in myself's answer , I would recommend to use gradle dependancies.

In App Gradle, comment this eclipse service dependancy:

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

& Add these instead where 3.3.5 is the current version:

//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'

Do not remove the eclipse client dependancy,

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

For imports, remove this eclipse import

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

& replace it with the new:

import info.mqtt.android.service.MqttAndroidClient;

For MQTT android client, add the last Ack param like this

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

Additionally, if you were using try catch with MqttException then you can comment it up as the new library doesn't require the same.

This is Java library that modified from paho.mqtt.android library. It is fixed for Android 12

Update the library of firebase:

implementation platform('com.google.firebase:firebase-bom:29.1.0') implementation 'com.google.firebase:firebase-messaging'

and remove implementation 'com.google.firebase:firebase-messaging:23.0.0'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM