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

[Bug]: WireUpControls throws exception in net8.0-android #3714

Closed
tommy10344 opened this issue Jan 19, 2024 · 11 comments
Closed

[Bug]: WireUpControls throws exception in net8.0-android #3714

tommy10344 opened this issue Jan 19, 2024 · 11 comments
Labels

Comments

@tommy10344
Copy link

Describe the bug 🐞

When calling ReactiveUI.ControlFetcherMixin.WireUpControls() in a .NET 8 Android app (net8.0-android), even though the correct resource ID is specified, a MissingFieldException is thrown.

Stacktrace:

Exception has occurred: System.MissingFieldException
Failed to wire up the Property XXX to a View in your layout with a corresponding identifier
    at ReactiveUI.ControlFetcherMixin.WireUpControls(Activity activity, ResolveStrategy resolveMembers) in /_/src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs:166
    at ReactiveUI_WireUpControls_Issue.MainActivity.OnCreate(Bundle savedInstanceState) in /path/to/projects/ReactiveUI-WireUpControls-Issue/MainActivity.cs:19
    at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.App.Activity.cs:3082
    at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:125

Step to reproduce

  1. Install .NET 8. (I installed 8.0.100)
  2. Create a new net8.0-android project using dotnet new android command.
  3. Add reference to ReactiveUI version 19.5.39
  4. Open MainActivity.cs and change it so that MainActivity inherits ReactiveUI.ReactiveActivity.
public class MainActivity : ReactiveActivity
  1. Assign an ID(android:id) to the TextView in activity_main.xml and add a member to MainActivity.cs to wire it up.
    <TextView 
        android:id="@+id/app_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/app_text"
    />
    [WireUpResource("app_text")]
    public TextView AppText { get; private set; }
  1. Add a call to WireUpControls() after SetContentView() in OnCreate().
this.WireUpControls(ControlFetcherMixin.ResolveStrategy.ExplicitOptIn);
  1. Build and launch application.
  2. MissingFieldException is thrown from WireUpControls().

Reproduction repository

https://github.com/tommy10344/ReactiveUI-WireUpControls-Issue

Expected behavior

After launching the application, the TextView should be wired up correctly, and the initial screen should be displayed.

Build the same code with .NET 7 (net7.0-android), it works as expected. (Confirmed in the dotnet7 branch of the reproduction repository)

Screenshots 🖼️

MissingFieldException

IDE

Visual Studio Code

Operating system

Android

Version

13

Device

Emulator

ReactiveUI Version

19.5.39

Additional information ℹ️

.NET 8 seems to have changed access to Android resources, is this issue related to this?
https://devblogs.microsoft.com/dotnet/android-resource-designer-dotnet-8/

Reading the implementation of WireUpControls(), it looks like it uses reflection to reference the Resource.Id class in the assembly of the application itself.
https://github.com/reactiveui/ReactiveUI/blob/main/src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs#L230

However, in .NET8, it seems that a new assembly called _Microsoft.Android.Resource.Designer is created and the Resource.Id class is generated there.
I am guessing that this may be related to the problem.
(I'm not very familiar with .NET, so sorry if I'm wrong)

@tommy10344 tommy10344 added the bug label Jan 19, 2024
@dpvreony
Copy link
Member

dpvreony commented Feb 23, 2024

can you try

<AndroidUseDesignerAssembly>False</AndroidUseDesignerAssembly>

if it works then you're correct it's a resource issue with the new build chain.

@tommy10344
Copy link
Author

@dpvreony Thanks! Sorry for the late reply.

I added <AndroidUseDesignerAssembly>False</AndroidUseDesignerAssembly> to csproj and tried to build it and got the following build error.

/usr/local/share/dotnet/packs/Microsoft.Android.Sdk.Darwin/34.0.52/tools/Xamarin.Android.Resource.Designer.targets(146,3): error XA1034: Your project references '/Users/foo/.nuget/packages/reactiveui/19.5.39/lib/net8.0-android34.0/ReactiveUI.dll' which uses the `_Microsoft.Android.Resource.Designer` assembly, but you do not have this feature enabled. Please set the `AndroidUseDesignerAssembly` MSBuild property to `true` in your project file. [/path/to/ReactiveUI-WireUpControls-Issue/ReactiveUI-WireUpControls-Issue.csproj]

To use this setting, does ReactiveUI also need to have the same setting?
https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/building-apps/build-properties.md#androidusedesignerassembly

.NET 8 Projects which choose to turn this setting off will not be able to consume references which do use it. If you try to use an assembly which does have this feature enabled in a project that does not, you will get a XA1034 build error.

Or is there any other way?

@gerardvanderkruijs
Copy link

I am having the same issue with ReactiveUI version 19.6.1, any updates on this?

@ChrisPulman
Copy link
Member

Hi, I will try to take a look at this at the weekend, I will be travelling back to the UK from Brazil tomorrow if I have any chance during the flight I will take a look then.

@ChrisPulman
Copy link
Member

So after some investigations it looks like there is a more widespread issue with this, for some reason the Resource returns no elements for net 8.0. I am trying to see if there is a different method of obtaining Resources with net 8. I will update on my findings and create a fix unless its a core bug with net 8

@ChrisPulman
Copy link
Member

image
This shows the issue we are experiencing with net 8.0

@ChrisPulman
Copy link
Member

ChrisPulman commented Apr 24, 2024

https://devblogs.microsoft.com/dotnet/android-resource-designer-dotnet-8/
This explains the reason, I will work on fixing this ASAP

.NET Blog
In .NET 8 we have made some vast improvements in changes to how Android resources work in regards to the designer assembly. If you are building Android apps and libraries there are some important changes to know about.

ChrisPulman added a commit that referenced this issue Apr 24, 2024
ChrisPulman added a commit that referenced this issue Apr 26, 2024
<!-- Please be sure to read the
[Contribute](https://github.com/reactiveui/reactiveui#contribute)
section of the README -->

**What kind of change does this PR introduce?**
<!-- Bug fix, feature, docs update, ... -->

Fix for #3714 

**What is the current behavior?**
<!-- You can also link to an open issue here. -->

WireUpControls throws exception with .Net 8.0 Android

**What is the new behavior?**
<!-- If this is a feature change -->

WireUpControls works with .Net 8.0 Android

**What might this PR break?**

none expected

**Please check if the PR fulfills these requirements**
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)

**Other information**:
@ChrisPulman
Copy link
Member

This will be resolved in the release in the next hour, thank you for raising this issue and sorry we took so long to resolve it.

@gerardvanderkruijs
Copy link

@ChrisPulman Amazing!!
Just installed the updated package and everything works like a charm again, thank you so much! 🙌

@tommy10344
Copy link
Author

@ChrisPulman Thank you for resolving this issue!
I have updated to 19.6.12 and confirmed that it works as expected without throwing exceptions.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants