How do I automatically version Xamarin.Android Library projects? RRS feed

  • Question

  • User47710 posted

    I'm trying to generate a NuGet package from a Android Library project. It seems NuGet doesn't respect/read the information in the AssemblyInfo.cs file for the Xamarin Android Library Project. The generated DLLs will contain the expected information (version number, author, assembly name, etc).

    Doing a nuget pack without specifying the version number will always create a package with version 1.0. This 1.0 must be coming from somewhere.

    In order to version my Android Library packages I need to do the following for NuGet.

    nuget pack -Version x.x.x.x

    Where x.x.x.x is the version numbering that I have to keep track of myself. I'd like to be using the automatic version numbering in AssemblyInfo.cs

    I've searched the forums and found references to version numbering in Xamarin.Android application projects, and using the app manifest file. There is no manifest file in the library project, or its generated files that I can find.

    I'd really like one central place to set the version number for my Android Library and have MSBuild, NuGet, and any other tools respect the version without manual intervention.

    Does anyone have experience with better way of doing this?

    Thursday, August 20, 2015 6:19 PM

All replies

  • User18049 posted

    On Windows you should be able to use nuget pack yourproject.csproj. With placeholders in the .nuspec file for various properties that will be taken from your project instead of from the .nuspec file. There is documentation on the NuGet site on user replacement tokens.

    If you are the Mac then you cannot use nuget pack with a project file since Mono does not fully support the MSBuild API which is used when this command is called.

    Thursday, August 20, 2015 8:01 PM
  • User47710 posted

    Here is my nuspec file from my Xamarin Android Library project. I've redacted company info and added 'Example' as a placeholder.

    <?xml version="1.0"?> <package > <metadata> <id>$id$</id> <version>$version$</version> <title>Example</title> <authors>Example</authors> <owners>Example</owners> <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl> <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl> <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Example</description> <releaseNotes>Summary of changes made in this release of the package.</releaseNotes> <copyright>Copyright 2015</copyright> <tags>Example</tags> </metadata> </package>

    The $author$, $description$, are usually between the (authors & owners) and description tag respectively. However, running nuget pack projname.csproj has errors. NuGet can't find information to fill in these tokens. This is only a problem for Xamarin Android Library projects.

    We work around the problem by manually adding information into our nuspec file.

    The $id$ token is filled with the expected value when we pack. However, the $version$ number is always 1.0 unless explicitly set using the -Version parameter.

    Thursday, August 20, 2015 10:20 PM
  • User47710 posted

    Additionally the AssemblyInfo.cs file has the version we want:

    [assembly: AssemblyVersion("1.1.1.*")]

    This autoincrements the * after each build.

    The dlls created from each build has the correct version number. Right Click the DLL -> Properties -> Details Tab. The version is The version is updated as we expect every time the project is rebuilt.

    Thursday, August 20, 2015 10:27 PM
  • User18049 posted

    OK now I see the problem. With a .nuspec file that has replacement tokens and an Android Library project when you run nuget pack YourAndroidLibrary.csproj it shows the warning:

    WARNING: Unable to extract metadata from 'AndroidLibraryTest.dll'.

    If you run nuget pack with the extra argument -verbosity detailed you can see that nuget is unable to extract the metadata because it is failing to find Mono.Android which is referenced by the Android Library assembly. So to fix that I just copied the Mono.Android.dll into the bin\debug folder from the following folder:

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v5.0

    The above assumes your project is targeting MonoAndroid version 5.0.

    Then running nuget pack YourAndroidLibrary.csproj works without any warning and the AssemblyVersion ("1.2.3") is used as the version of the NuGet package.

    To automate this you will probably want a script, maybe PowerShell, or a .bat file, or a post build task in your project, that copies the MonoAndroid.dll to the output directory of your project, runs nuget pack, then deletes the MonoAndroid.dll.

    Thursday, August 20, 2015 10:40 PM
  • User47710 posted

    THANK YOU!!!!!

    This fixed all my issues, everything is working as expected.

    Note: If anyone else makes the change like me and still get the error:

    System.InvalidOperationException: The replacement token 'description' has no value.

    Double check they are defined in your AssemblyInfo.cs

    [assembly: AssemblyDescription("")] // Causes error.


    [assembly: AssemblyDescription("Test description")] // Works

    Friday, August 21, 2015 2:47 AM
  • User56293 posted

    Thank you, this resolve the issue for me.

    Thursday, March 1, 2018 11:10 AM