8

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'
hardillb
  • 54,545
  • 11
  • 67
  • 105
Joseph Ofem
  • 304
  • 2
  • 15
  • Did you find any solution for this even I got the same error? Please post the answer. – Myself Feb 18 '22 at 17:55
  • I have not found a solution yet. – Joseph Ofem Feb 20 '22 at 13:34
  • 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 https://github.com/hannesa2/paho.mqtt.android/releases/tag/3.3.0 – Myself Feb 21 '22 at 09:37
  • can you please work me through the procedure – Joseph Ofem Mar 01 '22 at 15:03
  • Download the "serviceLibrary-release.aar" file from the https://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 "https://www.geeksforgeeks.org/how-to-import-external-jar-files-in-android-studio/" – Myself Mar 06 '22 at 15:41
  • Wow! you saved an entire project. Thanks a lot. @Myself – Joseph Ofem Mar 08 '22 at 10:23
  • Hi. I have the same issue. I am working with this project: https://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' – garrettb May 19 '23 at 17:45

7 Answers7

20

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.

Joel
  • 2,230
  • 1
  • 20
  • 28
Gmacv
  • 383
  • 3
  • 6
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.

Myself
  • 168
  • 1
  • 2
  • 10
  • Once again, thanks – Joseph Ofem Mar 09 '22 at 13:05
  • Thanks!. Unfortunately I also had to add Timber to my project as the library contains a dependency on it :( – codeman Apr 19 '22 at 07:53
  • 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. – Emre Can Serteli Jun 21 '22 at 11:51
1

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' }
    }
}
Community
  • 1
  • 1
Victor Han
  • 11
  • 3
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);

0

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

Somsak Elect
  • 31
  • 1
  • 6
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.

Mehrdad
  • 1,477
  • 15
  • 17
-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'