EddyVerbruggen / SocialSharing-PhoneGap-Plugin

👨‍❤️‍💋‍👨 Cordova plugin to share text, a file (image/PDF/..), or a URL (or all three) via the native sharing widget
MIT License
1.78k stars 905 forks source link

Error socialsharing.share(...) Android 12 #1176

Open jeanluck36 opened 1 year ago

jeanluck36 commented 1 year ago

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"

sachithd commented 1 year ago

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

dilo99 commented 1 year ago

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

synduit-jenkins commented 1 year ago

@jeanluck36 @sachithd I am facing the same issue https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin/issues/1176#issuecomment-1191642022d @dilo99 did you resolve this?

sachithd commented 1 year ago

In the plugin.xml file manually added android:exported="true" to receiver element

`

</config-file>`
Syahrul commented 1 year ago

If you're upgrading to Capacitor 4, you may encounter this issue as I did. As @sachithd guide above, to save people some time, the file is in node_modules/cordova-plugin-x-socialsharing/plugin.xml

vchabal commented 1 year ago

To anyone who got here recently:

bonjourjoel commented 1 year ago

I have the same error, and also the error about android:export=true.

The plugin seems dead. Is there any alternative with updates for the latest android?

pedropamn commented 1 year ago

In the plugin.xml file manually added android:exported="true" to receiver element

<config-file target="AndroidManifest.xml" parent="/*/application"> <receiver android:name="nl.xservices.plugins.ShareChooserPendingIntent" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.SEND"/> </intent-filter> </receiver> </config-file>

It fix the issue for you? For me, I can't even build. I'm still seeing the message:

Apps targeting Android 12 and higher are required to specify an explicit value forandroid:exportedwhen the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details

EYALIN commented 1 year ago

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

iamromec commented 1 year ago

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

Topiya commented 1 year ago

I am also facing the same issue. Is there any solution for the same? Please let me know if it is available.

EYALIN commented 1 year ago

@Topiya the fix was merged. please remove the plugin and add it again.

Topiya commented 1 year ago

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"

timonjagi commented 1 year ago

Same case as @Topiya, seems like the latest plugin is still not adding android:exported to the manifest. I solved it by using the forked plugin cordova-plugin-x-socialsharing-android12

EYALIN commented 1 year ago

@sachithd @Syahrul @timonjagi @vchabal @Topiya @pedropamn @bonjourjoel @jeanluck36 it has been fixed in my forked repo: https://github.com/EYALIN/community-cordova-plugin-social-sharing

you can install it as an npm package: cordova plugin add community-cordova-plugin-social-sharing --save

mschoettle commented 5 months ago

Does nl.xservices.plugins.ShareChooserPendingIntent actually have to be exported or could it be set to false?

I am getting the following for our app by the MobSF static analyzer:

Broadcast Receiver (nl.xservices.plugins.ShareChooserPendingIntent) is not Protected. [android:exported=true]

A Broadcast Receiver is found to be shared with other apps on the device therefore leaving it accessible to any other application on the device.