Not sure if people notice, but if you create a Xamarin.Forms project, just the simple helloworld created by VisualStudio. It will have a fileprovider added to it when you build. You'll have to go look in the compiled manifest to see it, but it's there. Why is this there?
-
For clarity, please add to question, an example of what is added. "Compiled manifest" - what is path to it? is that a readable text file? If so, copy/paste the relevant text lines into the question. If not, then how examine it? – ToolmakerSteve Dec 08 '22 at 02:55
1 Answers
According to FileProvider doc, reason is "To support URIs", as per Android spec.
Android FileProvider implementation for creating Content URIs to share files with other applications.
This is part of Xamarin's library implementation, that makes it convenient for an app coder to use URIs.
Why is it there?
tl;dr This was the easiest way to make URI communication convenient for all app coders.
For that library code to work, there must be a FileProvider to call.
[SPECULATION FOLLOWS]
Could they have written it in a way that required app coder to:
- Add their own FileProvider declaration to AppManifest.
- Construct an instance of a FileProvider.
- Pass that instance in to the Xamarin URI calls.
Sure. But that would be more work for any Xamarin app coder that uses URIs. So they chose to do it once, for everyone.
Could they have written logic that only added that XML to AppManifest, if app, anywhere in it, made URI calls?
Maybe, but that would take more development time, and be easier to get wrong, because it requires knowledge about whether your app code uses a feature, or doesn't use it.
BACKGROUND INFO
For more info, see official Android doc Sharing files
This is part of newer Androids attempt to maintain user privacy and app security, yet provide mechanisms for apps to share content (without resorting to saving files where ANY app can access them).
This also makes it easier for Android OS to "clean up" when an app is deleted. The old approach of saving shared files in a public place meant that those files did not get deleted when an app was deleted. Sometimes that isn't what you want, so this is a "cleaner" approach to sharing data between apps.

- 18,547
- 14
- 94
- 196
-
I'm sorry, but your answer doesn't make much sense to me. Your links are to android documentation about how to create a FileProvider and what it is. I know those answers. I am asking why every Xamarin forms app has a FileProvider added to it. – Colin Dec 08 '22 at 14:32
-
To me, it appears that Xamarin.Forms requires a fileProvider for some reason, implying that Xamarin.Forms is exchanging files with something else... What else? I would argue that this is a security risk. But mostly, I can't find any documentation that explains why this is so. – Colin Dec 08 '22 at 14:32
-
Android specifies that apps be able to communicate via URIs. So that a Xamarin app coder **can** do so, Xamarin defines a default FileProvider. Its as simple as that. They were writing a **library that supports URI communication**, and put in everything necessary. This is something necessary. Could Xamarin devs have "optimized" Xamarin.Essentials code, to leave out this FileProvider, if app coder never requests URI communication? Probably. But that would be an extra bit of work. It all comes down to priorities. You can always file an issue about this. – ToolmakerSteve Dec 08 '22 at 21:31