maui icon indicating copy to clipboard operation
maui copied to clipboard

[regression/8.0.3] [Bug] .net8 Android not building because Resources from resx not found.

Open Larhei opened this issue 1 year ago • 20 comments

Description

While migrating from .net7 to .net8 i was facing the issue that my app is not compiling in vs code and vs4mac on my mac.

I get a lot of errors like: error CS0234: The type or namespace name 'Resources' does not exist in the namespace 'StringResourcesBug' (are you missing an assembly reference?)

caused by code like this: var x = StringResourcesBug.Resources.Strings.Resource.Test;

After a lot of trial an error, I found out the reason for this is the name of the class in the Resource.Designer.cs file. If you create a rest file with the name Resource.resx you are running into this issue. Renaming the file to Resources.resx and the Class to Resources fixes the issue

But the project was building on .net6 and .net7. Also iOs version in .net8 with Resouce.resx is building and is able to run. So I looks like a Android specific thing to me.

Steps to Reproduce

  1. Clone the Repo
  2. Open the Solution in VSCode or VS4Mac
  3. Compile the Solution while Targeting Android Emulator

Link to public reproduction project repository

https://github.com/Larhei/Maui-Issues/tree/main/StringResourcesBug

Version with bug

8.0.3

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.101

Affected platforms

Android

Affected platform versions

all android

Did you find any workaround?

Renaming the resx file and the name of the class in *.Designer.cs

Relevant log output

No response

Larhei avatar Nov 29 '23 17:11 Larhei

@jonathanpeppers thoughts?

mattleibow avatar Nov 29 '23 18:11 mattleibow

Having a Resource.resx could certainly conflict with the Android Resource.designer.cs:

https://github.com/Larhei/Maui-Issues/blob/main/StringResourcesBug/StringResourcesBug/Resources/Strings/Resource.resx

This is the same as if you created a plain C# Resource class yourself with the same namespace.

I don't think this actually changed in .NET 8 at all, would have happened back in Xamarin.Android as well? @dellis1972 do you think the same?

jonathanpeppers avatar Nov 29 '23 18:11 jonathanpeppers

@jonathanpeppers

That my git diff tells me the changes to my csproj are: <TargetFrameworks>net7.0-android;net7.0-ios</TargetFrameworks> changed to <TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>

added <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" /> and changed

<Compile Update="Resources\Strings\Resource.Designer.cs"> <DesignTime>True</DesignTime> <AutoGen>True</AutoGen> <DependentUpon>Resource.resx</DependentUpon> </Compile> <EmbeddedResource Update="Resources\Strings\Resource.resx"> <Generator>PublicResXFileCodeGenerator</Generator> <LastGenOutput>Resource.Designer.cs</LastGenOutput> </EmbeddedResource>

to

<Compile Update="Resources\Strings\Resources.Designer.cs"> <DependentUpon>Resources.resx</DependentUpon> </Compile> <EmbeddedResource Update="Resources\Strings\Resources.resx"> <Generator>ResXFileCodeGenerator</Generator> <LastGenOutput>Resources.Designer.cs</LastGenOutput> </EmbeddedResource>

And the old Code was building on Mac with .net7 and is in Android and iOS store.

Larhei avatar Nov 29 '23 18:11 Larhei

@Larhei can you share a .binlog of when it worked (and doesn't now?) https://aka.ms/binlog

jonathanpeppers avatar Nov 29 '23 18:11 jonathanpeppers

@jonathanpeppers

Well I will try my best ;-)

Larhei avatar Nov 29 '23 18:11 Larhei

Hi @Larhei. 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 Nov 29 '23 19:11 ghost

@jonathanpeppers

here we go Binlogs.zip

Same Machine Same VS Same Project Only difference is TFM 7 and TFM 8

Larhei avatar Nov 29 '23 20:11 Larhei

Probably conflicting with obj/Debug/net8.0-android/__Microsoft.Android.Resource.Designer.cs . Which in itself is a partial class, so if the resx code is also partial it should have worked.

