BirjuVachhani / locus-android

An Awesome Kotlin Location library to retrieve location merely in 3 lines of code
https://birju.dev/posts/retrieve-location-in-just-3-lines-android/
Apache License 2.0
358 stars 40 forks source link

Pending intents need to have either FLAG_IMMUTABLE or FLAG_MUTABLE #56

Closed Zee-BA closed 2 years ago

Zee-BA commented 2 years ago

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)

larsmathuseck commented 2 years ago

+1 Same issue here

BirjuVachhani commented 2 years ago

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

@larsmathuseck @Zee-BA Can you please confirm?

larsmathuseck commented 2 years ago

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://jitpack.io/com/github/BirjuVachhani/locus-android/master-SNAPSHOT/locus-android-master-d9f8b59f14-1.pom gives Not found. Any idea what's wrong here?

BirjuVachhani commented 2 years ago

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

BirjuVachhani commented 2 years ago

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

johnkevin123898 commented 2 years ago

when the library gets updated for this issue.

AlvaroSanzRodrigo commented 2 years ago

+1 😢 Your library is awesome thoug

BirjuVachhani commented 2 years ago

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

BirjuVachhani commented 2 years ago

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.

larsmathuseck commented 2 years ago

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.

BirjuVachhani commented 2 years ago

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

larsmathuseck commented 2 years ago

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

BirjuVachhani commented 2 years ago

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

BirjuVachhani commented 2 years ago

Fixed in v4.0.1

implementation 'com.github.BirjuVachhani:locus-android:4.0.1'
AlvaroSanzRodrigo commented 2 years ago

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