paho.mqtt.android icon indicating copy to clipboard operation
paho.mqtt.android copied to clipboard

App crashing due to PendingIntent flag FLAG_IMMUTABLE on targetting API 31 SDK

Open harpreet830 opened this issue 3 years ago • 31 comments

Please fill out the form below before submitting, thank you!

  • [ ] Bug exists Release Version 1.1.1 (Java Repository Master Branch)
  • [ ] Bug exists in Snapshot Version 1.1.2-SNAPSHOT (Android Service Repository Master Branch)
  • [ ] Bug is just in the Sample Application.

__Android API Version Bug Seen on: API 31

__Android Version Bug Seen on: Android 12

Please also check that if you have found the bug in the Release version (1.1.1) that you check that it also exists in the Snapshot (1.1.2-SNAPSHOT) before raising a bug.

Description of Bug:

Just initialize mqtt connection on a Android API 31 version device and you will get this exception. I have done this on Pixel 4A API S device. I'm using the following dependency - implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'

Console Log output (if available):

java.lang.IllegalArgumentException: Targeting S+ (version 10000 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:375) at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645) at android.app.PendingIntent.getBroadcast(PendingIntent.java:632) 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)

harpreet830 avatar Aug 24 '21 12:08 harpreet830

I have the same situation as you

lightningrealm avatar Aug 27 '21 16:08 lightningrealm

Same here. Should be a very simple fix, but looking at the commit history, I am wondering if there will be any or if it is time to migrate to something else...

Staacks avatar Aug 30 '21 21:08 Staacks

I have the same situation as you

lindabang avatar Sep 09 '21 08:09 lindabang

Did you tried this more modern repo https://github.com/hannesa2/paho.mqtt.android ? You can give it a try

hannesa2 avatar Sep 12 '21 05:09 hannesa2

@hannesa2 Are you sure your library won't reproduce the same error? I see that you also do not pass the FLAG_IMMUTABLE flag when creating a PendingIntent in AlarmPingSender line 53...

rushsports avatar Sep 22 '21 10:09 rushsports

@rushsports As far as I understand I run app with 31 https://github.com/hannesa2/paho.mqtt.android/blob/master/extendedSample/build.gradle#L13 , library comes with 31 https://github.com/hannesa2/paho.mqtt.android/blob/master/serviceLibrary/build.gradle#L13 and Espresso tests are on a device api30 and api28 and I don't observe an error.

When you found an error, I would merge the pull request. But first I want to see the error on my tests

hannesa2 avatar Sep 22 '21 10:09 hannesa2

@hannesa2 it is reproducible. From android 12 it is mandatory to set FLAG_IMMUTABLE or FLAG_MUTABLE for a PendingIntent. In kotlin we did like this: https://github.com/Infomaniak/android-kDrive/blob/master/app/src/main/java/com/infomaniak/drive/data/sync/UploadNotifications.kt#L40

JorisBodin avatar Oct 05 '21 09:10 JorisBodin

@JorisBodin Please show me a failing test. I don't see it

hannesa2 avatar Oct 05 '21 09:10 hannesa2

@hannesa2 You can compile and launch our application on Android 12, as soon as it connects to MQTT the application crashes ...

JorisBodin avatar Oct 05 '21 09:10 JorisBodin

Probably your app will crash, but why mine not. Where is the diff ?

hannesa2 avatar Oct 05 '21 09:10 hannesa2

on Android 12 device?

lightningrealm avatar Oct 05 '21 10:10 lightningrealm

Anyway, switch to my repo (it's not this one !), make a pull request there and we will solve it.

hannesa2 avatar Oct 05 '21 10:10 hannesa2

on Android 12 device?

Ahh, there is the diff https://github.com/hannesa2/paho.mqtt.android/blob/master/.github/workflows/Android-CI-Espresso.yml#L16 it's Android 11 (API level 30)

hannesa2 avatar Oct 05 '21 10:10 hannesa2

Here you have the fix for Android 12 (api 31)

hannesa2 avatar Oct 05 '21 13:10 hannesa2

Is there anyone solved?

lightningrealm avatar Oct 16 '21 07:10 lightningrealm

Use the fork of @hannesa2, the problem is fixed

JorisBodin avatar Oct 18 '21 08:10 JorisBodin

I was facing this issue with room database on the @hannesa2 fork. If someone else is facing this, you can use my fork. I made changes over the latest release (v1.1.1) on the eclipse repo to make it work. You can use v1.1.3 release on my repo (also available on jitpack).

mukul29 avatar Dec 08 '21 07:12 mukul29

I have the same problem

movaffagh avatar Dec 26 '21 05:12 movaffagh

Same here!

mohammadfazelp avatar Dec 26 '21 05:12 mohammadfazelp

I followed your solution as below :

1- Imported serviceLibrary_release.aar (added in libs directory) 2- I also added in build.gradle (in root) allprojects { repositories { ... flatDir { dirs 'libs' } }

in build.gradle : implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5' implementation (name:'serviceLibrary_release', ext:'aar')

Voila , Build output : Execution failed for task ':...'. > 1 exception was raised by workers: java.lang.RuntimeException: Duplicate class info.mqtt.android.service.Ack found in modules jetified-serviceLibrary_release-runtime.jar @hannesa2

mohammadfazelp avatar Jan 01 '22 12:01 mohammadfazelp

@mohammadfazelp I don't know what I should do here in this dead repo. Maybe you give my fork a try ?

hannesa2 avatar Jan 17 '22 08:01 hannesa2

@mohammadfazelp @movaffagh @mukul29 The database issue is solved https://github.com/hannesa2/paho.mqtt.android/issues/249#issuecomment-1014245327

hannesa2 avatar Jan 23 '22 07:01 hannesa2

Here you have the fix for Android 12 (api 31)

thank you

YangShaoXiong avatar Apr 15 '22 07:04 YangShaoXiong

My app was forced to upgrade to Target 31. I encountered this problem and couldn't wait for the new version. I just forked and repackaged the version myself. implementation 'cn.bingerz.android:org.eclipse.paho.android.service:1.1.2-SNAPSHOT'

Update content: Support AndroidX. Upgrade Gradle to 7.x. Fix PendingIntent flag FLAG_IMMUTABLE issue. Avoid Permission Denial: getIntentForIntentSender() issue.

For specific modifications, please see: https://github.com/bingerz/flip-mqtt-android

bingerz avatar Apr 27 '22 09:04 bingerz

@hannesa2 I am facing an issue with your fork also.

https://github.com/hannesa2/paho.mqtt.android/issues/307

Slake07 avatar May 11 '22 05:05 Slake07

Will there be an official release from org.eclipse.paho.android with a fix for this android-12 issue?

gopaladhith avatar May 25 '22 13:05 gopaladhith

I have the same situation as you

FordGT40 avatar Jun 06 '22 07:06 FordGT40

same here

pricimus avatar Jun 13 '22 13:06 pricimus

same here

pricimus avatar Jun 13 '22 13:06 pricimus

how can we try to get this officially merged?

anujtripathi04 avatar Jun 15 '22 20:06 anujtripathi04