dellis1972 avatar Nov 29 '23 22:11 dellis1972

I think both .NET 7/8 were partial, though:

> cat .\obj\Debug\net7.0-android\Resource.designer.cs | select -First 25
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: global::Android.Runtime.ResourceDesignerAttribute("testlib.Resource", IsApplication=false)]

namespace testlib
{


        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "13.1.99.94")]
        public partial class Resource

vs:

> cat obj\Debug\net8.0-android\__Microsoft.Android.Resource.Designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
//      This code was generated by a tool. DO NOT EDIT
// </auto-generated>
//------------------------------------------------------------------------------
using System;

namespace testlib {
        #pragma warning disable IDE0002
        public partial class Resource : _Microsoft.Android.Resource.Designer.ResourceConstant {
        }
        #pragma warning restore IDE0002
}

It's weird that 7 works somehow?

jonathanpeppers avatar Nov 29 '23 22:11 jonathanpeppers

I know. Its the inheritance, doh 🤦

dellis1972 avatar Nov 29 '23 22:11 dellis1972

I bet the resx class, does not inherit from anything. That would throw off the compiler maybe.

dellis1972 avatar Nov 29 '23 22:11 dellis1972

Here is the example:

https://github.com/Larhei/Maui-Issues/blob/72b0ba302cdaebf1e438318895c1667b02fe7385/StringResourcesBug/StringResourcesBug/Resources/Strings/Resource.Designer.cs#L10C49-L22

jonathanpeppers avatar Nov 29 '23 22:11 jonathanpeppers

actually that should not clash as its in a different namespace StringResourcesBug.Resources.Strings. The android designer will be in StringResourcesBug.Resource.

dellis1972 avatar Nov 29 '23 22:11 dellis1972

Do you have .NET Upgrade Assistant extension installed in VS? What is the build propety of your .resx files? It should be MauiResource or something similar. For me I had a similar issue: the Assistant extension somehow set my resources files build property to AndroidResource. If this is the case, chek all your other resources(images, ttf, icons).

the-gozo avatar Nov 30 '23 19:11 the-gozo

I am also having this issue with Android resources. Resource.Designer.cs file is not beging created for Android resources. I am getting the error in the IDE CS0117 but when i build it it is building. I have the old Resource.designer.cs from xamarin android project and it is never getting updated with newly address reosurces.

last-Programmer avatar Dec 04 '23 21:12 last-Programmer

@last-Programmer if you are targetting .net 8 the old Resource.designer.cs is now redundant and can be deleted. See https://devblogs.microsoft.com/dotnet/android-resource-designer-dotnet-8/ for details.

dellis1972 avatar Dec 04 '23 22:12 dellis1972

@dellis1972 Thank You very much. I missed that part. Wont i get the intellisense for resource ids any more? because in vs for mac i get the ide error CS0117 at present. Is there a way to get around this. ? In the article it does not say anything about intellisense or error CS0117

last-Programmer avatar Dec 04 '23 22:12 last-Programmer

@last-Programmer if you have the full details of the CS0117 error please post them here.

dellis1972 avatar Dec 04 '23 23:12 dellis1972

@dellis1972 It seems to be that the error CS0117 is happening only in VS for MAC and not in VS for Windows with newly created .net8 Android application

Steps to reproduce in VS for Mac.

Create a new net8.0 android application and open MainActivity.cs and you can see that at Resource.Layout.* error CS0117 is shown. However it builds fine. I suspect because .net 8.0 is only preview support in VS for Mac and it is not supported fully. Any workarounds?

last-Programmer avatar Dec 05 '23 09:12 last-Programmer

@last-Programmer not that I'm aware of. Its likely that VSForMac is not handling the new system, it might still be trying to make use of the old system. I'll mention it to my collegue on the IDE team see if they can take a look.

dellis1972 avatar Dec 05 '23 10:12 dellis1972

I used the Repo project and reproduced the problem in 17.6.9 build (415 )on android platform. image

ninachen03 avatar Mar 07 '24 08:03 ninachen03