android: [Bug] System.IO.IOException: The process cannot access the file '......android\assets\xxxx.dll' because it is being used by another process. - Visual Studio

Description

Android project in Xamarin forms does not build Gives the error System.IO.IOException: The process cannot access the file ‘C:\Project\100\android\assets\xxxx.dll’ because it is being used by another process. where xxxx is different every different try of build

Severity	Code	Description	Project	File	Line	Suppression State
Error		System.IO.IOException: The process cannot access the file '

C:\Source\DevPrateek\src\ProjectName.Mobile.Droid\obj\Debug\100\android\assets\Plugin.Media.dll
' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.IO.File.OpenFile(String path, FileAccess access, SafeFileHandle& handle)
   at System.IO.File.SetLastWriteTimeUtc(String path, DateTime lastWriteTimeUtc)
   at Xamarin.Android.Tools.Files.CopyIfChanged(String source, String destination)
   at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination)
   at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask()
   at Xamarin.Android.Tasks.AndroidTask.Execute()	ProjectName.Mobile.Droid			

I thought this is due to Windows max path issue so I even tried setting Enable Win32 long paths = Enabled in Local Policy Editor and added <UseShortFileNames>True</UseShortFileNames> and <IntermediateOutputPath>C:\Project</IntermediateOutputPath>

Every time it shows error message with different dll. ‘C:\ProjectNameTemp\100\android\assets\NUglify.dll’

‘C:\ProjectNameTemp\100\android\assets\AndUHD.dll’ and so on…

Also ran Get File Locking Process but found nothing.

PS C:\Project> Get-LockingProcess .\100\android\
WARNING: No matching handles found

Wasted entire day on this with no luck. Please note the solution gets build fine and runs on Visual Studio for Mac and also if I unload the Android project.

Also tried to change android build properties like dex compiler shared runtime etc but its the same. This is frustrating that IDE is not available. Cant use Mac all the time

Steps to Reproduce

  1. Create new Xamarin forms project on Visual Studio Windows
  2. Build

Expected Behavior

All projects should build

Actual Behavior

Android project does not build only on VIsual Studio windows , but builds on Visual Studio Mac

Basic Information

  • Version with issue: NA
  • Last known good version: NA
  • IDE:only Visual Studio Windows
  • Platform Target Frameworks:
    • iOS: NA
    • Android: 10 SDK 29
    • UWP:
  • Android Support Library Version:
  • Nuget Packages: Xamarin forms Xamarin essentials latest
  • Affected Devices: Android

Screenshots

Reproduction Link

NA

Workaround

No work around at this time, trying to build project varius changes and seeing if anything works.

This has been also reported at Visual Studio Developer Community

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (5 by maintainers)

Most upvoted comments

Notes for the Xamarin team members:

I took a first look at the ETL file, and it appears there might be some virus detection software that starts accessing the file between the File.Copy() call and the File.SetLastWriteTimeUtc() call. I’ve added some private notes on the Developer Community item with possible ideas about resolving the issue by adding the solution directory or MSBuild.exe to the exclusion list for the antivirus software.

I also started thinking a little about whether there might be any changes possible in Xamarin.Android to try to help complete SetLastWriteTimeUtc() if antivirus software happens to temporarily try to access the file after the Copy() call. Maybe CopyIfChanged() could retry SetLastWriteTimeUtc() after a timeout if the first attempt fails? But maybe that would be messy.