3
votes

I have inherited a Xamarin project (not Xamarin.Forms) and right now it has three major components.

An iOS project, an Android project and finally a Shared Project (not a PCL and not a .Net Standard project).

Despite the Shared Project, the application reads a json file included within the assets folders of each project. The file is a duplicate. I feel like this file should be in the Shared Project

To clarify, this is the structure:

Shared Project

No Assets

iOS

Resources/test.json

Android

Assets/test.json

I believe the test.json file should exist as one file in the Shared Project. My first step was to make this move and I changed the file to Build Action=Content which worked for the iOS project. It seems however the Android project does not get this file. I cannot find it anywhere in the compiled and archived .apk file.

Is there anything I can do about this, or do I need to accept this design flaw and build a .Net Standard library or PCL? If so, how exactly do you make a .Net Standard or PCL for a non-Xamarin.Forms project.

1
Not a design flaw, just the way each platform work with "content". A way to share a "file" without including it each platform-specific application (or library) is to embed it as a .Net assembly resource in a NetStd/PCL library and include that library in each app. Personally I highly against permanently consuming memory with embedded resources in assemblies and create symlinks within each application project to a central location in the solution and make use the platform specific bundle options... - SushiHangover
No no no, I don't mean design flaw like that! I meant in the way the app I inherited was developed. - Jeff
hahahahah, ok.. ;-) .... Shared Projects do have their benefits, including one less assembly to slow the app startup time ;-) - SushiHangover

1 Answers

5
votes

What is your use case? If you just need to read the test.json at runtime and parse it using .NET code, you can use embedded resources, details here: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/files/

If you need the file to go into iOS reosurces / Android assets folder, you can keep the test.json file only once in the solution (either in shared project or as a solution item) and add it as link to both iOS and Android project - here is screen shot how to do it in Visual Studio.

Add as link

You can do it also in Visual Studio for Mac - right click on the containing folder in solution explorer, choose Add Files and you will get the following prompt - choose Add a link to the file

VS For mac