Skip to content

Commit 02c3347

Browse files
[One .NET] add $(AndroidGenerateResourceDesigner) property (#6224)
Fixes: #6198 When migrating Xamarin.Android binding projects to .NET 6, one difference is that `Resource.designer.cs` will be generated by .NET 6 class libraries. This can result in build failures, e.g.: error CS0101: The namespace 'MyBinding' already contains a definition for 'Resource' There is not currently a way to turn this off; if you try: dotnet build -p:AndroidUseIntermediateDesignerFile=false Then `Resources\Resource.designer.cs` will be generated instead. There is no way to prevent this behavior, because `$(AndroidResgenFile)` defaults to this value. To solve cases where you *don't* want `Resource.designer.cs` at all: 1. Remove the `$(AndroidResgenFile)` default value. 2. Add a new `$(AndroidGenerateResourceDesigner)` MSBuild property, that seems like it will be a better name for .NET 6. `$(AndroidGenerateResourceDesigner)` defaults to True. 3. `$(AndroidUseIntermediateDesignerFile)` is based on `$(AndroidGenerateResourceDesigner)`. This way, if a Xamarin.Android binding project is migrated to .NET 6, the binding project can set `$(AndroidGenerateResourceDesigner)`=False to prevent `Resource.designer.cs` from being generated, which may prevent build failures. I added a test and docs for the new property.
1 parent 5f408eb commit 02c3347

File tree

6 files changed

+72
-4
lines changed

6 files changed

+72
-4
lines changed

Documentation/guides/OneDotNetBindingProjects.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,36 @@ As the default for .NET 6 is `disable`, the same applies for Xamarin Android pro
217217

218218
Use `enable` as shown above to enable NRT support.
219219

220-
221220
[11-0-release-notes]: https://docs.microsoft.com/en-us/xamarin/android/release-notes/11/11.0
221+
222+
### `Resource.designer.cs`
223+
224+
In Xamarin.Android, Java binding projects did not support generating a `Resource.designer.cs` file.
225+
Since binding projects are just class libraries in .NET 6, this file will be generated. This could
226+
be a breaking change when migrating existing projects.
227+
228+
One example of a failure from this change, is if your binding generates a class named `Resource`
229+
in the root namespace:
230+
231+
```
232+
error CS0101: The namespace 'MyBinding' already contains a definition for 'Resource'
233+
```
234+
235+
Or in the case of AndroidX, we have project files with `-` in the name such as
236+
`androidx.window/window-extensions.csproj`. This results in the root namespace `window-extensions`
237+
and invalid C# in `Resource.designer.cs`:
238+
239+
```
240+
error CS0116: A namespace cannot directly contain members such as fields, methods or statements
241+
error CS1514: { expected
242+
error CS1022: Type or namespace definition, or end-of-file expected
243+
```
244+
245+
To disable `Resource.designer.cs` generation, set `$(AndroidGenerateResourceDesigner)` to `false`
246+
in your `.csproj`:
247+
248+
```xml
249+
<PropertyGroup>
250+
<AndroidGenerateResourceDesigner>false</AndroidGenerateResourceDesigner>
251+
</PropertyGroup>
252+
```

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@ Enables generation of [layout code-behind](https://github.com/xamarin/xamarin-an
569569
if set to `true` or disables it completely if set to `false`. The
570570
default value is `false`.
571571

572+
## AndroidGenerateResourceDesigner
573+
574+
Defaults to `true`. When set to `false`, disables the generation of `Resource.designer.cs`.
575+
576+
Added in .NET 6 RC 1. Not supported in Xamarin.Android.
577+
572578
## AndroidHttpClientHandlerType
573579

574580
Controls the default

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<MonoAndroidResourcePrefix Condition=" '$(MonoAndroidResourcePrefix)' == '' ">Resources</MonoAndroidResourcePrefix>
88
<MonoAndroidAssetsPrefix Condition=" '$(MonoAndroidAssetsPrefix)' == '' ">Assets</MonoAndroidAssetsPrefix>
99
<AndroidResgenClass Condition=" '$(AndroidResgenClass)' == '' ">Resource</AndroidResgenClass>
10-
<AndroidResgenFile Condition=" '$(AndroidResgenFile)' == '' ">$(MonoAndroidResourcePrefix)\$(_AndroidResourceDesigner)</AndroidResgenFile>
1110
<AndroidEnableSGenConcurrent Condition=" '$(AndroidEnableSGenConcurrent)' == '' ">true</AndroidEnableSGenConcurrent>
1211
<AndroidHttpClientHandlerType Condition=" '$(AndroidHttpClientHandlerType)' == '' and '$(UsingAndroidNETSdk)' == 'true' ">Xamarin.Android.Net.AndroidMessageHandler</AndroidHttpClientHandlerType>
1312
<AndroidHttpClientHandlerType Condition=" '$(AndroidHttpClientHandlerType)' == '' and '$(UsingAndroidNETSdk)' != 'true' ">Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
14-
<AndroidUseIntermediateDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' == '' ">true</AndroidUseIntermediateDesignerFile>
13+
<AndroidGenerateResourceDesigner Condition=" '$(AndroidGenerateResourceDesigner)' == '' ">true</AndroidGenerateResourceDesigner>
14+
<AndroidUseIntermediateDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' == '' ">$(AndroidGenerateResourceDesigner)</AndroidUseIntermediateDesignerFile>
1515
<GenerateDependencyFile Condition=" '$(GenerateDependencyFile)' == '' ">false</GenerateDependencyFile>
1616
<CopyLocalLockFileAssemblies Condition=" '$(CopyLocalLockFileAssemblies)' == '' ">false</CopyLocalLockFileAssemblies>
1717
<ComputeNETCoreBuildOutputFiles Condition=" '$(ComputeNETCoreBuildOutputFiles)' == '' ">false</ComputeNETCoreBuildOutputFiles>

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,37 @@ public void DotNetLibraryAarChanges ()
286286
}
287287
}
288288

