Skip to content

Commit 7b16e97

Browse files
jpobstjonathanpeppers
authored andcommitted
[Xamarin.Android.Build.Tasks] Add support for $(AndroidEnableObsoleteOverrideInheritance). (#8393)
Context: dotnet/java-interop@8e63cc8 Context: dotnet/java-interop@d0231c5 dotnet/java-interop@d0231c5c updated generator output so that `[Obsolete]` would automagically "flow" to derived types and method overrides, so that [warning CS0672][0] would not be emitted. Before dotnet/java-interop@d0231c5c, we would emit: public class Context { [Obsolete] public virtual void SetWallpaper () { … } } public class ContextWrapper : Context { [ObsoletedOSPlatform ("android23.0")] public override void SetWallpaper () { … } } `ContextWrapper.SetWallpaper()` would elicit a CS0672 warning: warning CS0672: Member 'ContextWrapper.SetWallpaper()' overrides obsolete member 'Context.SetWallpaper()'. Add the Obsolete attribute to 'ContextWrapper.SetWallpaper()' With dotnet/java-interop@d0231c5c, we now emit the following, which no longer emits CS0672: public class Context { [Obsolete] public virtual void SetWallpaper () { … } } public class ContextWrapper : Context { [Obsolete] public override void SetWallpaper () { … } } While we feel that this is a nice improvement, this broke some customers who didn't want their bindings to automatically gain `[Obsolete]` "just because" they were overriding `[Obsolete]` types or members. In dotnet/java-interop@8e63cc8d, we added the ability to globally *opt-out* of this new behavior by using `generator --lang-features=do-not-fix-obsolete-overrides`. Add a new `$(AndroidEnableObsoleteOverrideInheritance)` MSBuild property. When False, this adds `generator --lang-features=do-not-fix-obsolete-overrides`, preventing the automatic CS0672 fix behavior. By default `$(AndroidEnableObsoleteOverrideInheritance)` is True. [0]: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0672
1 parent 2958ae6 commit 7b16e97

4 files changed

Lines changed: 15 additions & 0 deletions

File tree

Documentation/guides/building-apps/build-properties.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ Support for this property was added in Xamarin.Android 5.1.
469469

470470
This property is `False` by default.
471471

472+
## AndroidEnableObsoleteOverrideInheritance
473+
474+
A boolean property that determines if bound methods automatically inherit `[Obsolete]`
475+
attributes from methods they override.
476+
477+
Support for this property was added in .NET 8.
478+
479+
This property is `True` by default.
480+
472481
## AndroidEnablePreloadAssemblies
473482

474483
A boolean property that controls

src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Core.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ It is shared between "legacy" binding projects and .NET 5 projects.
9999
EnableBindingNestedInterfaceTypes="$(AndroidBoundInterfacesContainTypes)"
100100
EnableBindingInterfaceConstants="$(AndroidBoundInterfacesContainConstants)"
101101
EnableRestrictToAttributes="$(AndroidEnableRestrictToAttributes)"
102+
EnableObsoleteOverrideInheritance="$(AndroidEnableObsoleteOverrideInheritance)"
102103
Nullable="$(Nullable)"
103104
UseJavaLegacyResolver="$(_AndroidUseJavaLegacyResolver)"
104105
NamespaceTransforms="@(AndroidNamespaceReplacement)"

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<AndroidBoundInterfacesContainStaticAndDefaultInterfaceMethods Condition=" '$(AndroidBoundInterfacesContainStaticAndDefaultInterfaceMethods)' == '' ">true</AndroidBoundInterfacesContainStaticAndDefaultInterfaceMethods>
4040
<AndroidBoundInterfacesContainTypes Condition=" '$(AndroidBoundInterfacesContainTypes)' == '' ">true</AndroidBoundInterfacesContainTypes>
4141
<AndroidBoundInterfacesContainConstants Condition=" '$(AndroidBoundInterfacesContainConstants)' == '' ">true</AndroidBoundInterfacesContainConstants>
42+
<AndroidEnableObsoleteOverrideInheritance Condition=" '$(AndroidEnableObsoleteOverrideInheritance)' == '' ">true</AndroidEnableObsoleteOverrideInheritance>
4243
<AndroidEnableRestrictToAttributes Condition=" '$(AndroidEnableRestrictToAttributes)' == '' ">obsolete</AndroidEnableRestrictToAttributes>
4344

4445
<!-- Mono components -->

src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class BindingsGenerator : AndroidDotnetToolTask
5252
public bool EnableBindingNestedInterfaceTypes { get; set; }
5353
public bool EnableBindingInterfaceConstants { get; set; }
5454
public string EnableRestrictToAttributes { get; set; }
55+
public bool EnableObsoleteOverrideInheritance { get; set; }
5556
public string Nullable { get; set; }
5657

5758
public ITaskItem[] TransformFiles { get; set; }
@@ -217,6 +218,9 @@ protected override string GenerateCommandLineCommands ()
217218
if (EnableBindingStaticAndDefaultInterfaceMethods)
218219
features.Add ("default-interface-methods");
219220

221+
if (!EnableObsoleteOverrideInheritance)
222+
features.Add ("do-not-fix-obsolete-overrides");
223+
220224
if (string.Equals (EnableRestrictToAttributes, "obsolete", StringComparison.OrdinalIgnoreCase))
221225
features.Add ("restrict-to-attributes");
222226

0 commit comments

Comments
 (0)