Joseph Ofem
Joseph Ofem

Reputation: 384

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'

Upvotes: 11

Views: 13345

Answers (7)

Victor Han
Victor Han

Reputation: 31

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

Upvotes: 3

Gmacv
Gmacv

Reputation: 603

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.

Upvotes: 28

Mehrdad
Mehrdad

Reputation: 1637

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.

Upvotes: 0

Somsak Elect
Somsak Elect

Reputation: 31

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

Upvotes: 0

Myself
Myself

Reputation: 168

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.

Upvotes: 4

Dharmik Modi
Dharmik Modi

Reputation: 37

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

Upvotes: 0

Dharmik Modi
Dharmik Modi

Reputation: 37

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'

Upvotes: -1

Related Questions