Skip to content

Commit 5e730fc

Browse files
[One .NET] support latest C# 10 language features (#6118)
Fixes: #6075 Fixes: #6076 Context: dotnet/sdk#19521 We need to make two sets of changes for C# 10: 1. Support ["global usings"][0]. Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Set `$(Nullable)`=enable by default in project templates, i.e. enable [C#8 nullable reference types][1] by default. To test this, our .NET 6 MSBuild tests use `$(Nullable)`=enable and `$(ImplicitUsings)`=enable by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`, and the `System` namespace will be in `@(Using)` by default. `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> so that these `using`s are present at the time `.csproj` files are compiled. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely, or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup> [0]: https://github.com/dotnet/csharplang/blob/b89d4c934041db923f7238b1427cd5f3ae71ed4b/proposals/csharp-10.0/GlobalUsingDirective.md#global-using-alias-directives [1]: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references
1 parent 2028600 commit 5e730fc

File tree

20 files changed

+76
-96
lines changed

20 files changed

+76
-96
lines changed

src/Microsoft.Android.Templates/android-activity/Activity1.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using Android.App;
2-
using Android.OS;
3-
using Android.Runtime;
4-
using Android.Widget;
5-
61
namespace AndroidApp1
72
{
83
[Activity(Label = "@string/app_name", MainLauncher = true)]
94
public class Activity1 : Activity
105
{
11-
protected override void OnCreate(Bundle savedInstanceState)
6+
protected override void OnCreate(Bundle? savedInstanceState)
127
{
138
base.OnCreate(savedInstanceState);
149

src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
<TargetFramework>net6.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>SUPPORTED_OS_PLATFORM_VERSION</SupportedOSPlatformVersion>
55
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
68
</PropertyGroup>
79
</Project>

src/Microsoft.Android.Templates/android/AndroidApp1.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<SupportedOSPlatformVersion>SUPPORTED_OS_PLATFORM_VERSION</SupportedOSPlatformVersion>
55
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidApp1</RootNamespace>
66
<OutputType>Exe</OutputType>
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
79
</PropertyGroup>
810
</Project>

src/Microsoft.Android.Templates/android/MainActivity.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using Android.App;
2-
using Android.OS;
3-
using Android.Runtime;
4-
using Android.Widget;
5-
61
namespace AndroidApp1
72
{
83
[Activity(Label = "@string/app_name", MainLauncher = true)]
94
public class MainActivity : Activity
105
{
11-
protected override void OnCreate(Bundle savedInstanceState)
6+
protected override void OnCreate(Bundle? savedInstanceState)
127
{
138
base.OnCreate(savedInstanceState);
149

src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
<TargetFramework>net6.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>SUPPORTED_OS_PLATFORM_VERSION</SupportedOSPlatformVersion>
55
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidLib1</RootNamespace>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
68
</PropertyGroup>
79
</Project>

src/Microsoft.Android.Templates/androidlib/Class1.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
31
namespace AndroidLib1
42
{
53
public class Class1

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ https://github.com/dotnet/designs/blob/4703666296f5e59964961464c25807c727282cae/
2020
<_DefaultJavaSourceJarPattern>**\*-source.jar;**\*-sources.jar;**\*-src.jar</_DefaultJavaSourceJarPattern>
2121
</PropertyGroup>
2222

23+
<ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') ">
24+
<Using Include="Android.App" />
25+
<Using Include="Android.Widget" />
26+
<Using Include="Android.OS.Bundle" Alias="Bundle" />
27+
</ItemGroup>
28+
2329
<ItemGroup Condition=" '$(EnableDefaultAndroidItems)' == 'true' ">
2430
<!-- Default Resource file inclusion -->
2531
<!-- https://developer.android.com/guide/topics/resources/providing-resources -->

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/DeviceTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ protected override void CleanupTest ()
6767
{
6868
if (HasDevices && TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed &&
6969
TestOutputDirectories.TryGetValue (TestContext.CurrentContext.Test.ID, out string outputDir)) {
70+
Directory.CreateDirectory (outputDir);
7071
string local = Path.Combine (outputDir, "screenshot.png");
7172
string deviceLog = Path.Combine (outputDir, "logcat-failed.log");
7273
string remote = "/data/local/tmp/screenshot.png";

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public void DotNetNew ([Values ("android", "androidlib", "android-bindinglib")]
205205
Assert.IsTrue (dotnet.New ("android-activity"), "`dotnet new android-activity` should succeed");
206206
Assert.IsTrue (dotnet.New ("android-layout", Path.Combine (dotnet.ProjectDirectory, "Resources", "layout")), "`dotnet new android-layout` should succeed");
207207
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
208+
dotnet.AssertHasNoWarnings ();
208209
}
209210

210211
[Test]
@@ -550,7 +551,7 @@ public void SupportedOSPlatformVersion ([Values (21, 30)] int minSdkVersion)
550551
};
551552
// Call AccessibilityTraversalAfter from API level 22
552553
// https://developer.android.com/reference/android/view/View#getAccessibilityTraversalAfter()
553-
proj.MainActivity = proj.DefaultMainActivity.Replace ("button.Click", "button.AccessibilityTraversalAfter.ToString ();\nbutton.Click");
554+
proj.MainActivity = proj.DefaultMainActivity.Replace ("button!.Click", "button!.AccessibilityTraversalAfter.ToString ();\nbutton!.Click");
554555

555556
var dotnet = CreateDotNetBuilder (proj);
556557
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
@@ -645,7 +646,7 @@ void CreateEmptyFile (params string [] paths)
645646
public void XamarinLegacySdk ()
646647
{
647648
var proj = new XASdkProject (outputType: "Library") {
648-
Sdk = "Xamarin.Legacy.Sdk/0.1.0-alpha2",
649+
Sdk = "Xamarin.Legacy.Sdk/0.1.0-alpha4",
649650
Sources = {
650651
new AndroidItem.AndroidLibrary ("javaclasses.jar") {
651652
BinaryContent = () => ResourceData.JavaSourceJarTestJar,

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownProperties.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static class KnownProperties
3636
public const string AndroidFastDeploymentType = "AndroidFastDeploymentType";
3737
public const string AndroidClassParser = "AndroidClassParser";
3838
public const string _AndroidAllowDeltaInstall = "_AndroidAllowDeltaInstall";
39+
public const string Nullable = "Nullable";
40+
public const string ImplicitUsings = "ImplicitUsings";
3941
}
4042
}
4143

0 commit comments

Comments
 (0)