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
- Create a new, default .NET MAUI 9.0 app (use 9.0.0-preview.6.24327.7).
- Rebuild it. It should succeed.
- 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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 9
- Comments: 80 (30 by maintainers)
Commits related to this issue
- [Microsoft.Android.Build.BaseTasks] report locked files for UnauthorizedAccessException Context: https://github.com/dotnet/android/issues/9133 In the above issue, a customer got: error XARDF701... — committed to jonathanpeppers/xamarin-android-tools by jonathanpeppers 2 years ago
- [Microsoft.Android.Build.BaseTasks] report locked files for UnauthorizedAccessException (#244) Context: https://github.com/dotnet/android/issues/9133 In the above issue, a customer got: err... — committed to dotnet/android-tools by jonathanpeppers 2 years ago
- [Microsoft.Android.Build.BaseTasks] retry when copying files (#245) Context: https://github.com/dotnet/android/issues/9133 Context: https://learn.microsoft.com/visualstudio/msbuild/copy-task?view=vs... — committed to dotnet/android-tools by dellis1972 2 years ago
- [Xamarin.Android.Build.Tasks] Add retry ability to RemoveDirFixed (#9409) Fixes: https://github.com/dotnet/android/issues/9133 Context: https://github.com/dotnet/android-tools/commit/60fae1924e2d7... — committed to dotnet/android by dellis1972 2 years ago
- [Xamarin.Android.Build.Tasks] Add retry ability to RemoveDirFixed (#9409) Fixes: https://github.com/dotnet/android/issues/9133 Context: https://github.com/dotnet/android-tools/commit/60fae1924e2d7... — committed to MaxMood96/xamarin-android by dellis1972 2 years ago
- [Xamarin.Android.Build.Tasks] close `XAAssemblyResolvers` Context: https://discord.com/channels/732297728826277939/732297916680765551/1308554103206580244 Fixes: https://github.com/dotnet/android/issu... — committed to dotnet/android by jonathanpeppers 2 years ago
- [Xamarin.Android.Build.Tasks] close `XAAssemblyResolvers` (#9531) Fixes: https://github.com/dotnet/android/issues/9133 Context: https://discord.com/channels/732297728826277939/732297916680765551/1... — committed to jonathanpeppers/xamarin-android by jonathanpeppers 2 years ago
- [Xamarin.Android.Build.Tasks] close `XAAssemblyResolvers` (#9531) Fixes: https://github.com/dotnet/android/issues/9133 Context: https://discord.com/channels/732297728826277939/732297916680765551/1... — committed to dotnet/android by jonathanpeppers 2 years ago
- Bump back to version 8 because of dotnet/android/issues/9133 — committed to avans-marc/RandomFactApp by avans-marc 2 years ago
- Added Map Feature + Version Bumpback (#2) * Initial commit with readme * Added alternative implementation to demonstrate the advantage of DI. * Added Union Architecture & Unit Tests (#1) * ... — committed to avans-marc/RandomFactApp by avans-marc 2 years ago
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 updateto 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 updateto 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" /Fdoes not solve the problemYou don’t.
Run
taskkill /IM "msbuild.exe" /Finstead.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 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:
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 sameUnauthorizedAccessException, 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:
Case 2:
Case 3:
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