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

7

9

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'
Haughay answered 17/2, 2022 at 9:8 Comment(7)
Did you find any solution for this even I got the same error? Please post the answer.Erickaericksen
I have not found a solution yet.Haughay
Use this jar file for a temporary solution for ''org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'' until they fix the solution. This works for me github.com/hannesa2/paho.mqtt.android/releases/tag/3.3.0Erickaericksen
can you please work me through the procedureHaughay
Download the "serviceLibrary-release.aar" file from the github.com/hannesa2/paho.mqtt.android/releases/tag/3.3.0 add it to the project then remove the "'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1" dependency from Gradle. import the Service class from the jar library. "import info.mqtt.android.service.MqttAndroidClient". How to add the jar file to the project is in the below link "geeksforgeeks.org/…"Erickaericksen
Wow! you saved an entire project. Thanks a lot. @ErickaericksenHaughay
Hi. I have the same issue. I am working with this project: github.com/leonardocavagnis/MQTTClientSample_Android. And I get the same error. When I perform the changes above, that you recommend, I get a new error for the "import info.mqtt.android.service.MqttAndroidClient", it cannot resolve 'info'Drizzle
A
24

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.

Acyl answered 12/4, 2022 at 8:22 Comment(7)
What should i do on <service android:name="org.eclipse.paho.android.service.MqttService" /> at Android ManifestUnbalanced
Invaluable... You have covered all my issues in one post. Thank.Geotectonic
@Unbalanced it's gonna be in red. Let it be. The app would still work.Acyl
I tried this out, but Android Studio was unable to resolve com.github.hannesa2. I ended up downloading the AAR but using the rest of the steps described here.Catholicon
Works also on android 13 sdk 33!Swine
@Acyl do you have example code of build.gradle(both level) part?Footloose
works great, using it in productionMinimalist
E
4

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.

Erickaericksen answered 9/3, 2022 at 5:6 Comment(3)
Once again, thanksHaughay
Thanks!. Unfortunately I also had to add Timber to my project as the library contains a dependency on it :(Burka
For anyone trying to import the aar file and getting various errors: Just forget about the aar file and add the related dependencies to your build.gradle as shown in the GitHub page. Otherwise, it may throw some errors about missing libraries.Corporator
P
3

In case Android Studio is unable to resolve com.github.hannesa2:

//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@aar'
implementation 'androidx.room:room-runtime:2.3.0'
implementation 'com.jakewharton.timber:timber:5.0.1'

// Add below to fix Runtime error(ClassNotFoundException)
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

If you use Gradle 7 or later, add this below to settings.gradle:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}
Pindus answered 21/7, 2023 at 5:9 Comment(0)
S
0

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);

Secretive answered 23/2, 2022 at 12:7 Comment(6)
From my observation, the error is from the third party library (implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1') I am using. It does not have an update yet.Haughay
Are you using Eclipse or Android Studio, if Eclipse then I have no idea!Secretive
I am using android studioHaughay
are you using Latest gradle version?Secretive
Yes. I am using the current Gradle versionHaughay
Ohhh can i show your sample code where you got crash?Secretive
P
0

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

Pedraza answered 9/7, 2022 at 14:47 Comment(0)
T
0

The best way to solve this problem is by editing the source code. To do this, copy all library files to your project, then open the AlarmPingSender.java file and find the code below near line 75:

pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
                action), PendingIntent.FLAG_UPDATE_CURRENT)

Replace the code with the following:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
                action), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    } else {
        pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
                action), PendingIntent.FLAG_UPDATE_CURRENT);

    }

Now replace your 'import' with the new file. This should solve the problem.

Tell answered 22/2, 2023 at 9:7 Comment(0)
S
-1

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'

Secretive answered 21/2, 2022 at 12:36 Comment(4)
Thanks for the answer but it didn't solve the problemHaughay
Can you tell me the error what you get?Secretive
I posted another answer, hope resolved your issue.Secretive
The app crashes with the error aboveHaughay

© 2022 - 2024 — McMap. All rights reserved.