19

By default, Xamarin.iOS and Xamarin.Android projects use a package.config file to manage Nuget dependencies. The Microsoft page about Package references (PackageReference) in project files says:

By default, PackageReference is used for .NET Core projects, .NET Standard projects, and UWP projects targeting Windows 10 Build 15063 (Creators Update) and later. .NET full framework projects support PackageReference, but currently default to packages.config. To use PackageReference, migrate the dependencies from packages.config into your project file, then remove packages.config.

I think this is not clear about the use of PackageReference style in Xamarin.Android or Xamarin.iOS projects.

My question: is it possible to use PackageReference in Xamarin.iOS and Xamarin.Android projects?

Elte Hupkes
  • 2,773
  • 2
  • 23
  • 18
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89

1 Answers1

40

Yes, PackageReference works for Xamarin.iOS|Android projects and in Visual Studio for Windows or Mac or via the cmd-line (nuget restore or via msbuild).

Add the following to the top of your Xamarin.iOS and/or Xamarin.Android .csproj file:

  <PropertyGroup>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  </PropertyGroup>

My current steps are:

  • Copy your packages.config as a backup
    • Use it as a reference when you re-add the packages on the last step
  • Manually remove all packages from the project (Xamarin.iOS or Xamarin.Android)
  • Edit the csproj and add the PackageReference
  • Re-add just the top-level packages

Note: I do the manual package remove one-by-one via the IDE in order to properly update the project references. Also, when re-adding them after the PackageReference additional, the csproj is updated correctly (you can do this manually in the csproj text but it is a pain and easy to screwup).

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Hello @SushiHangover. Thank you for your answer. Is this approach compatible with Visual Studio for Mac? Sorry, I'm a newbie in the .NET world. – Bruno Peres Mar 13 '18 at 22:32
  • @BrunoPeres Yes, just updated answer, I do this mainly in VS4M as that is my prime development IDE (and of course if works in VSfW also). – SushiHangover Mar 13 '18 at 22:33
  • thank you again! I upvoted your answer and now I will test your approach here. I will accept your answer as the right as soon as possible. :) – Bruno Peres Mar 13 '18 at 22:37
  • Thanks so much. This works in a xamarin.mac project also. – Pellet Nov 17 '18 at 07:17
  • 4
    Looks like we now have a feature `Migrate packages.config to PackageReference` if you right click on `References` in your project. Worked for me in `VSfW2017 15.9.4 Preview 1.0` with a little hand cleanup – Malachi Jan 04 '19 at 01:05
  • 3
    Migrate packages still not available for VS for Mac in late 2021 :-( – Jamie M. Dec 16 '21 at 20:36