Skip to content

Commit a33084b

Browse files
authored
[generator] Wrap SupportedOSPlatformAttribute in NET (#779)
Fixes: #777 Commit da12df4 updated `generator` to emit and use a `SupportedOSPlatformAttribute`, wrapping it in `NET5_0_OR_GREATER` to prevent it from being defined in .NET 6: #if !NET5_0_OR_GREATER namespace System.Runtime.Versioning { partial class SupportedOSPlatformAttribute { } } #endif `NET5_0_OR_GREATER` was chosen because, at the time, we believed that it would be defined when building code targeting .NET 5+. Unfortunately, this is not yet the case; it's only in the [design stage][0]. Consequently, when building on xamarin-android CI, [unit tests fail][1] because the `SupportedOSPlatformAttribute` type is defined when it shouldn't be defined: …/DotNetBuildandroid.21-armandroid.21-arm64android.21-x86android.21-x64False/obj/Debug/net6.0-android/UnnamedProject.AssemblyInfo.cs(21,38): warning CS0436: The type 'SupportedOSPlatformAttribute' in '…/DotNetBuildandroid.21-armandroid.21-arm64android.21-x86android.21-x64False/obj/Debug/net6.0-android/generated/src/__NamespaceMapping__.cs' conflicts with the imported type 'SupportedOSPlatformAttribute' in 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Using the type defined in '…/DotNetBuildandroid.21-armandroid.21-arm64android.21-x86android.21-x64False/obj/Debug/net6.0-android/generated/src/__NamespaceMapping__.cs'. (It's a warning, but the containing test asserts that there are no build warnings, hence the failing unit test.) Instead, wrap `SupportedOSPlatformAttribute` in `NET`, which is currently defined when building code for .NET 5+, but *not* defined when building code targeting the Xamarin.Android target frameworks or the `net472` target framework moniker. #if !NET namespace System.Runtime.Versioning { partial class SupportedOSPlatformAttribute { } } #endif [0]: https://github.com/dotnet/designs/blob/6149aa6df1567d4d69e1219247a1e423b775457f/accepted/2020/or-greater-defines/or-greater-defines.md [1]: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=4370096&view=ms.vss-test-web.build-test-results-tab&runId=18107834&resultId=100055&paneView=attachments
1 parent 44192fa commit a33084b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

tools/generator/Java.Interop.Tools.Generator.ObjectModel/NamespaceMapping.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
3535
// [SupportedOSPlatform] only exists in .NET 5.0+, so we need to generate a
3636
// dummy one so earlier frameworks can compile.
3737
if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1) {
38-
sw.WriteLine ("#if !NET5_0_OR_GREATER");
38+
sw.WriteLine ("#if !NET");
3939
sw.WriteLine ("namespace System.Runtime.Versioning {");
4040
sw.WriteLine (" [System.Diagnostics.Conditional(\"NEVER\")]");
4141
sw.WriteLine (" [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");

0 commit comments

Comments
 (0)