diff --git a/Documentation/guides/OneDotNetBindingProjects.md b/Documentation/guides/OneDotNetBindingProjects.md index f0881f518e4..f16ea18b502 100644 --- a/Documentation/guides/OneDotNetBindingProjects.md +++ b/Documentation/guides/OneDotNetBindingProjects.md @@ -217,5 +217,36 @@ As the default for .NET 6 is `disable`, the same applies for Xamarin Android pro Use `enable` as shown above to enable NRT support. - [11-0-release-notes]: https://docs.microsoft.com/en-us/xamarin/android/release-notes/11/11.0 + +### `Resource.designer.cs` + +In Xamarin.Android, Java binding projects did not support generating a `Resource.designer.cs` file. +Since binding projects are just class libraries in .NET 6, this file will be generated. This could +be a breaking change when migrating existing projects. + +One example of a failure from this change, is if your binding generates a class named `Resource` +in the root namespace: + +``` +error CS0101: The namespace 'MyBinding' already contains a definition for 'Resource' +``` + +Or in the case of AndroidX, we have project files with `-` in the name such as +`androidx.window/window-extensions.csproj`. This results in the root namespace `window-extensions` +and invalid C# in `Resource.designer.cs`: + +``` +error CS0116: A namespace cannot directly contain members such as fields, methods or statements +error CS1514: { expected +error CS1022: Type or namespace definition, or end-of-file expected +``` + +To disable `Resource.designer.cs` generation, set `$(AndroidGenerateResourceDesigner)` to `false` +in your `.csproj`: + +```xml + + false + +``` diff --git a/Documentation/guides/building-apps/build-properties.md b/Documentation/guides/building-apps/build-properties.md index 06e038b28df..a01cbf51337 100644 --- a/Documentation/guides/building-apps/build-properties.md +++ b/Documentation/guides/building-apps/build-properties.md @@ -569,6 +569,12 @@ Enables generation of [layout code-behind](https://github.com/xamarin/xamarin-an if set to `true` or disables it completely if set to `false`. The default value is `false`. +## AndroidGenerateResourceDesigner + +Defaults to `true`. When set to `false`, disables the generation of `Resource.designer.cs`. + +Added in .NET 6 RC 1. Not supported in Xamarin.Android. + ## AndroidHttpClientHandlerType Controls the default diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets index cbe556852cf..382fede7a3a 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets @@ -7,11 +7,11 @@ Resources Assets Resource - $(MonoAndroidResourcePrefix)\$(_AndroidResourceDesigner) true Xamarin.Android.Net.AndroidMessageHandler Xamarin.Android.Net.AndroidClientHandler - true + true + $(AndroidGenerateResourceDesigner) false false false diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs index f2606c22da5..190eeea70b0 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs @@ -286,6 +286,37 @@ public void DotNetLibraryAarChanges () } } + [Test] + public void GenerateResourceDesigner_false() + { + var proj = new XASdkProject (outputType: "Library") { + Sources = { + new AndroidItem.AndroidResource (() => "Resources\\drawable\\foo.png") { + BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi, + }, + } + }; + // Turn off Resource.designer.cs and remove usage of it + proj.SetProperty ("AndroidGenerateResourceDesigner", "false"); + proj.MainActivity = proj.DefaultMainActivity + .Replace ("Resource.Layout.Main", "0") + .Replace ("Resource.Id.myButton", "0"); + + var dotnet = CreateDotNetBuilder (proj); + Assert.IsTrue (dotnet.Build (), "build should succeed"); + + var intermediate = Path.Combine (FullProjectDirectory, proj.IntermediateOutputPath); + var resource_designer_cs = Path.Combine (intermediate, "Resource.designer.cs"); + FileAssert.DoesNotExist (resource_designer_cs); + + var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.dll"); + FileAssert.Exists (assemblyPath); + using var assembly = AssemblyDefinition.ReadAssembly (assemblyPath); + var typeName = $"{proj.ProjectName}.Resource"; + var type = assembly.MainModule.GetType (typeName); + Assert.IsNull (type, $"{assemblyPath} should *not* contain {typeName}"); + } + [Test] [Category ("SmokeTests")] public void DotNetBuildBinding () diff --git a/tests/CodeBehind/BuildTests/CodeBehindBuildTests.NET.csproj b/tests/CodeBehind/BuildTests/CodeBehindBuildTests.NET.csproj index 2831e8349d7..5d703c262ee 100644 --- a/tests/CodeBehind/BuildTests/CodeBehindBuildTests.NET.csproj +++ b/tests/CodeBehind/BuildTests/CodeBehindBuildTests.NET.csproj @@ -7,6 +7,7 @@ <_ApkDebugKeyStore>debug.keystore false false + Resources\Resource.designer.cs false false false diff --git a/tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj b/tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj index 9cb12a9b4ca..3ad19f518e5 100644 --- a/tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj +++ b/tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj @@ -33,7 +33,6 @@ -