Cross Platform
Android
iOS
Mac
Test Cloud

Sharing Code Options

This document compares the two methods of sharing code between cross-platform projects: Shared Projects and Portable Class Libraries, including the benefits and disadvantages of each.

Overview

There are two alternative methods for sharing code between cross-platform applications:

  • Shared Projects – Use the Shared Asset Project type to organize your source code, and use #if compiler directives as required to manage platform-specific requirements.
  • Portable Class Libraries – Create a Portable Class Library (PCL) targetting the platforms you wish to support, and use Interfaces to provide platform-specific functionality.

The goal of a code-sharing strategy is to support the architecture shown in this diagram, where a single codebase can be utilized by multiple platforms.

This article compares the two methods to help you choose the right project type for your applications.

Shared Projects

The simplest approach to sharing code files is to use a Shared Project (introduced in Xamarin Studio 5 and Visual Studio 2013 Update 2).

This screenshot shows a solution file containing three application projects (for Android, iOS and Windows Phone), with a Shared project that contains common C# source code files:

The conceptual architecture is shown in the following diagram, where each project includes all the shared source files:

Example

A cross platform application that supports iOS, Android and Windows Phone would require an application project for each platform. The common code lives in the Shared Project.

An example solution would contain the following folders and projects (project names have been chosen for expressiveness, your projects do not have to follow these naming guidelines):

  • Shared – Shared Project containing the code common to all projects.
  • AppAndroid – Xamarin.Android application project.
  • AppiOS – Xamarin.iOS application project.
  • AppWinPhone – Windows Phone application project.

In this way the three application projects are sharing the same source code (the C# files in Shared). Any edits to the shared code will be shared across all three projects.

Benefits

  • Allows you to share code across multiple projects.
  • Shared code can be branched based on the platform using compiler directives (eg. using #if __ANDROID__, as discussed in the Building Cross Platform Applications document).
  • Application projects can include platform-specific references that the shared code can utilize (such as using Community.CsharpSqlite.WP7 in the Tasky sample for Windows Phone).

Disadvantages

  • Unlike most other project types, a Shared Project has no 'output' assembly. During compilation, the files are treated as part of the referencing project and compiled into that DLL. If you wish to share your code as a DLL then Portable Class Libraries are a better solution.
  • Refactorings that affect code inside 'inactive' compiler directives will not update the code.

Remarks

A good solution for application developers writing code that is only intended for sharing in their app (and not distributing to other developers).

Portable Class Libraries

Portable Class Libraries are discussed in detail here.

Benefits

  • Allows you to share code across multiple projects.
  • Refactoring operations always update all affected references.

Disadvantages

  • Cannot use compiler directives.
  • Only a subset of the .NET framework is available to use, determined by the profile selected (see the Introduction to PCL for more info).

Remarks

A good solution if you plan to share the resulting assembly with other developers.

Summary

Portable Class Libraries are the simplest way to share code across multiple platforms, however they have some disadvantages including the custom configuration currently required to install PCL support.

The code sharing strategy you choose will be driven by the platforms you are targeting. Choose a method that works best for your project.