A Note on Shared Projects in Visual Studio
Categories: Mobile Apps,
I came across an issue when working on the Android implementation of my Rise app using Visual Studio for Mac.
No, not Xamarin, Visual Studio! I made the switch to Visual Studio for Mac when it came out of preview mode a few days back. The apps are nearly identical, which is no coincidence. Microsoft bought Xamarin last year and built Xamarin Studio as a near clone of Visual Studio Community. I imagine now that Visual Studio for Mac is no longer in preview mode, Xamarin Studio will be phased out in favor of cross-mobile development under the company’s flagship IDE. Smart move, Microsoft!
Anyway, I had been using a library called Json.NET made by Newtonsoft for converting JSON files to C# objects (deserializing) and writing objects to JSON files (serializing). If you don’t know what JSON is, it stands for JavaScript Object Notation, and it’s a convenient way of representing data with key-value pairs. And Json.NET is a pretty neat tool for reading and writing files stored in this format. But I’ve encountered this issue where some days, the library is recognized by Xamarin/Visual Studio, and other days I’m unable to use it. I’ve attached a screenshot below from someone who had the same problem. It’s from a stackoverflow discussion I regrettably can no longer find.
I tried uninstalling and re-installing the package multiple times, and downgrading to a previous version. None of these worked. However, I finally figured out where I went wrong:
Packages on which shared classes depend must be imported into all platform-specific projects.
I had one project called TheRiseAndroid
and another called TheRiseiOS
, but I was only working on the Android version of the app. The iOS version was completely empty because I hadn’t started it yet. Between these projects is a Shared
folder that contains classes to be used on both platforms. These include classes that would be used to deserialize and serialize JSON files, so these classes used the Newtonsoft.Json
namespace. But because it wasn’t included in the iOS project, Visual Studio complained that I didn’t have a reference to the library. Except I did have a reference to the library. I just needed another reference within the empty, almost-forgotten iOS project. So yeah…I was stuck on this for a while.
Once I loaded the package into TheRiseiOS
, the errors disappeared! It seems like this would be a common problem considering how many shared Android and iOS projects there are in the Xamarin/Visual Studio universe, but I haven’t found any question-answer threads along these lines. Maybe this is totally obvious and not worth its own blog post. But feel free to leave a comment if this was useful to you!