Skip to content

Commit 245ec40

Browse files
[One .NET] support latest C# 10 language features
Fixes: #6075 Fixes: #6076 Context: dotnet/macios#12173 We need to make two sets of changes for C# 10: 1. Support "global usings". Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Use `$(Nullable)` `enable` by default in project templates. To test this, our .NET 6 MSBuild tests use `Nullable=enable` by default and do no include `using` statements. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file.
1 parent e4debf7 commit 245ec40

File tree

16 files changed

+61
-82
lines changed

16 files changed

+61
-82
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(Android.OS.Bundle? savedInstanceState)
127
{
138
base.OnCreate(savedInstanceState);
149

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<PropertyGroup>
33
<TargetFramework>net6.0-android</TargetFramework>
44
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67
</Project>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<TargetFramework>net6.0-android</TargetFramework>
44
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidApp1</RootNamespace>
55
<OutputType>Exe</OutputType>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78
</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(Android.OS.Bundle? savedInstanceState)
127
{
138
base.OnCreate(savedInstanceState);
149

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<PropertyGroup>
33
<TargetFramework>net6.0-android</TargetFramework>
44
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidLib1</RootNamespace>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67
</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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--
2+
***********************************************************************************************
3+
Microsoft.Android.Sdk.ImplicitNamespaceImports.targets
4+
5+
This file contains @(Import) items for C# 10 global usings.
6+
7+
***********************************************************************************************
8+
-->
9+
<Project>
10+
<!--
11+
There's a master switch, $(DisableImplicitNamespaceImports),
12+
which, if set, will prevent all implicit namespace imports.
13+
This is done automatically, we don't have to check
14+
$(DisableImplicitNamespaceImports) here.
15+
-->
16+
<ItemGroup Condition=" '$(DisableImplicitNamespaceImports_Android)' != 'true' ">
17+
<Import Include="Android.App" />
18+
<Import Include="Android.Widget" />
19+
</ItemGroup>
20+
</Project>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<Import Project="Microsoft.Android.Sdk.BundledVersions.targets" />
2626
<Import Project="Microsoft.Android.Sdk.SupportedPlatforms.targets" />
2727
<Import Project="Microsoft.Android.Sdk.DefaultProperties.targets" />
28+
<Import Project="Microsoft.Android.Sdk.ImplicitNamespaceImports.targets" />
2829
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props"
2930
Condition="Exists('$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props')"/>
3031

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public void DotNetNew ([Values ("android", "androidlib", "android-bindinglib")]
204204
Assert.IsTrue (dotnet.New ("android-activity"), "`dotnet new android-activity` should succeed");
205205
Assert.IsTrue (dotnet.New ("android-layout", Path.Combine (dotnet.ProjectDirectory, "Resources", "layout")), "`dotnet new android-layout` should succeed");
206206
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
207+
dotnet.AssertHasNoWarnings ();
207208
}
208209

209210
[Test]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class KnownProperties
3535
public const string AndroidFastDeploymentType = "AndroidFastDeploymentType";
3636
public const string AndroidClassParser = "AndroidClassParser";
3737
public const string _AndroidAllowDeltaInstall = "_AndroidAllowDeltaInstall";
38+
public const string Nullable = "Nullable";
3839
}
3940
}
4041

0 commit comments

Comments
 (0)