289+
[Test]
290+
public void GenerateResourceDesigner_false()
291+
{
292+
var proj = new XASdkProject (outputType: "Library") {
293+
Sources = {
294+
new AndroidItem.AndroidResource (() => "Resources\\drawable\\foo.png") {
295+
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi,
296+
},
297+
}
298+
};
299+
// Turn off Resource.designer.cs and remove usage of it
300+
proj.SetProperty ("AndroidGenerateResourceDesigner", "false");
301+
proj.MainActivity = proj.DefaultMainActivity
302+
.Replace ("Resource.Layout.Main", "0")
303+
.Replace ("Resource.Id.myButton", "0");
304+
305+
var dotnet = CreateDotNetBuilder (proj);
306+
Assert.IsTrue (dotnet.Build (), "build should succeed");
307+
308+
var intermediate = Path.Combine (FullProjectDirectory, proj.IntermediateOutputPath);
309+
var resource_designer_cs = Path.Combine (intermediate, "Resource.designer.cs");
310+
FileAssert.DoesNotExist (resource_designer_cs);
311+
312+
var assemblyPath = Path.Combine (FullProjectDirectory, proj.OutputPath, $"{proj.ProjectName}.dll");
313+
FileAssert.Exists (assemblyPath);
314+
using var assembly = AssemblyDefinition.ReadAssembly (assemblyPath);
315+
var typeName = $"{proj.ProjectName}.Resource";
316+
var type = assembly.MainModule.GetType (typeName);
317+
Assert.IsNull (type, $"{assemblyPath} should *not* contain {typeName}");
318+
}
319+
289320
[Test]
290321
[Category ("SmokeTests")]
291322
public void DotNetBuildBinding ()

tests/CodeBehind/BuildTests/CodeBehindBuildTests.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<_ApkDebugKeyStore>debug.keystore</_ApkDebugKeyStore>
88
<EnableDefaultItems>false</EnableDefaultItems>
99
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
10+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
1011
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
1112
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
1213
<AppendTargetFrameworkToIntermediateOutputPath>false</AppendTargetFrameworkToIntermediateOutputPath>

tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
<ItemGroup>
3535
<Compile Remove="..\Resources\Resource.designer.cs" />
36-
<Compile Include="Resources\Resource.designer.cs" />
3736
<Compile Include="System\AppContextTests.cs" />
3837
<!-- Mono.Data.Sqlite is not supported in .NET 6 -->
3938
<Compile Remove="..\Mono.Data.Sqlite\SqliteTests.cs" />

0 commit comments

Comments
 (0)