Error XF001: Xamarin.Forms targets have been imported multiple times

If you’re in the process of updating a Xamarin.Forms app to a more modern style project set up (NET Standard 2.0, PackageReference, etc), you may get the following error.

 

Error XF001: Xamarin.Forms targets have been imported multiple times. Please check your project file and remove the duplicate import(s).

 

This can be because of the move to using PackageReference for your NuGet packages and a simple fix awaits you.

  1. Close Visual Studio and navigate to the Solution in File Explorer
  2. Delete the hidden .vs folder
  3. Go into each affected project sub folder and delete the following files; project_name.nuget.props and project_name.nuget.targets
  4. Open the solution in Visual Studio, do a Clean and Rebuild, 

 

You should no longer see the error and be able to deploy.

 

 

 

 

4 Replies to “Error XF001: Xamarin.Forms targets have been imported multiple times”

    1. The *.nuget.* generated files are when a project uses the old NuGet reference mechanisms (e.g. packages.config and packages.json). If a project is being changed from the old approach to the new PackageReference approach (i.e. the NuGet packages are now listed in the csproj), you will likely have left over auto-generated *.nuget.* files. If these are causing build errors, then clean up any leftovers as I show in the post.

      One example you will see of this happening is if you are working on a UWP project that was created with an older project template that used packages.json file to list the NuGet packages. When you switch the target SDK and modernize the project, it moves to a ProjectReference style. Visual Studio will automatically remove the packages.json file and place it into a backup folder, nut the autogenerated files might still be hanging around and cause this build error.

  1. None of my projects have project_name.nuget.props and project_name.nuget.targets files.

    What I see are lots of these files accross all projects in solution

    project_name.nuget.g.props
    project_name.nuget.g.targets
    project_name.nuget.cache
    project_name.nuget.stamp

    , so not sure how reliable is that article

    1. Those files are only needed when there’s Packages.config for that project. Once you switch that project to use ProjectReference (i.e. the NuGet packages are listed in the csproj), you no longer needed them.

      If your project is already using PAckageReference (which is the default for the last few Visual Studio releases), you will not see any *.nuget.* files generated.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.