Search code examples
androidvisual-studiomsbuildtransformationxdt

Config Transformations From Visual Studio (Xamarin)


I'm trying to implement a pre-build transformation to my Android.Manifest file within a Xamarin.Android project within Visual Studio - The purpose is simply to change the bundleID of my app depending on whether I build a debug or release version.

I've added a new .netFramework project to my solution, referenced Microsoft.Build and then added a new class named BuildTask and referenced the DLL in my Xamarin.Android project.

I then intend to add a UsingTask to my Xamarin.Android project's .csproj file at the very bottom, just above the closing tag - I presume this is correct.

Before I add the UsingTask I wanted to make sure the Xamarin.Android project builds, but unfortunately I'm getting errors about a missing reference in my Xamarin.Android project saying I'm missing a reference to System.Collections, but the error disappears the second I remove the reference to my new BuildTask.DLL

I've never done this before so I may be heading down the rabbit hole... If anyone's got any advise, it'd be greatly appreciated.

Here's my BuildTask class for reference:

using System;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Xml;

public class BuildTask : Task
{
    private const string AndroidNamespace = "http://schemas.android.com/apk/res/android";

    [Required]
    public string PackageName { get; set; }
    [Required]
    public string ApplicationLabel { get; set; }
    [Required]
    public string ManifestFilename { get; set; }

    public string Debuggable { get; set; }

    public override bool Execute()
    {

        var xml = new XmlDocument();

        xml.Load(ManifestFilename);

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
        nsmgr.AddNamespace("android", AndroidNamespace);

        if (xml.DocumentElement != null)
        {
            xml.DocumentElement.SetAttribute("package", PackageName);


            var appNode = xml.DocumentElement.SelectSingleNode("/manifest/application", nsmgr);
            if (appNode != null && appNode.Attributes != null)
            {

                var labelAttribute = appNode.Attributes["label", AndroidNamespace];
                if (labelAttribute != null)
                {
                    labelAttribute.Value = ApplicationLabel;
                }

                var debuggableAttribute = appNode.Attributes["debuggable", AndroidNamespace];
                if (debuggableAttribute != null)
                {
                    debuggableAttribute.Value = Debuggable;
                }

                xml.Save(ManifestFilename);
                return true;
            }
        }
        return false;
    }
}

Thanks.


Solution

  • That because you are referencing full .NET Framework to Xamarin.Android project. This full .NET framework is incompatible with the Xamarin.Android. You should use one of the following:

    1. Create an Android Library Project.

    2. Create a Portable Class Library Project.

    3. Create a Shared Project.