maui icon indicating copy to clipboard operation
maui copied to clipboard

Accessing string resource throws a false FileNotFoundException in Android

Open jlbilh opened this issue 2 years ago • 14 comments

Description

The first time any string resource is accessed in Android, this exception is thrown by the resource manager: System.IO.FileNotFoundException: ''

This does not break any functionality and can be ignored, but creates a developer experience problem. Developers who rely on exception trapping to detect runtime issues will find the feed to be polluted and may ignore real issues.

Steps to Reproduce

New MAUI App Enable Break When Thrown (Ctrl+Alt+E) ->All Common Language Runtime Exceptions Project properties->Resources->General->Create or open assembly resources New String resource {"Name":"AResource", "Value":"A resource"} image

Open MainPage.xaml.cs Add this line to OnCounterClicked: System.Diagnostics.Debug.WriteLine(Properties.Resources.AResource);

Build and run on Android. Tap the button, and the exception described above will be thrown.

Version with bug

Release Candidate 3 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12

Did you find any workaround?

Disable Break When Thrown->[System.Exception, System.IO.FileNotFoundException, System.IO.IOException, System.SystemException].

Relevant log output

No response

jlbilh avatar May 24 '22 15:05 jlbilh

If you try the same thing from a straight net6.0-android app (Ex. dotnet new android, one without MAUI) does it do the same thing?

drasticactions avatar May 24 '22 15:05 drasticactions

Hi @jlbilh. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar May 24 '22 15:05 ghost

When I create an Android app in the manner asked to, it will not build. The following errors occur:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1514	{ expected	androidapp2	.\MainActivity.cs	1	Active
Error	CS1513	} expected	androidapp2	.\MainActivity.cs	13	Active
Error	NU1012	Platform version is not present for one or more target frameworks, even though they have specified a platform: net6.0-android	androidapp2	.\androidapp2.csproj	1	
Error	NETSDK1139	The target platform identifier android was not recognized.	androidapp2	C:\Program Files\dotnet\sdk\5.0.408\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets	109	

Apologies if this is a basic problem. I have never developed a mobile app with Visual Studio before, and am exploring MAUI.NET as an entry point for this.

jlbilh avatar May 24 '22 17:05 jlbilh

repro with vs main build(32524.329.main)

VincentBu avatar May 25 '22 07:05 VincentBu

@VincentBu can you try again and attach a repro please?

PureWeen avatar Jun 28 '22 20:06 PureWeen

@PureWeen not repro with vs main build 32630.352, here is an example MauiApp7443.zip

VincentBu avatar Jul 01 '22 06:07 VincentBu

I have the issue described by @jlbilh in my current project as well. However, initially I hadn't been able to recreate it in an example/debug project either.

Wonder if the exception is related to these log entries:

[monodroid-assembly] open_from_bundles: failed to load assembly en-US/MyLibrary.resources.dll
[monodroid-assembly] open_from_bundles: failed to load assembly en-US/MyLibrary.resources.dll
[monodroid-assembly] open_from_bundles: failed to load assembly en/MyLibrary.resources.dll
[monodroid-assembly] open_from_bundles: failed to load assembly en/MyLibrary.resources.dll
**System.IO.FileNotFoundException:** ''

My library is called MyLibrary.dll, so I don't expect MyLibrary.resources.dll to exist.

When continuing, after the breakpoint has been thrown, the strings seem to be displayed correctly. I have had a look in my /data/data/<project name>/files/__override__/ directory and found that only MyLibrary.dll exists. (and no MyLibrary.resources.dll)

My Microsoft.Maui.* libraries are on v6.0.408. Microsoft.Android is on 32.0.424. I'm debugging on a physical device Android 10 (SDK 29) The issue is not present when targeting Windows 10 OS Build 19044.1766

Finally I've tested the code provided by @VincentBu. And was able to reproduce the issue, key is that you have to set your exception settings to break on System.IO.FileNotFoundException or you'll miss it.

Wynadorn avatar Jul 18 '22 10:07 Wynadorn

Get the error too, just sporadically - that's weird

Workaround that worked for me:

  • creating a copy of my fallback resource file and renaming it to "AppResources.en-us.resx" (AppResources.resx is my fallback filename).
  • Clean up and redeploy.

snoova avatar Aug 02 '22 10:08 snoova

I have the same issue.

pierre01 avatar Aug 09 '22 04:08 pierre01

