Easier Code Sharing Across iOS, Android, and Windows
At the Build developer conference this year, Microsoft introduced “Universal Apps”, which are apps that target Windows Runtime and can run on all Windows devices. Since Universal apps run on the same Windows runtime, developers can now have a common way of building and architecting apps for phones, tablets, and PCs. Universal apps shouldn’t be confused as a single executable running on multiple platforms. From a developer standpoint, a universal app project template in Visual Studio will consist of platform specific projects (Windows Phone 8.1, Windows 8.1) along with a Shared Project. To get started with Universal Apps, refer to the MSDN article on Building Universal Windows apps for all devices.
Code Sharing Strategies
Xamarin developers have been taking advantage of code sharing techniques like Portable Class Libraries and File Linking to share their platform agnostic codes across Xamarin.iOS, Xamarin.Android and other .NET projects ever since. In fact, almost any .NET codebase, including Windows Forms, WPF, ASP.NET, and Silverlight, has sharable code that can be ported to Xamarin.iOS, Xamarin.Android, Windows Phone and Windows Store. In the past, we’ve looked at a step-by-step approach to port an existing .NET app to mobile using both file linking and PCL. In case you missed them earlier, here are the links:
- Porting existing .NET apps to iOS
- Porting existing .NET apps to Android
- Porting existing .NET apps to Four Mobile Platforms using PCL
In all the posts above, I used a DietCalculator Silverlight MVC example that uses a simple algorithm to calculate the daily calories required based on inputs provided by the user. Here’s how the ported Silverlight app looks on iOS, Android, and Windows Phone devices: In this blog post, I’ll quickly recap the steps involved to separate shareable code and then use Shared Projects to reuse them in Universal App Projects (Windows 8.1 and Windows Phone 8.1), Xamarin.iOS, and Xamarin.Android using Visual Studio.
Identifying the Sharable Code
We used Xamarin’s .NET Mobility Scanner to analyze how mobile our Silverlight code is and concluded that 78% of the app code can be reused on all platforms. As per the detailed report, all of the platform-specific method calls (i.e., code that we can’t re-use across platforms) are user-interface specific and hence the business logic code that consists of Models, Controllers, and Helpers from the Silverlight_MVC project can be reused on all of the platforms. This time we’ll move these reusable codes to Shared Project instead of PCL.
To get started with Shared Projects and Universal Windows Apps in Visual Studio, you need to install Visual Studio 2013 Update 2 RC. By default, Shared Projects are supported only in Universal Apps. By installing a VS plugin Shared Project Reference Manager you can use them with almost any C# project. That said, this plugin is mandatory to make them support in Xamarin.iOS and Xamarin.Android projects. Please note support for Shared Projects in Xamarin Studio is coming soon!
Now, in Visual Studio, create a new Blank Universal App and call it DietCalculator. Resulting solution will consist of three projects – Windows, WindowsPhone, and a Shared Project.
Move all the reusable code that we identified in our previous step (i.e. Models, Controllers and Helper files) into a Core folder inside Shared Project. Now add the existing Xamarin.iOS and Xamarin.Android projects to this solution and reference the Shared Project in them. To reference a shared project in Xamarin.Android and Xamarin.iOS, right click on the references -> Add Shared Project Reference
That’s it! You’ll now notice that all of this code is directly accessible inside your Xamarin.iOS, Xamarin.Android, Windows Phone 8.1, and Windows 8.1 projects.
One Last thing..
You may notice that both Windows Phone and Windows Projects share App.xaml and few other Common files that use Windows specific APIs in its Shared Project. Since iOS and Android don’t have these APIs, they will fail to compile. Just wrap this code using #if NETFX_CORE… #endif conditional compilation statements and they will not complain again.
This is just one example of how Shared Projects are extremely powerful. You can tap into the native functionality of Android, iOS, and Windows with a simple conditional #if statement, allowing you to easily share more of your source files across all projects.