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

Crash on Android 12 and API 31 or more #146

Closed egorovsa closed 1 year ago

egorovsa commented 1 year ago

Description of the bug

Version 1.4.7 - May 02, 2022

Hi. It crashes on Samsung devices usualy.

So The crash description is

Fatal Exception: java.lang.Error FATAL EXCEPTION [main] Unity version : 2021.2.14f1 Device model : samsung SM-A515F Device fingerprint: samsung/a51nsser/a51:12/SP1A.210812.016/A515FXXU5FVE2:user/release-keys Build Type : Release Scripting Backend : IL2CPP ABI : arm64-v8a Strip Engine Code : true

AND

Caused by java.lang.IllegalArgumentException com.success.motivation.affirmations: 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.

I've faced the same issue in my own plugin when I wanted to get and activity like this

PendingIntent.getActivity(context, 0, openAppIntent, PendingIntent.FLAG_UPDATE_CURRENT)

so I just added an additional condition to check the API vestion and fixed that like this

 return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                PendingIntent.getActivity(context, 0, openAppIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
            }else{
                PendingIntent.getActivity(context, 0, openAppIntent, PendingIntent.FLAG_UPDATE_CURRENT)
            }

Reproduction steps

Just try to share something in Samsung device with Android 12 Mine was Galaxy A51

The whole stack trace:

Caused by java.lang.IllegalArgumentException: com.success.motivation.affirmations: 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:382) at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673) at android.app.PendingIntent.getBroadcast(PendingIntent.java:660) at com.yasirkula.unity.NativeShareBroadcastListener.Initialize(NativeShareBroadcastListener.java:32) at com.yasirkula.unity.NativeShareFragment.onCreate(NativeShareFragment.java:42) 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:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Fatal Exception: java.lang.Error: FATAL EXCEPTION [main] Unity version : 2021.2.14f1 Device model : samsung SM-A515F Device fingerprint: samsung/a51nsser/a51:12/SP1A.210812.016/A515FXXU5FVE2:user/release-keys Build Type : Release Scripting Backend : IL2CPP ABI : arm64-v8a Strip Engine Code : true

egorovsa commented 1 year ago

I found in your codebase here NativeShareBroadcastListener.java you handle it however it's commended so it's time to use

// We must mark PendingIntent as either mutable or immutable on Android 12+
// Maybe FLAG_IMMUTABLE is sufficient but the pre-31 default value was implicitly mutable and I don't trust
// all social apps to work correctly on Android 12+ (API 31+) if I set it to FLAG_IMMUTABLE
//pendingIntentFlags |= PendingIntent.FLAG_MUTABL

UPD.. Ah no// I was in the somebody's fork. you current version is handled this.

yasirkula commented 1 year ago

If you've downloaded the plugin from Asset Store, you might've fetched an older version because Asset Store is buggy on some Unity versions. Glad to hear that the issue is resolved :D

egorovsa commented 1 year ago

Ok! Got it ! I will check and let you know. I've added this through assets manager and it says that the version is the very last one ) https://prnt.sc/2mPP_w4EoLdd check this out )

egorovsa commented 1 year ago

Glad to hear that the issue is resolved :D

Yes! All's cool now! I would like to thank you for the very impressive package. You are great!

yasirkula commented 1 year ago

Ok! Got it ! I will check and let you know. I've added this through assets manager and it says that the version is the very last one ) https://prnt.sc/2mPP_w4EoLdd check this out )

Yeah that's how that bug works, unfortunately. Asset Store downloads an older version of the plugin and says that it's up-to-date which is so annoying :-) And Unity team doesn't even mention this issue on any of their blog posts or a sticky forum thread which is insult to injury.