Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net8 Android not building because Resources from resx not found. #9057

Merged
merged 5 commits into from
Oct 15, 2024

Conversation

dellis1972
Copy link
Contributor

@dellis1972 dellis1972 commented Jun 26, 2024

Fixes #9163

With the release of the new Resource Designer assembly we purposely removed the existing legacy Resource.designer.cs file from the @(Compile) ItemGroup. This is so that we did not get any clashes with the new system.

Unfortunately the name Resource.designer.cs is also used by developers as the name for the generated class when using .resx files. As a result we got the above bug report because the class was disappearing.

error CS0234: The type or namespace name 'Resources' does not exist in the namespace 'StringResourcesBug' (are you missing an assembly reference?)

It turns out that the _RemoveLegacyDesigner was being a bit too aggressive with its removal of files. Because it was matching on filename only it was removing files which had nothing to do with Android Resources. This would cause the above error.

The fix in this case is to check for additional metadata. In the case of .resx file designers they typically have a DependentUpon metadata to signal that they are generated from the .resx. So we should check this metadata to make sure it is NOT set before removing a file.

A new unit test was added to test this fix and it worked. But it did show up one other issue. In certain cases we would end up with two Resource classes in the same namespace. This would then cause the following build error.

error CS0260: Missing partial modifier on declaration of type 'Resource'; another partial declaration of this type exists

This is because the two classes had different modifiers. We could also get an error if the access modifiers are different (public vs internal). So lets add a new MSBuild property AndroidResourceDesignerClassModifier. This property defaults to public, but can be overridden by the user to control the access modifier of the generated class.

Also we have to remove the GeneratedCode Attribute usage on the generated Resource class. Since it can conflict with the one that is auto added to the Resource.designer.cs generated by the resx code generator. If we leave this in place we get the following error.

__Microsoft.Android.Resource.Designer.cs(15,3,15,16): error CS0579: Duplicate 'GeneratedCode' attribute

Unfortunately the GeneratedCode Attribute [1] is marked as AllowMultiple=false and in this case there is no way to know if it exists on the class already as its a partial class.

[1] https://learn.microsoft.com/en-us/dotnet/api/system.codedom.compiler.generatedcodeattribute?view=net-8.0

@dellis1972 dellis1972 force-pushed the Issue_maui_19117 branch 2 times, most recently from 690f387 to aad2d2c Compare July 2, 2024 15:27
@dellis1972 dellis1972 marked this pull request as ready for review July 2, 2024 15:28
@dellis1972 dellis1972 requested a review from jonpryor as a code owner July 2, 2024 15:28
Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, LGTM.

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM. 👍 I reran some test lanes to check if they will be green.

@dellis1972 dellis1972 force-pushed the Issue_maui_19117 branch 2 times, most recently from eb3b038 to cf55b7b Compare August 6, 2024 15:06
@dellis1972 dellis1972 force-pushed the Issue_maui_19117 branch 3 times, most recently from 2d0b75c to 4050b08 Compare September 5, 2024 15:31
@dellis1972 dellis1972 requested a review from jonpryor October 8, 2024 20:47
Fixes https://github.com/dotnet/maui/issues/19117

With the release of the new Resource Designer assembly we purposely
removed the existing legacy `Resource.designer.cs` file from the
`@(Compile)` ItemGroup. This is so that we did not get any clashes
with the new system.

Unfortunately the name `Resource.designer.cs` is also used by
developers as the name for the generated class when using `.resx` files.
As a result we got the above bug report because the class was disappearing.

```
error CS0234: The type or namespace name 'Resources' does not exist in the namespace 'StringResourcesBug' (are you missing an assembly reference?)
```

It turns out that the `_RemoveLegacyDesigner` was being a bit too aggressive
with its removal of files. Because it was matching on filename only it was
removing files which had nothing to do with Android Resources. This would
cause the above error.

The fix in this case is to check for additional metadata. In the case of
`.resx` file designers they typically have a `DependentUpon` metadata to
signal that they are generated from the `.resx`. So we should check this
metadata to make sure it is NOT set before removing a file.

A new unit test was added to test this fix and it worked. But it did show
up one other issue. In certain cases we would end up with two `Resource`
classes in the same namespace. This would then cause the following
build error.

```
error CS0260: Missing partial modifier on declaration of type 'Resource'; another partial declaration of this type exists
```

This is because the two classes had different modifiers. We could also get
an error if the access modifiers are different (public vs internal).
So lets add a new MSBuild property `AndroidResourceDesignerClassModifier`.
This property defaults to `public`, but can be overridden by the user to
control the access modifier of the generated class.
It causes

```
__Microsoft.Android.Resource.Designer.cs(15,3,15,16): error CS0579: Duplicate 'GeneratedCode' attribute
```
@jonpryor jonpryor merged commit 15a1429 into dotnet:main Oct 15, 2024
57 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Nov 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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