Same issue on Android 11.

izabelalaiancu avatar Aug 11 '22 12:08 izabelalaiancu

Same issue with iOS. The first attempt to access resources that are not in the main project fail. This is a huge problem because I don't want to ignore a 'file not found' exception.

DRAirey1 avatar Aug 27 '22 19:08 DRAirey1

@jonathanpeppers is this a limitation of Android or some build process is doing some work?

mattleibow avatar Nov 10 '22 21:11 mattleibow

What is the stack trace of the exception being thrown? I would guess this is BCL library code in dotnet/runtime.

jonathanpeppers avatar Nov 10 '22 22:11 jonathanpeppers

The stack trace is:

XXX.App.InitializeComponent() in XXX/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator/App.xaml.sg.cs:22
XXX.App..ctor(Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope provider, XXX.PreferenceService preferenceService, XXX.Services.SessionService sessionService) in XXX.App.xaml.cs:23
XXX.Program.Main(string[] args) in XXX/Platforms/iOS/Program.cs:14

Inner Exception:
   at System.Reflection.Assembly.Load(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
   at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(Assembly assembly, CultureInfo culture, Version version, Boolean throwOnFileNotFound)

Caused by setting MainPage

 MainPage = new NavigationPage(Routing.GetOrCreateContent(Routes.MyView, _serviceProvider) as Page);

MyView contains a Label:

<Label Text="{x:Static strings:Resource.MyString}" />

This results in:

ResourceManager.GetString("MyString", resourceCulture);

Here resourceCulture is null.

My App specifies in .plist

 <key>CFBundleLocalizations</key>
  <array>
    <string>de</string>
  </array>

and in .csproj

<NeutralLanguage>de</NeutralLanguage>

System language on osx is en iOS-Simulator language is en.

So the exception is correct. It can not find a SatelliteAssembly for Resource.resx because there is no SatelliteAssembly. But getting this exception every time I start debugging... is annoying.

Larhei avatar Nov 11 '22 23:11 Larhei

because there is no SatelliteAssembly.

@Larhei does a similar thing happen in a .NET 6 console app? This might be a general .NET thing.

jonathanpeppers avatar Nov 15 '22 20:11 jonathanpeppers

Tested with dotnet new console on Mac with .net 6 and 7. Works on my machine without Exception. For what ever reason it tries not to access a not existing SatelliteAssembly.

Larhei avatar Nov 16 '22 15:11 Larhei

Is there any progress (at least workaround) with this issue? I am facing it in my project as well...

hlavli avatar Jan 25 '23 08:01 hlavli

@hlavli I think the workaround is to prevent the debugger from breaking on certain exceptions:

Disable Break When Thrown->[System.Exception, System.IO.FileNotFoundException, System.IO.IOException, System.SystemException].

This does not break any functionality and can be ignored, but creates a developer experience problem. Developers who rely on exception trapping to detect runtime issues will find the feed to be polluted and may ignore real issues.

Is this issue crashing your app? That sounds different.

jonathanpeppers avatar Jan 26 '23 15:01 jonathanpeppers

It doesn't lead to breakdown of my app, but it is disrupting my development a lot and it is very annoying. Although the breaking of this kind of exception has been disabled, the app is not running smoothly and stopping every time until continue button is pressed... For the first time the issue appears somewhere in InitializeComponent method in AppShell constructor. I had even tried to put this in try-catch block but the catch section is not reached in any time this exception is raised also further in other parts of code... image

hlavli avatar Jan 27 '23 07:01 hlavli

@hlavli I think the workaround is to prevent the debugger from breaking on certain exceptions:

1.) Disabling the exception doesn't work and 2.) this is a terrible exception to disable.

DRAirey1 avatar Jan 27 '23 12:01 DRAirey1

There is a problem with Fast Deployment, where files like en-US/MyLibrary.resources.dll aren't deployed properly.

Should be fixed in:

  • (private repo) https://github.com/xamarin/monodroid/pull/1276
  • https://github.com/xamarin/xamarin-android/pull/7741

jonathanpeppers avatar Jan 27 '23 16:01 jonathanpeppers

Another example of this is: https://github.com/TonyHenrique/bug-2023-03-21

jonathanpeppers avatar Apr 10 '23 17:04 jonathanpeppers

@jonathanpeppers can this be closed, then?

jstedfast avatar May 09 '23 18:05 jstedfast