Skip to content

Commit 6a61a80

Browse files
committed
[LayoutBindings] LayoutBindings: 'PreserveAttribute' is obsolete warnings
Fixes #7480 * Remove the use of `PreserveAttribute` as we no longer support Xamarin Classic in main. * Add #pragma and code comments to fix certain CS* warnings which are emitted by the C# compiler. * Ignore a bunch of other warnings in the unit test.
1 parent c663ee1 commit 6a61a80

File tree

6 files changed

+48
-26
lines changed

6 files changed

+48
-26
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/GenerateLayoutBindings.BindingGenerator.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ public sealed class State
3030
// generating code for the partial Activity class
3131
public string BindingClassName { get; }
3232

33-
public bool LinkerPreserveConstructors { get; set; }
34-
3533
public List<string> ExtraImportNamespaces { get; } = new List <string> ();
3634

3735
public string AndroidFragmentType { get; }
@@ -155,20 +153,18 @@ protected virtual void WriteOnSetContentViewPartials (State state)
155153
WritePartialClassOnSetContentViewPartial_Int (state);
156154
}
157155

158-
public State BeginBindingFile (StreamWriter writer, string layoutResourceId, string classNamespace, string className, string androidFragmentType, bool linkerPreserveConstructors = true)
156+
public State BeginBindingFile (StreamWriter writer, string layoutResourceId, string classNamespace, string className, string androidFragmentType)
159157
{
160-
var state = new State (writer, className, !String.IsNullOrWhiteSpace (classNamespace), androidFragmentType) {
161-
LinkerPreserveConstructors = linkerPreserveConstructors
162-
};
158+
var state = new State (writer, className, !String.IsNullOrWhiteSpace (classNamespace), androidFragmentType);
163159
BeginBindingFile (state, layoutResourceId, classNamespace, className);
164-
WriteBindingConstructors (state, className, state.LinkerPreserveConstructors);
160+
WriteBindingConstructors (state, className);
165161
return state;
166162
}
167163

168164
protected abstract void BeginBindingFile (State state, string layoutResourceId, string classNamespace, string className);
169165
public abstract void EndBindingFile (State state);
170166

171-
protected abstract void WriteBindingConstructors (State state, string className, bool linkerPreserve);
167+
protected abstract void WriteBindingConstructors (State state, string className);
172168
protected abstract void WriteBindingViewProperty (State state, LayoutWidget widget, string resourceNamespace);
173169
protected abstract void WriteBindingFragmentProperty (State state, LayoutWidget widget, string resourceNamespace);
174170
protected abstract void WriteBindingMixedProperty (State state, LayoutWidget widget, string resourceNamespace);
@@ -287,10 +283,11 @@ protected void WriteBindingPropertyBackingField (State state, LayoutWidget widge
287283
WriteResetLocation (state);
288284
}
289285

290-
public void WriteComment (State state, string text)
286+
public void WriteComment (State state, params string [] text)
291287
{
292288
EnsureArgument (state, nameof (state));
293-
WriteLineIndent (state, $"{LineCommentString}{text}");
289+
foreach (string line in text)
290+
WriteLineIndent (state, $"{LineCommentString}{line}");
294291
}
295292

296293
public void WriteComment (State state, ICollection<string> lines)

src/Xamarin.Android.Build.Tasks/Tasks/GenerateLayoutBindings.CSharpBindingGenerator.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,35 @@ void WriteClassClose (State state)
157157
WriteLineIndent (state, "}");
158158
}
159159

160+
void WriteDisableWarnings (State state)
161+
{
162+
state.WriteLine ("#pragma warning disable CS0618");
163+
state.WriteLine ("#pragma warning disable CS8981");
164+
state.WriteLine ("#pragma warning disable CS1591");
165+
}
166+
167+
void WriteEnableWarnings (State state)
168+
{
169+
state.WriteLine ("#pragma warning restore CS1591");
170+
state.WriteLine ("#pragma warning restore CS8981");
171+
state.WriteLine ("#pragma warning restore CS0618");
172+
}
173+
174+
void WriteAutoGeneratedComment (State state)
175+
{
176+
state.WriteLine (@"//------------------------------------------------------------------------------
177+
// <auto-generated>
178+
// This code was generated by a tool.
179+
//
180+
// Changes to this file may cause incorrect behavior and will be lost if
181+
// the code is regenerated.
182+
// </auto-generated>
183+
//------------------------------------------------------------------------------");
184+
}
185+
160186
void WriteFilePreamble (State state, string classNamespace)
161187
{
188+
WriteAutoGeneratedComment (state);
162189
WriteUsings (state);
163190
state.WriteLine ();
164191
WriteNamespaceOpen (state, classNamespace);
@@ -172,13 +199,15 @@ void WriteNamespaceOpen (State state, string classNamespace)
172199
state.WriteLine ($"namespace {classNamespace}");
173200
state.WriteLine ("{");
174201
state.IncreaseIndent ();
202+
WriteDisableWarnings (state);
175203
}
176204

177205
void WriteNamespaceClose (State state)
178206
{
179207
if (!state.IsInNamespace)
180208
return;
181209

210+
WriteEnableWarnings (state);
182211
state.DecreaseIndent ();
183212
state.WriteLine ("}");
184213
}
@@ -200,15 +229,14 @@ void WriteUsing (State state, string ns)
200229
state.WriteLine ($"using global::{ns};");
201230
}
202231

