Blog

Automating Xamarin Builds with FAKE – Part 2: Building PCL, iOS, and Android Projects

Continuing from part 1 of this series we now have FAKE setup and ready for us to use our automated building needs. Now let’s build our Xamarin Forms application. In here we’ll go through how much FAKE helps us out by including built-in APIs that allow us to easily create our build targets. If you’ve never seen a FAKE build script before, you may be surprised by how little we need to do, since FAKE has all sorts of APIs that can be leveraged.

Of course, before we can really do anything else with our application we need to make sure everything builds so that will be the focus of this post.

Updated Build Script

In here we’re going to utilize FAKE’s Xamarin Helper library to help us build our iOS application. For our PCL and Android applications, however, we will just use FAKE’s MSBuild helper method as it doesn’t need anything special to build like iOS applications do.

The full script is at the bottom of this post and can be also found on GitHub, but let’s first break it up and talk a bit about each build target.

PCL

In most Xamarin applications, your PCL will house all of your core logic. If you’re doing Xamarin Forms, it will also house most of your UI code as well which the iOS and Android projects will be dependent on. With this in mind, we’re going to use this opportunity to restore all of our packages in the whole solution. FAKE, as you’ll come to find out throughout this series, helps out us a ton and one of those ways is by providing us with a RestorePackages() method. It just scans through all the package.config files and runs NuGet restore on it.

Note that, if your solution is in a path with spaces, the restore may result in an error.

Now that that we have our packages restored we can continue the build process. The !! is one of a few custom operators that FAKE has. This takes in a string pattern, in our case the .csproj file name for our PCL project, and creates a FileInfo type of what it finds. This result gets forward piped into the the MSBuildfunction. In this function call we give it some default parameters and this is where the actual building process happens. We then forward pipe those results into a Log function that will be displayed in our command line window.

Target "Build-Pcl" (fun _ ->
    RestorePackages()

    !! "FakeDemo.csproj"
        |> MSBuild "FakeDemo/bin/Debug" "Build"  [ ("Configuration", "Debug"); ("Platform", "Any CPU") ] 
        |> Log "---PCL build output---"
)

iOS

Here is another area where FAKE helps us out quite a bit. I’m sure most of you know that, in order to build iOS or Mac applications, you need have it built a Mac with XCode. Let’s take a quick look at what this target looks like.

Target "Build-iOS" (fun _ ->
    iOSBuild (fun defaults -> 
        { 
            defaults with ProjectPath = "iOS/FakeDemo.iOS.csproj"
                          OutputPath = "iOS/iPhoneSimulator/Debug"
                          Configuration = "Debug|iPhoneSimulator"
                          Target = "Build"
        })
)

That’s really it. Pretty simple, right?

Android

Unlike iOS, building an Android project doesn’t need anything special other than having the Android SDKs being installed on your system. If you’re already working with Xamarin.Android then there’s a very good chance you already have those installed. Since it just utilizes those SDKs in a build, all we need to do to build it here is to call the MSBuild function again after getting the FileInfo type with the pattern of our Android project.

Target "Build-Droid" (fun _ ->
    !! "Droid/FakeDemo.Droid.csproj"
        |> MSBuild "Droid/bin/Debug" "Build" [ ("Configuration", "Debug"); ("Platform", "Any CPU") ]
        |> Log "----Android build output----"
)

Final Build Script

Below is the full build script. Hopefully, you got a better idea of how using FAKE can help you with building your Xamarin applications. In the next post we’ll take a quick look at how to run unit tests from FAKE.

#r "packages/FAKE/tools/FakeLib.dll"
open Fake
open Fake.XamarinHelper

let buildDir = "FakeDemo/bin/Debug"

Target "Clean" (fun _ ->
    CleanDir buildDir
)

Target "Test" (fun _ ->
    trace "Testing stuff..."
)

Target "Build-Pcl" (fun _ ->
    RestorePackages()

    !! "FakeDemo.csproj"
        |> MSBuild "FakeDemo/bin/Debug" "Build"  [ ("Configuration", "Debug"); ("Platform", "Any CPU") ] 
        |> Log "---PCL build output---"
)

Target "Build-iOS" (fun _ ->
    iOSBuild (fun defaults -> 
        { 
            defaults with ProjectPath = "iOS/FakeDemo.iOS.csproj"
                          OutputPath = "iOS/iPhoneSimulator/Debug"
                          Configuration = "Debug|iPhoneSimulator"
                          Target = "Build"
        })
)

Target "Build-Droid" (fun _ ->
    !! "Droid/FakeDemo.Droid.csproj"
        |> MSBuild "Droid/bin/Debug" "Build" [ ("Configuration", "Debug"); ("Platform", "Any CPU") ]
        |> Log "----Android build output----"
)

"Clean"
  ==> "Build-Pcl"
  ==> "Test"

"Clean"
  ==> "Build-Pcl"
  ==> "Build-iOS"
  ==> "Build-Droid"

RunTargetOrDefault "Test"

Need Xamarin Help?

Xamarin Consulting  Xamarin Training

Jonathan Wood

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

3 days ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

1 week ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

3 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago