eclipse / paho.mqtt.android

MQTT Android
Other
2.88k stars 862 forks source link

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

Open harpreet830 opened 2 years ago

harpreet830 commented 2 years ago

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

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

lightningrealm commented 2 years ago

I have the same situation as you

Staacks commented 2 years ago

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...

lindabang commented 2 years ago

I have the same situation as you

hannesa2 commented 2 years ago

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

rushsports commented 2 years ago

@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...

hannesa2 commented 2 years ago

@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

JorisBodin commented 2 years ago

@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

hannesa2 commented 2 years ago

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

JorisBodin commented 2 years ago

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

hannesa2 commented 2 years ago

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

lightningrealm commented 2 years ago

on Android 12 device?

hannesa2 commented 2 years ago

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

hannesa2 commented 2 years ago

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 commented 2 years ago

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

lightningrealm commented 2 years ago

Is there anyone solved?

JorisBodin commented 2 years ago

Use the fork of @hannesa2, the problem is fixed

mukul29 commented 2 years ago

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

movaffagh commented 2 years ago

I have the same problem

mohammadfazelp commented 2 years ago

Same here!

mohammadfazelp commented 2 years ago

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

hannesa2 commented 2 years ago

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

hannesa2 commented 2 years ago

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

YangShaoXiong commented 2 years ago

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

thank you

bingerz commented 2 years ago

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

Slake07 commented 2 years ago

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

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

gopaladhith commented 2 years ago

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

FordGT40 commented 2 years ago

I have the same situation as you

pricimus commented 2 years ago

same here

pricimus commented 2 years ago

same here

anujtripathi04 commented 2 years ago

how can we try to get this officially merged?

Javatraher commented 2 years ago

@hannesa2 Hello, I installed your library, a new error started popping up, here it is:"Exception 'org.eclipse.paho.client.mqttv3.MqttException' is never thrown in the corresponding try block".Tell me what to do with it.