Skip to content

LayoutBindings: 'PreserveAttribute' is obsolete warnings #7480

Description

@rgroenewoudt

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

VS2022 17.3.6

Description

We are using a lot of AndroidBoundLayout in our app but this results in 2 warnings per layout file:

warning CS0618: 'PreserveAttribute' is obsolete: 'Please use [System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute]'
[global::Android.Runtime.PreserveAttribute (Conditional=true)]
public AboutSequriX (
	global::Android.App.Activity client,
	global::Xamarin.Android.Design.OnLayoutItemNotFoundHandler itemNotFoundHandler = null)
		: base (client, itemNotFoundHandler)
{}

[global::Android.Runtime.PreserveAttribute (Conditional=true)]
public AboutSequriX (
	global::Android.Views.View client,
	global::Xamarin.Android.Design.OnLayoutItemNotFoundHandler itemNotFoundHandler = null)
		: base (client, itemNotFoundHandler)
{}

We would like to get to 0 build warnings :)

Steps to Reproduce

Did you find any workaround?

No response

Relevant log output

No response

Activity

  1. dellis1972 commented on Oct 21, 2022

    @dellis1972
    Contributor

    Those look like they are coming from the Xamarin.Android.Support.Design Nuget Package.
    I believe the recomendation now is to upgrade to use AndroidX rather than the old packages.
    So you will need to migrate the code away from the old Support libraries to AndroidX.

    See https://learn.microsoft.com/en-us/xamarin/android/platform/androidx for more details.

    The PreserveAttribute is going to be removed in a future version of .net so migrating is the best option.

  2. rgroenewoudt commented on Oct 21, 2022

    @rgroenewoudt
    Author

    @dellis1972 I don't have any nuget packages Xamarin.Android.Support or any referencing it. Only AndroidX.

    Example project. I created a new .NET 6 Android project, changed activity_main.xml to AndroidBoundlayout and added an ID.
    AndroidApp2.zip

    1>C:\git\AndroidApp2\AndroidApp2\obj\Debug\net6.0-android\generated\Binding.activity_main.g.cs(12,4,12,45): warning CS0618: 'PreserveAttribute' is obsolete: 'Please use [System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute]'
    1>C:\git\AndroidApp2\AndroidApp2\obj\Debug\net6.0-android\generated\Binding.activity_main.g.cs(19,4,19,45): warning CS0618: 'PreserveAttribute' is obsolete: 'Please use [System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute]'
    
  3. dellis1972 commented on Oct 21, 2022

    @dellis1972
    Contributor

    Ah. Are you using LayoutBindings?

  4. dellis1972 commented on Oct 21, 2022

    @dellis1972
    Contributor

    Ignore that, yes you are :) sorry.

    OK, that is where the issue is then.

  5. changed the title [-]'PreserveAttribute' is obsolete warnings[/-] [+]LayoutBindings: 'PreserveAttribute' is obsolete warnings[/+] on Oct 21, 2022
  6. rgroenewoudt commented on Oct 26, 2022

    @rgroenewoudt
    Author
  7. dellis1972 commented on Oct 26, 2022

    @dellis1972
    Contributor

    DynamicDependencyAttribute works in the reverse manner to the old PreserveAttribute. The old system you placed the attribute at the code you wanted to preserver. The new DynamicDependencyAttribute you have to place it where the type is consumed. So we need to figure out the best way to deal with this without requiring the user to add the DynamicDependencyAttribute to the activity where its used.

  8. rgroenewoudt commented on Oct 27, 2022

    @rgroenewoudt
    Author

    Is the attribute even needed? This is the C# binding which is used by the developer so if unused constructors are removed it shouldn't be a problem?

    The file should also contain the autogenerated comment so any warnings would be ignored.

  9. jpobst commented on Nov 28, 2022

    @jpobst
    Contributor
  10. rgroenewoudt commented on Mar 13, 2023

    @rgroenewoudt
    Author

    I'm still seeing this issue in NET 7 Android 33, even though #1051 is resolved.

    There is no autogenerated comment in generated/Binding.xxx.g.cs

  11. jpobst commented on Mar 13, 2023

    @jpobst
    Contributor

    This change was too late to make it into .NET 7, I would suspect that it is fixed in .NET 8 Previews.

  12. rgroenewoudt commented on Nov 23, 2023

    @rgroenewoudt
    Author

    Same issue in .NET 8.
    xxxx\obj\Debug\net8.0-android\codebehind\Binding.StartWorkShift.g.cs(19,4,19,45): warning CS0618: 'PreserveAttribute' is obsolete: 'Please use [System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute]'

  13. added a commit that references this issue on Nov 30, 2023
    c1caf61
  14. rgroenewoudt commented on Dec 21, 2023

    @rgroenewoudt
    Author

    Would it be possible to backport to .NET 8 if it's a limited change: only adding <auto-generated> comment?

  15. jonathanpeppers commented on Jan 3, 2024

    @jonathanpeppers
    Member

    Yes, we can consider taking this one to .NET 8 servicing after it's merged:

  16. added a commit that references this issue on Jan 4, 2024
    0a427d1
  17. added 2 commits that reference this issue on Feb 16, 2024
    96cd1f9
    5dd7d3a
  18. added 2 commits that reference this issue on Mar 1, 2024
    65b409b
    bf44afd
  19. locked and limited conversation to collaborators on Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App+Library BuildIssues when building Library projects or Application projects.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions