Nirav Mehta
Nirav Mehta

Reputation: 7073

"CopyAndConvertResources" task failed unexpectedly (Read-only resources)

"Access to the path ...obj\Debug\res... is denied" when resource files are marked as read-only

I am getting error "Copy And Convert Resources" task failed unexpectedly (Read-only resources)

This problem is particularly noticeable when using TFS because TFS automatically marks all of the resources as read-only.

Error 1 The "CopyAndConvertResources" task failed unexpectedly. System.UnauthorizedAccessException: Access to the path 'C:\Users\Windows User\Desktop\AndroidApp1\AndroidApp1\obj\Debug\res\drawable\image1.png' is denied. 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.Tasks.CopyAndConvertResources.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.d__20.MoveNext() AndroidApp1

Upvotes: 0

Views: 1357

Answers (1)

Nirav Mehta
Nirav Mehta

Reputation: 7073

Xamarin 3.7.201 and Xamarin.Android 4.18 introduced a regression for resource files that are marked read-only on the file system. I've filed a high priority bug for the issue. Because many of the Xamarin developers are busy at Evolve this week, a hotfix for this issue might not make it out until next week.

This problem is particularly noticeable when using TFS because TFS automatically marks all of the resource files as read-only when it puts them under source control.

Workaround possibility 1: check out the Resources folder (recursively) from TFS

From the comments so far, it sounds like using TFS is the most common way to hit this problem. TFS has most likely marked the Resources\ folder and all of its contents as read-only. The following steps have succeeded in some cases as a temporary workaround:

  1. Delete the bin\ and obj\ folders.
  2. Check out the Resources\ folder from TFS (recursively). This should turn off the read-only flag on all of the files.

Workaround possibility 2: recursively remove the read-only flag from the Resources folder

You can try removing the read-only flag from the Resources\ folder and all of its contents by hand. You could for example create a copy of the solution entirely outside of TFS or SourceGear Vault, or remove just the Resources\ folder from source control, and then use this technique.

One user reported that workaround 1 did not solve the problem with TFS in their case, but adding a "Pre-build event" command to remove all the read-only attributes and a "Post-build event" command to put them back (under "Project Properties -> Build Events") did work.

Workaround possibility 3: build the project twice in a row

From the comments so far, it sounds like this method does not work when the source control has marked the files as read-only. But in simpler cases where only individual resources are marked read-only, the build error that occurs during the initial build might not appear during subsequent builds (after the resources have already been copied to the obj\ folder). It is possible to work around the problem in those cases by building the project twice during each deploy (no intervening cleans or rebuilds).

Workaround possibility 4: downgrade to Xamarin 3.6.262 (Windows) or Xamarin.Android 4.16.0.17 (Mac) or earlier

Windows

  1. Downgrade to Xamarin 3.6.262 (or earlier) for Windows: http://download.xamarin.com/XamarinforVisualStudio/Windows/Xamarin.VisualStudio_Setup-3.6.262.msi
  2. If you use Xamarin.iOS, download this corrected .targets file: https://s3.amazonaws.com/xvs.xamarin.com/Xamarin.iOS.Common.targets. (Steps 2 and 3 are needed to work around a bug in 3.6.262).
  3. Copy the file into C:\Program Files (x86)\MSBuild\Xamarin\iOS, overwriting the existing file. (This step requires administrator privileges.)
  4. If you use Xamarin.iOS, you'll also need to downgrade the Mac build host to the matching version: http://download.xamarin.com/MonoTouch/Mac/monotouch-8.0.0.63.pkg

Mac

  1. Downgrade to Xamarin.Android 4.16.0.17 or earlier: http://download.xamarin.com/MonoforAndroid/Mac/mono-android-4.16.0-17.pkg

Build error

// Error    1   The "CopyAndConvertResources" task failed unexpectedly.
// System.UnauthorizedAccessException: Access to the path 'C:\Users\Windows User\Desktop\AndroidApp1\AndroidApp1\obj\Debug\res\drawable\image1.png' is denied.
//    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.Tasks.CopyAndConvertResources.Execute()
//    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
//    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__20.MoveNext()  AndroidApp1

Note about QA testing for this particular issue

At present, TFS is not included in Xamarin's stable release testing process. If you'd like to see this added as part of the testing process we'd highly recommend voting for it on Xamarin's UserVoice. In the absence of data about the number of users who need regression testing for TFS, plans to add TFS to the testing regimen will have to wait until Xamarin has grown to have some extra testing capacity. If TFS regression testing shows up as a highly demanded suggestion on UserVoice, it can be added to the testing regimen sooner.

Upvotes: 1

Related Questions