203-
protected override void WriteBindingConstructors (State state, string className, bool linkerPreserve)
232+
protected override void WriteBindingConstructors (State state, string className)
204233
{
205-
WriteConstructor (state, className, "Android.App.Activity", linkerPreserve);
206-
WriteConstructor (state, className, "Android.Views.View", linkerPreserve);
234+
WriteConstructor (state, className, "Android.App.Activity");
235+
WriteConstructor (state, className, "Android.Views.View");
207236
}
208237

209-
void WriteConstructor (State state, string className, string clientType, bool linkerPreserve)
238+
void WriteConstructor (State state, string className, string clientType)
210239
{
211-
WritePreserveAtribute (state, linkerPreserve);
212240
WriteLineIndent (state, $"public {className} (");
213241
state.IncreaseIndent ();
214242
WriteLineIndent (state, $"global::{clientType} client,");
@@ -221,14 +249,6 @@ void WriteConstructor (State state, string className, string clientType, bool li
221249
state.WriteLine ();
222250
}
223251

224-
void WritePreserveAtribute (State state, bool linkerPreserve)
225-
{
226-
if (!linkerPreserve)
227-
return;
228-
229-
WriteLineIndent (state, $"[global::Android.Runtime.PreserveAttribute (Conditional=true)]");
230-
}
231-
232252
public override void EndBindingFile (State state)
233253
{
234254
EnsureArgument (state, nameof (state));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public class GenerateResourceDesignerIntermediateClass : AndroidTask
2424
2525
namespace %NAMESPACE% {
2626
#pragma warning disable IDE0002
27+
/// <summary>
28+
/// Android Resource Designer class.
29+
/// Exposes the Android Resource designer assembly into the project Namespace.
30+
/// </summary>
2731
public partial class Resource : %BASECLASS% {
2832
}
2933
#pragma warning restore IDE0002

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ void SuccessfulBuild_AndroidX (TestProjectInfo testInfo, bool many, bool dtb, Lo
346346

347347
CopyLogs (testInfo, true);
348348
Assert.That (success, Is.True, "Build should have succeeded");
349+
Assert.IsTrue (StringAssertEx.ContainsText (builder.LastBuildOutput, " 0 Warning(s)"), $"{builder.BuildLogFile} should have no MSBuild warnings.");
349350

350351
CopyGeneratedFiles (testInfo);
351352

@@ -515,7 +516,8 @@ string GetBuildTarget (bool isDTB)
515516
string[] GetBuildProperties (LocalBuilder builder, bool manyBuild, bool dtbBuild, bool referenceAndroidX, params string[] extraConstants)
516517
{
517518
var ret = new List <string> {
518-
"AndroidGenerateLayoutBindings=true"
519+
"AndroidGenerateLayoutBindings=true",
520+
"\"NoWarn=CS0414;CA1416;CA1422;CS1591;XA1005;XA4225\""
519521
};
520522
if (manyBuild)
521523
ret.Add ("ForceParallelBuild=true");

tests/CodeBehind/BuildTests/CodeBehindBuildTests.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@
6464
<ProjectReference Include="..\CommonSampleLibrary\CommonSampleLibrary.NET.csproj" />
6565
</ItemGroup>
6666
<ItemGroup Condition=" '$(ReferenceAndroidX)' == 'True' ">
67-
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.4.1.1" />
67+
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.5" />
6868
</ItemGroup>
6969
</Project>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.CodeBehindBuildTests">
3-
<uses-sdk android:minSdkVersion="21" />
43
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
54
</application>
65
</manifest>

0 commit comments

Comments
 (0)