yasirkula / UnityNativeShare

A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS
MIT License
890 stars 131 forks source link

Android 12/S+ Native Share requires FLAG_IMMUTABLE Flag #130

Closed lamington-david closed 2 years ago

lamington-david commented 2 years ago

Description of the bug

When calling NativeShare.Share() on my Pixel 3 on Android 12, my app crashes with the error:

_Error AndroidRuntime java.lang.IllegalArgumentException: com.package.packagename: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAGMUTABLE be specified when creating a PendingIntent.

Reproduction steps

On a phone running Android 12, initialize a NativeShare object, and then call the NativeShare.Share function. The app should crash with the given error.

Platform specs

Please provide the following info if this is a Unity 3D repository.

Additional info

Full Logcat: 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime FATAL EXCEPTION: main 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime Process: com.package.packagename, PID: 10401 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime java.lang.IllegalArgumentException: com.package.packagename: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime 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. 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.PendingIntent.checkFlags(PendingIntent.java:375) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.PendingIntent.getBroadcast(PendingIntent.java:632) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at com.yasirkula.unity.NativeShareBroadcastListener.Initialize(NativeShareBroadcastListener.java:31) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at com.yasirkula.unity.NativeShareFragment.onCreate(NativeShareFragment.java:46) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.Fragment.performCreate(Fragment.java:2486) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1261) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2431) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2210) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2166) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2067) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.FragmentManagerImpl$1.run(FragmentManager.java:742) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.os.Handler.handleCallback(Handler.java:938) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.os.Looper.loopOnce(Looper.java:201) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.os.Looper.loop(Looper.java:288) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:7842) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at java.lang.reflect.Method.invoke(Native Method) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 2021/11/09 11:09:09.188 10401 10401 Error AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

yasirkula commented 2 years ago

This line must be uncommented and the library must be rebuilt with Android 12 SDK: https://github.com/yasirkula/UnityNativeShare/blob/de749d040538ca26d774db9a0a90e35ac91bcca5/.github/AAR%20Source%20(Android)/java/com/yasirkula/unity/NativeShareBroadcastListener.java#L25-L28

I'm waiting for a stable version of Android 12 SDK to be released because I don't want to compile the library with a preview-SDK, it might introduce new bugs. Perhaps I should temporarily add FLAG_IMMUTABLE flag to get rid of this issue until then.

yasirkula commented 2 years ago

Can you temporarily test the following unitypackage (you need to unzip the zip archive first) to see if it resolves the crash: NativeShare.zip

lamington-david commented 2 years ago

I just tested it on my Pixel 3 running the latest official firmware (Android 12) and it worked perfectly!

lamington-david commented 2 years ago

Edit Unity Package Manager wasn't picking up that version 1.4.4 was available, I removed and added the package again and was able to update. I'll see if this fixes the issue.

Version 1.4.3 Problem Just an update on this issue, after a few weeks I've gotten more crashes reported on the Google Play storefront using the updated plugin with the following error:

java.lang.Error: FATAL EXCEPTION [main] Unity version : 2020.3.15f2 Device model : Google Pixel 5 Build Type : Release Scripting Backend : IL2CPP ABI : arm64-v8a Strip Engine Code : true 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.yasirkula.unity.NativeShareBroadcastListener.Initialize (NativeShareBroadcastListener.java:31) at com.yasirkula.unity.NativeShareFragment.onCreate (NativeShareFragment.java:46) at android.app.Fragment.performCreate (Fragment.java:2486) at android.app.FragmentManagerImpl.moveToState (FragmentManager.java:1261) at android.app.FragmentManagerImpl.addAddedFragments (FragmentManager.java:2431) at android.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2210) at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2166) at android.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2067) at android.app.FragmentManagerImpl$1.run (FragmentManager.java:742) 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:7838) 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)

I haven't been able to reproduce the issue myself, but it's happening on other phones running android 12 including:

yasirkula commented 2 years ago

FLAG_MUTABLE is commented out until Android 12 SDK is released. Currently FLAG_IMMUTABLE flag is set: https://github.com/yasirkula/UnityNativeShare/blob/6df63fe95e20bd5b560331d23c64aaae6e10b089/.github/AAR%20Source%20(Android)/java/com/yasirkula/unity/NativeShareBroadcastListener.java#L28-L32

Those users must be running the older version of the app.

hippogamesunity commented 1 year ago

Thanks for fixing this, I also experienced to this issue)