BirjuVachhani/locus-android

Pending intents need to have either FLAG_IMMUTABLE or FLAG_MUTABLE

Zee-BA opened this issue · 16 comments

When trying to run my app on an API 31 emulator, I get a crash with the following stack track:

java.lang.IllegalArgumentException: 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:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at com.birjuvachhani.locus.LocationBroadcastReceiver$Companion.getPendingIntent(LocationBroadcastReceiver.kt:41)
at com.birjuvachhani.locus.LocationProvider$pendingIntent$2.invoke(LocationProvider.kt:40)
at com.birjuvachhani.locus.LocationProvider$pendingIntent$2.invoke(LocationProvider.kt:39)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.birjuvachhani.locus.LocationProvider.getPendingIntent(LocationProvider.kt:39)
at com.birjuvachhani.locus.LocationProvider.startUpdates$locus_release(LocationProvider.kt:56)
at com.birjuvachhani.locus.Locus.startUpdates(Locus.kt:248)
at com.birjuvachhani.locus.Locus.access$startUpdates(Locus.kt:81)
at com.birjuvachhani.locus.Locus$checkAndStartLocationUpdates$2.invoke(Locus.kt:209)
at com.birjuvachhani.locus.Locus$checkAndStartLocationUpdates$2.invoke(Locus.kt:206)
at com.birjuvachhani.locus.Locus.checkLocationSettings$lambda-3(Locus.kt:233)
at com.birjuvachhani.locus.Locus.$r8$lambda$9D3IJ2F1aenyoyH999DecV6GSsU(Unknown Source:0)
at com.birjuvachhani.locus.Locus$$ExternalSyntheticLambda3.onSuccess(Unknown Source:4)
at com.google.android.gms.tasks.zzn.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

+1
Same issue here

This should fix it. Try this and let me know if it works fine. I'll release it.

implementation 'com.github.BirjuVachhani:locus-android:master-SNAPSHOT'

@larsmathuseck @Zee-BA Can you please confirm?

Thanks for the fast reply.
I have trouble building from the master snapshot

Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
Could not find com.github.BirjuVachhani:locus-android:master-SNAPSHOT.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/maven-metadata.xml
- https://dl.google.com/dl/android/maven2/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/locus-android-master-SNAPSHOT.pom
- https://repo.maven.apache.org/maven2/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/maven-metadata.xml
- https://repo.maven.apache.org/maven2/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/locus-android-master-SNAPSHOT.pom
- https://jitpack.io/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/maven-metadata.xml
- https://jitpack.io/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/locus-android-master-d9f8b59f14-1.pom
Required by:
project :app

https://jitpack.io/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/locus-android-master-d9f8b59f14-1.pom
gives Not found. Any idea what's wrong here?

Could it be that the snapshot build failed for some reason?? Let me check.

Found it. Its Java version issue. The project uses JAVA 1.8 but Gradle requires Java 11. I'll fix this.

when the library gets updated for this issue.

+1
😢
Your library is awesome thoug

I am having hard time fixing Jitpack with Java 11. I'll try to release a version this weekend. Thank you for bearing this with me...

Meanwhile can anyone try to run the sample project and see if the flags issue is gone or not? It is working for me so just wanted to confirm before releasing.

I just checked the demo-app. It still crashes for me on my Pixel 4 with Android 12.

Caused by: java.lang.IllegalArgumentException: com.birjuvachhani.locationextensionsample: 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:375)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at com.birjuvachhani.locus.LocationBroadcastReceiver$Companion.getPendingIntent(LocationBroadcastReceiver.kt:46)

I think the code should be

val flags =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
    else PendingIntent.FLAG_UPDATE_CURRENT

instead of

val flags =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
        PendingIntent.FLAG_UPDATE_CURRENT and PendingIntent.FLAG_MUTABLE
    else PendingIntent.FLAG_UPDATE_CURRENT

( or instead of and).

Also, are you sure the Intent must be mutable?
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.

@larsmathuseck Thanks for the super informative response. I am not 100% sure about mutability flag as this library was developed a long time ago. I'll do some research to remove it if possible.

For now, I have made suggested changes and have tested on Android 12 emulator (which I couldn't before). It is not crashing for me. Let me know if this works for you. I have pushed the fix to master.

Thanks for the fast fix.
The latest version seems to work on my device.

That's great. Now I need to fix Jitpack.

Fixed in v4.0.1

implementation 'com.github.BirjuVachhani:locus-android:4.0.1'

Sorry for not being able to test it, will update to the new version tomorrow, thanks for your effort!