Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 23, 2026

  • Analyze the issue and understand the problem
  • Review the stack trace and identify where the error occurs
  • Understand the existing error handling patterns in the codebase
  • Add try-catch handling in GenerateJavaCallableWrappers.GenerateWrappers() to catch InvalidOperationException
  • Create a proper MSBuild error with error code XA4238 for the exception
  • Add a new error resource string for the error (XA4238)
  • Add test for XA4238 error scenario (InvalidConfigTests.cs)
  • Request code review - PASSED

Summary

This PR fixes issue #10715 by converting the unhandled InvalidOperationException that occurs when using [Register("CustomView")] without a package prefix into a proper MSBuild error (XA4238).

Changes Made:

  1. src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaCallableWrappers.cs - Added try-catch around JavaObjectsXmlFile.Import() to catch InvalidOperationException and log a proper MSBuild error
  2. src/Xamarin.Android.Build.Tasks/Properties/Resources.resx - Added new error message XA4238
  3. src/Xamarin.Android.Build.Tasks/Properties/Resources.Designer.cs - Added generated code for XA4238
  4. src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/InvalidConfigTests.cs - Added test for XA4238 that creates a View with [Register("CustomView")] and asserts the error is emitted

Error Message:

Before: System.InvalidOperationException: Missing required attribute 'package' (stack trace)
After: error XA4238: Could not load Java callable wrapper XML file '{path}': Missing required attribute 'package'

Original prompt

This section details on the original issue you should resolve

<issue_title>Register Attribute breaks build</issue_title>
<issue_description>RegisterException.zip

Android framework version

net10.0-android (Preview)

Affected platform version

VS 2026 18.1.1, .Net 10.0.1 (also 10.0.0)

Description

As soon as i set the "Register("ViewName") attribute to a View in .Net for Android, it breaks the build with an "InvalidOperationException" when i try to rebuild the project.

Image

Steps to Reproduce

  1. File -> New Android Project
  2. Create a CustomView that inherits from Android.Views.View --> building the project is fine
  3. Add the Attribute "Register("CustomView") to the class. --> building the project fails

With .net 9 this worked fine.

Did you find any workaround?

No, i didn't find a workaround

Relevant log output

System.InvalidOperationException: Missing required attribute 'package'
   at Java.Interop.Tools.JavaCallableWrappers.Extensions.XmlExtensions.GetRequiredAttribute(XElement xml, String name)
   at Java.Interop.Tools.JavaCallableWrappers.Adapters.XmlImporter.ImportType(XElement xml)
   at Java.Interop.Tools.JavaCallableWrappers.Adapters.XmlImporter.Import(XElement xml)
   at Xamarin.Android.Tasks.JavaObjectsXmlFile.Import(String filename, JavaObjectsXmlFileReadType readType)
   at Xamarin.Android.Tasks.GenerateJavaCallableWrappers.GenerateWrappers(List`1 assemblies)
   at Xamarin.Android.Tasks.GenerateJavaCallableWrappers.RunTask()
   at Microsoft.Android.Build.Tasks.AndroidTask.Execute()
```</issue_description>

<agent_instructions>Try to figure out what actually broke this in .NET 10. Review git history to discover what went wrong.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@jonathanpeppers</author><body>
I believe Java types require a package name, and that is what the error says?

Does this fix it:
```csharp
[Register("literallyanyname.ViewName")]
```</body></comment_new>
<comment_new><author>@jonathanpeppers</author><body>
I would not think this is anything to do with the move from Xamarin; it is the same codebase. Did you find a specific commit that suggested this?

The main thing to fix here would be to just make a real MSBuild error code instead of an exception.</body></comment_new>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix build issue caused by Register attribute in custom view Add proper MSBuild error XA4238 for JCW XML import failures Jan 23, 2026
Copilot AI requested a review from jonathanpeppers January 23, 2026 20:13
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.

@copilot can you add a test for this scenario and assert it gets XA4238

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.

We should not merge, as I understand now a fix is required in dotnet/java-interop.

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 23, 2026

@copilot can you add a test for this scenario and assert it gets XA4238

Added test in InvalidConfigTests.XA4238() that creates a custom View with [Register("CustomView")] (no package prefix) and asserts the build fails with XA4238 error. (25c088a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Register Attribute breaks build

2 participants