Skip to content

Commit

Permalink
[One .NET] support latest C #10 language features
Browse files Browse the repository at this point in the history
Fixes: dotnet#6075
Fixes: dotnet#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.
  • Loading branch information
jonathanpeppers committed Jul 23, 2021
1 parent 96eb449 commit e628acd
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;

namespace AndroidApp1
{
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<PropertyGroup>
<TargetFramework>net6.0-android</TargetFramework>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidBinding1</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions src/Microsoft.Android.Templates/android/AndroidApp1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<TargetFramework>net6.0-android</TargetFramework>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidApp1</RootNamespace>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
7 changes: 1 addition & 6 deletions src/Microsoft.Android.Templates/android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Widget;

namespace AndroidApp1
{
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<PropertyGroup>
<TargetFramework>net6.0-android</TargetFramework>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">AndroidLib1</RootNamespace>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
2 changes: 0 additions & 2 deletions src/Microsoft.Android.Templates/androidlib/Class1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace AndroidLib1
{
public class Class1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
***********************************************************************************************
Microsoft.Android.Sdk.ImplicitNamespaceImports.targets
This file contains @(Import) items for C# 10 global usings.
***********************************************************************************************
-->
<Project>
<!--
There's a master switch, $(DisableImplicitNamespaceImports),
which, if set, will prevent all implicit namespace imports.
This is done automatically, we don't have to check
$(DisableImplicitNamespaceImports) here.
-->
<ItemGroup Condition=" '$(DisableImplicitNamespaceImports_Android)' != 'true' ">
<Import Include="Android.App" />
<Import Include="Android.OS" />
<Import Include="Android.Runtime" />
<Import Include="Android.Widget" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Import Project="Microsoft.Android.Sdk.BundledVersions.targets" />
<Import Project="Microsoft.Android.Sdk.SupportedPlatforms.targets" />
<Import Project="Microsoft.Android.Sdk.DefaultProperties.targets" />
<Import Project="Microsoft.Android.Sdk.ImplicitNamespaceImports.targets" />
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props"
Condition="Exists('$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props')"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public void DotNetNew ([Values ("android", "androidlib", "android-bindinglib")]
Assert.IsTrue (dotnet.New ("android-activity"), "`dotnet new android-activity` should succeed");
Assert.IsTrue (dotnet.New ("android-layout", Path.Combine (dotnet.ProjectDirectory, "Resources", "layout")), "`dotnet new android-layout` should succeed");
Assert.IsTrue (dotnet.Build (), "`dotnet build` should succeed");
dotnet.AssertHasNoWarnings ();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class KnownProperties
public const string AndroidFastDeploymentType = "AndroidFastDeploymentType";
public const string AndroidClassParser = "AndroidClassParser";
public const string _AndroidAllowDeltaInstall = "_AndroidAllowDeltaInstall";
public const string Nullable = "Nullable";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static XASdkProject ()
var assembly = typeof (XASdkProject).Assembly;
using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Base.AndroidManifest.xml")))
default_android_manifest = sr.ReadToEnd ();
using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Base.MainActivity.cs")))
using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.DotNet.MainActivity.cs")))
default_main_activity_cs = sr.ReadToEnd ();
using (var sr = new StreamReader (assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Base.LayoutMain.axml")))
default_layout_main = sr.ReadToEnd ();
Expand Down Expand Up @@ -62,6 +62,7 @@ public XASdkProject (string outputType = "Exe", [CallerMemberName] string packag
JavaPackageName = JavaPackageName ?? PackageName.ToLowerInvariant ();
GlobalPackagesFolder = FileSystemUtils.FindNugetGlobalPackageFolder ();
SetProperty (KnownProperties.OutputType, outputType);
SetProperty (KnownProperties.Nullable, "enable");

// Add relevant Android content to our project without writing it to the .csproj file
if (outputType == "Exe") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class XamarinAndroidApplicationProject : XamarinAndroidCommonProject

static XamarinAndroidApplicationProject ()
{
using (var sr = new StreamReader (typeof(XamarinAndroidApplicationProject).Assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Base.MainActivity.cs")))
var folder = Builder.UseDotNet ? "DotNet" : "Base";
using (var sr = new StreamReader (typeof(XamarinAndroidApplicationProject).Assembly.GetManifestResourceStream ($"Xamarin.ProjectTools.Resources.{folder}.MainActivity.cs")))
default_main_activity_cs = sr.ReadToEnd ();
using (var sr = new StreamReader (typeof(XamarinAndroidApplicationProject).Assembly.GetManifestResourceStream ("Xamarin.ProjectTools.Resources.Base.MainActivity.fs")))
default_main_activity_fs = sr.ReadToEnd ();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace ${ROOT_NAMESPACE}
{
[Register ("${JAVA_PACKAGENAME}.MainActivity"), Activity (Label = "${PROJECT_NAME}", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;

protected override void OnCreate (Bundle? bundle)
{
base.OnCreate (bundle);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);

var button = FindViewById<Button> (Resource.Id.myButton);
button!.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};

//${AFTER_ONCREATE}
}
}
//${AFTER_MAINACTIVITY}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{2DD1EE75-6D8D-4653-A800-0A24367F7F38}</ProjectGuid>
Expand All @@ -9,72 +9,7 @@
<Import Project="..\..\..\..\external\xamarin-android-tools\src\Microsoft.Android.Build.BaseTasks\MSBuildReferences.projitems" />
<ItemGroup>
<Compile Remove="Resources\**\*.cs" />
<EmbeddedResource Include="Resources\AndroidX\Tabbar.xml">
<LogicalName>Xamarin.ProjectTools.Resources.AndroidX.Tabbar.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\AndroidX\Toolbar.xml">
<LogicalName>Xamarin.ProjectTools.Resources.AndroidX.Toolbar.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Wear\LayoutMain.axml">
<LogicalName>Xamarin.ProjectTools.Resources.Wear.LayoutMain.axml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Wear\LayoutRectMain.axml">
<LogicalName>Xamarin.ProjectTools.Resources.Wear.LayoutRectMain.axml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Wear\LayoutRoundMain.axml">
<LogicalName>Xamarin.ProjectTools.Resources.Wear.LayoutRoundMain.axml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Wear\MainActivity.cs">
<LogicalName>Xamarin.ProjectTools.Resources.Wear.MainActivity.cs</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Wear\Strings.xml">
<LogicalName>Xamarin.ProjectTools.Resources.Wear.Strings.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\AndroidManifest.xml">
<LogicalName>Xamarin.ProjectTools.Resources.Base.AndroidManifest.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\Icon.png">
<LogicalName>Xamarin.ProjectTools.Resources.Base.Icon.png</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\LayoutMain.axml">
<LogicalName>Xamarin.ProjectTools.Resources.Base.LayoutMain.axml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\MainActivity.cs">
<LogicalName>Xamarin.ProjectTools.Resources.Base.MainActivity.cs</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\MainActivity.fs">
<LogicalName>Xamarin.ProjectTools.Resources.Base.MainActivity.fs</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\AssemblyInfo.fs">
<LogicalName>Xamarin.ProjectTools.Resources.Base.AssemblyInfo.fs</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\Image.9.png">
<LogicalName>Xamarin.ProjectTools.Resources.Base.Image.9.png</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\Image2.9.png">
<LogicalName>Xamarin.ProjectTools.Resources.Base.Image2.9.png</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\test.keystore">
<LogicalName>Xamarin.ProjectTools.Resources.Base.test.keystore</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\classes.jar">
<LogicalName>Xamarin.ProjectTools.Resources.Base.classes.jar</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Base\custom.aotprofile" />
<EmbeddedResource Include="Resources\Base\BuildReleaseArm64SimpleDotNet.apkdesc" />
<EmbeddedResource Include="Resources\Base\BuildReleaseArm64SimpleLegacy.apkdesc" />
<EmbeddedResource Include="Resources\Base\BuildReleaseArm64XFormsDotNet.apkdesc" />
<EmbeddedResource Include="Resources\Base\BuildReleaseArm64XFormsLegacy.apkdesc" />
<EmbeddedResource Include="Resources\Forms\MainActivity.cs" />
<EmbeddedResource Include="Resources\Forms\App.xaml" />
<EmbeddedResource Include="Resources\Forms\App.xaml.cs" />
<EmbeddedResource Include="Resources\Forms\MainPage.xaml" />
<EmbeddedResource Include="Resources\Forms\MainPage.xaml.cs" />
<EmbeddedResource Include="Resources\Forms\MainPageMaps.xaml" />
<EmbeddedResource Include="Resources\Forms\Tabbar.axml" />
<EmbeddedResource Include="Resources\Forms\Toolbar.axml" />
<EmbeddedResource Include="Resources\Forms\colors.xml" />
<EmbeddedResource Include="Resources\Forms\styles.xml" />
<EmbeddedResource Include="Resources\**\*" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\..\..\.nuget\NuGet.exe">
Expand Down

0 comments on commit e628acd

Please sign in to comment.