android: UnauthorizedAccessExceptions are getting raised when building a blank MAUI app when using .NET 9.0

Description

I just moved to MAUI 9 this morning (specifically 9.0.0-preview.6.24327.7). I was using .NET 8.0 prior to this and everything was fine. I noticed that the first build of solution would work fine, but if I did a rebuild all, the build would fail with an UnauthorizedAccessException. The only way I can get it to go away is to close and reopen VS (I’m using 17.11.0 Preview 3.0, for the record, running in admin mode on Win 10). After that, it builds again, but only once before I start getting the UnauthorizedAccessExceptions.

To investigate, I made a blank .NET MAUI 8 app and tried it - everything builds fine. You can ‘Rebuild All’ as many times as you want and it’ll compete successfully.

Then I made a blank .NET MAUI 9 app and built it - it was fine the first time. Then I tried ‘Rebuild All’ and, unfortunately, ran into the same UnauthorizedAccessExceptions during the build:

Error (active)	XARDF7019	System.UnauthorizedAccessException: Access to the path 'GoogleGson.dll' is denied.
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound, WIN32_FIND_DATA& data)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
   at Xamarin.Android.Tasks.RemoveDirFixed.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/RemoveDirFixed.cs:line 54	MauiApp2 (net9.0-android)	C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.6.340\tools\Xamarin.Android.Common.targets	2503

and

Error (active)	XALNS7019	System.UnauthorizedAccessException: Access to the path 'D:\Projects\MauiApp2\obj\Debug\net9.0-android\android\assets\armeabi-v7a\MauiApp2.dll' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalDelete(String path, Boolean checkHost)
   at Microsoft.Android.Build.Tasks.Files.CopyIfChanged(String source, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 125
   at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:line 344
   at Xamarin.Android.Tasks.LinkAssembliesNoShrink.CopyIfChanged(ITaskItem source, ITaskItem destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 161
   at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 76
   at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25	MauiApp2 (net9.0-android)	C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\34.99.0-preview.6.340\tools\Xamarin.Android.Common.targets	1407

These all seem to be Android-releated. And when I try to deploy the successful build to my Android device, I get this error:

Error		XA0127: Error deploying 'files/.__override__/arm64-v8a/ar//Microsoft.Maui.Controls.resources.dll' using 'xamarin.sync: error: could not open 'files/.__override__/arm64-v8a/ar//Microsoft.Maui.Controls.resources.dll'.'.
Please set the 'EmbedAssembliesIntoApk' MSBuild property to 'true' to disable Fast Deployment in the Visual Studio project property pages, or edit the project file in a text editor.

I never got this error with .NET 8.0. Again, this is a fresh MAUI 9.0 app.

Steps to Reproduce

  1. Create a new, default .NET MAUI 9.0 app (use 9.0.0-preview.6.24327.7).
  2. Rebuild it. It should succeed.
  3. Rebuild it again. It should fail with an UnauthorizedAccessException.

Link to public reproduction project repository

https://github.com/whodges/mauiapp1

Version with bug

9.0.0-preview.1.9973

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.70

Affected platforms

Android

Affected platform versions

Android 31

Did you find any workaround?

Close and restart Visual Studio (but you’ll only get one build out of it).

Relevant log output

binlog_success.zip binlog_fail.zip binlog_deploy_fail.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 80 (30 by maintainers)

Commits related to this issue

Most upvoted comments

I get the same error in a MAUI Project with .NET9 and with Visual Studio 17.11 Preview 2.1. With NET8 everything works without problems

This released today:

So, you can install either VS 17.13 Preview 2, or run dotnet workload update to get the latest Android workload. Thanks!

@OvrBtn This is a serious issue with VS and Android emulator on .NET 9…an absolute productivity killer if you have to always restart VS or got to Process Explorer each time and kill off all the previous MSBuild processed just to build each time and deploy to the Android emulator to test - I hope MS dropes everything, and fixes this ASAP…they just killed off multiple devs developing MAUI apps for Android (they dare not upgrade to .NET 9 at this stage of the game - .NET 8 had none of these problems!) I must say that you do not always have to do this…sometimes it builds fine…if, you give it some time before trying to build and deploy again…it seems the locks release after a few minutes automatically and you do not need to restart VS or kill MSBuild in Process Explorer…it seems the issue is that the locks on the files in the obj folder do not release quick enough automatically…

I ran the 17.12.3 update on a VM and I can confirm that the .NET Android 35.0.24 update is not present. I also ran dotnet workload update to make sure. Looks like the fix has not yet been released. I will leave this issue open until 35.0.24 actually gets released.

I just updated to visual studio 17.12.3 which was released today and the problem still happens.

running taskkill /IM "msbuild.exe" /F does not solve the problem

I am also having the same issue. So annoying that I have to restart VS every time to have a successful build. Version 17.12.2

You don’t.

Run taskkill /IM "msbuild.exe" /F instead.

I am also having the same issue. So annoying that I have to restart VS every time to have a successful build. Version 17.12.2

Never mind , we found the issue we think thants @OvrBtn !

Next this is to figure out how to fix it

Currently what is the way to workaround this issue if there is one? killing msbuild leftovers?

You can use the same tool I did to locate the exact MSBuild.exe and kill it or try killing all the MSBuild.exe you can find but since there can be a lot of those processes running at the same time (depends on processor) it might be easier to keep the tool open and kill the exact process with 2 clicks.

Never mind , we found the issue we think thants @OvrBtn !

Next this is to figure out how to fix it

To clarify my previous comment: if you uncheck ‘Enable targeting of the Android platform’ in the project properties, these locking errors go away.

Hi I’m an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

The fix is inserted in the next VS 17.13 Preview, but we will also ship it to NuGet.org.

@amryakout2024 Yes…it is still an issue…but I think it has been fixed…we need to wait for the patch release to .NET 9 I think…not sure when that will be though

I’ve tried to repeat the build process with Windows Defender disabled and this file locking still occurs, so I don’t think it’s the culprit. Nor do I believe the retry attempts/delays will accomplish anything as this isn’t a timing issue: the files are locked and they’ll continue to be locked until the IDE is closed and restarted.

I appreciate it’s very, very difficult to diagnose/solve a problem when you can’t reproduce it. I’ve been there many times myself. I’m happy to help in anyway possible to help solve this, either by following whatever debugging ideas you may have, or even granting you remote access to my system for testing. Like I said before, I’m running Windows 10 on a physical machine (not a VM).

What do we know: this only happens with .NET 9, and not with 8 and earlier. It’s also tied to Android. So what’s changed in .NET 9 with the Android build process?

I’ve attached the latest binlog the build failure.

MauiApp1_Debug_AnyCPU_Build_2024-11-19T11_36_59.0220817-05_00.zip

This still happens in the latest VS 17.12 and it is frustrating. Every edit seems to cause the error and you can’t run your MAUI project smoothly. It only works once/twice when you close VS.

So please reopen this issue. With previous version everything is working fine, after upgrading you have to restart Visual Studio or kill hundreds of msbuild processes EVERYTIME you change your code und build your solution… so it is impossible to work anymore and this issue happens for everyone, this is a BIG ISSUE which needs an urgent Hot Fix!

Yeah. Still happens for me too. I can’t use .NET 9 because of it; stuck on 8.

The issue still exists with the final versions of .NET 9 and Visual Studio 17.12. Why was this closed, when it is still not fixed?

Corrected logs attached - sorry for the wait

binlogs.zip

I also encountered almost the same problem, but my current solution is to switch to Windows Machine, stop it and switch back to Android, and it will return to normal. In addition, this problem happens occasionally, even if I don’t modify any code. I am currently using Visual Studio 2022 v17.12 preview 2, .net 9 rc2, and the project was created using the “maui-blazor-web” template

The command line (dotnet build --no-incremental) works perfectly fine on its own. If, however, I have the IDE open, and have used it to build the solution once, both the IDE and the command line will fail with the same UnauthorizedAccessException, suggesting that it is indeed the IDE that’s locking the Android files.

Design binlogs attached (not sure if I captured them right but I think I did)

design binlogs.zip

Hi! In my situation, MsBuild.exe holds onto .dll files. If I kill that specific MsBuild instance (identified using Process Explorer), I can build the app again. Additionally, I’ve noticed that if I wait for a minute or so, the process eventually releases the .dll files, allowing me to build once more. While there’s no specific reproducible scenario, I’ve observed that certain workflows trigger this issue with locked app .dll files (it’s not always the app dll, for example once it was the CommunityToolkit.Mvvm.dll):

Case 1:

  1. Build the app.
  2. Run it.
  3. Close the app in the emulator.
  4. Make a small code change to ensure that the app needs recompilation.
  5. Attempt to build and deploy.

Case 2:

  1. The app is running in the emulator.
  2. Make a small code change to ensure that the app needs recompilation.
  3. Press hot reload.
  4. Stop the app.
  5. Attempt to build and deploy.

Case 3:

  1. The app is running in the emulator.
  2. Press the restart button (Ctrl+Shift+F5).
  3. Stop the app.
  4. Make a small code change to ensure that the app needs recompilation.
  5. Attempt to build and deploy.

Important Note: I’ve tried the mentioned scenarios with a blank/sample app using .NET 8.0 LTS, and it works perfectly. Additionally, deployment to the emulator also seems faster.

TargetFrameworks: net9.0-android;net9.0-ios;net9.0-maccatalyst VS: 17.12.0 Preview 1.0 Emulator: Pixel 5- API 34 (Android 14.0 - API 34) dotnet --version : 9.0.100-preview.7.24407.12

Exception:

System.UnauthorizedAccessException: Access to the path ‘<MY PATH :)>\repos\MauiApp2\MauiApp2\obj\Debug\net9.0-android\android\assets\armeabi-v7a\MauiApp2.dll’ is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalDelete(String path, Boolean checkHost) at Microsoft.Android.Build.Tasks.Files.CopyIfChanged(String source, String destination) in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/Files.cs:line 125 at Xamarin.Android.Tasks.MonoAndroidHelper.CopyAssemblyAndSymbols(String source, String destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs:line 344 at Xamarin.Android.Tasks.LinkAssembliesNoShrink.CopyIfChanged(ITaskItem source, ITaskItem destination) in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 161 at Xamarin.Android.Tasks.LinkAssembliesNoShrink.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssembliesNoShrink.cs:line 76 at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25