SocialSharing-PhoneGap-Plugin: Error socialsharing.share(...) Android 12

In Android 12 after trying to use method socialsharing.share(…) the application closes immediately, in android studio shows this error in the log:

java.lang.IllegalArgumentException: com.app-xxx: 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 nl.xservices.plugins.SocialSharing$2.run(SocialSharing.java:274)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:920)

To solve this I had to go to android folder in the plugin, find SocialSharing.java, and replace this line:

final PendingIntent pendingIntent = PendingIntent.getBroadcast(cordova.getActivity().getApplicationContext(), 0, receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);

With this code int pendingIntentValueFLAG = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { pendingIntentValueFLAG = PendingIntent.FLAG_MUTABLE; } else { pendingIntentValueFLAG = PendingIntent.FLAG_UPDATE_CURRENT; } final PendingIntent pendingIntent = PendingIntent.getBroadcast(cordova.getActivity().getApplicationContext(), 0, receiverIntent, pendingIntentValueFLAG);

Cordova and plugin version: “cordova”: “11.0.0” “cordova-android”: “^10.1.2” “cordova-plugin-x-socialsharing”: “^6.0.3”

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 22
  • Comments: 16

Commits related to this issue

Most upvoted comments

To anyone who got here recently:

  • There are 2PRs to fix this: #1158 #1181
  • Discussion continues in PR #1158
  • bug known since Nov 10, 2021, read issue #1155 TLDR, is plugin dead? idk, if you are in rush try cordova-plugin-x-socialsharing-android12

@EddyVerbruggen can you please merge the pr for the solution for Android 12,13 ?

@jeanluck36 @sachithd ok, but the build with this plugin still fails receiving this error: android:exported needs to be explicitly specified for element <receiver#nl.xservices.plugins.ShareChooserPendingIntent>. Apps targeting Android 12 and higher are required to specify an explicit value for "android:exported" when the corresponding component has an intent filter defined. so you have to add android:exported="true" or better for this plugin false value, manually on AndroidManifest.xml after adding this plugin… to you the build works even without this change on android 12?

Thanks for the reply. but latest plugin install didn’t solve the issue for me in “cordova-android”: “^11.0.0” I got the solution by using “cordova-plugin-x-socialsharing-android12”

great stuff. This resolved the issue. Please merge this code on the next release. Thanks