How do I resolve a PathTooLongException error?

Cause

Generated path names in a Xamarin.Android project can be quite long. For example, a path like the following could be generated during a build:

C:\Some\Directory\Solution\Project\obj\Debug\library_projects\Xamarin.Forms.Platform.Android\library_project_imports\assets

On Windows (where the maximum length for a path is 260 characters), a PathTooLongException could be produced while building the project if a generated path exceeds the maximum length.

Fix

The UseShortFileNames MSBuild property is set to True to circumvent this error by default. When this property is set to True, the build process uses shorter path names to reduce the likelihood of producing a PathTooLongException. For example, when UseShortFileNames is set to True, the above path is shortened to path that is similar to the following:

C:\Some\Directory\Solution\Project\obj\Debug\lp\1\jl\assets

To set this property manually, add the following MSBuild property to the project .csproj file:

<PropertyGroup>
    <UseShortFileNames>True</UseShortFileNames>
</PropertyGroup>

If setting this flag does not fix the PathTooLongException error, another approach is to specify a common intermediate output root for projects in your solution by setting IntermediateOutputPath in the project .csproj file. Try to use a relatively short path. For example:

<PropertyGroup>
    <IntermediateOutputPath>C:\Projects\MyApp</IntermediateOutputPath>
</PropertyGroup>

For more information about setting build properties, see Build Process.