forked from dotnet/android
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[One .NET] support latest C# 10 language features
Fixes: dotnet#6075 Fixes: dotnet#6076 Context: xamarin/xamarin-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. 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`. I also had to write our own target to generate the using for `Bundle`, because the dotnet/sdk always prepends `global::`: https://github.com/dotnet/sdk/blob/86946f52cd012cefd811b25287a8da034bf082a3/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateImplicitNamespaceImports.targets#L46
- Loading branch information
1 parent
babd603
commit fdd34a1
Showing
16 changed files
with
96 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
using System; | ||
|
||
namespace AndroidLib1 | ||
{ | ||
public class Class1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...asks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ImplicitNamespaceImports.targets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<!-- | ||
*********************************************************************************************** | ||
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.Widget" /> | ||
</ItemGroup> | ||
|
||
<!-- | ||
NOTE: Bundle is specifically imported by itself, so | ||
Android.OS.Environment doesn't conflict with System.Environment. | ||
We have to write our own .cs file to do this, because dotnet/sdk | ||
always prefixes with `global::`, see: | ||
https://github.com/dotnet/sdk/blob/86946f52cd012cefd811b25287a8da034bf082a3/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateImplicitNamespaceImports.targets#L46 | ||
--> | ||
<Target Name="_AndroidGenerateImplicitNamespaceImports" | ||
Condition=" '$(DisableImplicitNamespaceImports_Android)' != 'true' " | ||
AfterTargets="CoreGenerateImplicitNamespaceImports"> | ||
<PropertyGroup> | ||
<_AndroidGeneratedImplicitNamespaceImportFile>$(IntermediateOutputPath)$(MSBuildProjectName).AndroidImplicitNamespaceImports$(DefaultLanguageSourceExtension)</_AndroidGeneratedImplicitNamespaceImportFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<_AndroidImportFileLine Include="// %3Cautogenerated />"/> | ||
<_AndroidImportFileLine Include="global using Bundle = global::Android.OS.Bundle%3B"/> | ||
</ItemGroup> | ||
<WriteLinesToFile | ||
Lines="@(_AndroidImportFileLine)" | ||
File="$(_AndroidGeneratedImplicitNamespaceImportFile)" | ||
Overwrite="true" | ||
WriteOnlyWhenDifferent="true" | ||
/> | ||
<ItemGroup> | ||
<Compile Include="$(_AndroidGeneratedImplicitNamespaceImportFile)" /> | ||
<FileWrites Include="$(_AndroidGeneratedImplicitNamespaceImportFile)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/DotNet/MainActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace ${ROOT_NAMESPACE} | ||
{ | ||
[Android.Runtime.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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters