diff --git a/src/Microsoft.Android.Templates/android-activity/Activity1.cs b/src/Microsoft.Android.Templates/android-activity/Activity1.cs index f974a451ba3..e6d6f33c9a5 100644 --- a/src/Microsoft.Android.Templates/android-activity/Activity1.cs +++ b/src/Microsoft.Android.Templates/android-activity/Activity1.cs @@ -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); diff --git a/src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj b/src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj index 5a6a74ace70..9a3a8828d34 100644 --- a/src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj +++ b/src/Microsoft.Android.Templates/android-bindinglib/AndroidBinding1.csproj @@ -3,5 +3,6 @@ net6.0-android SUPPORTED_OS_PLATFORM_VERSION AndroidBinding1 + enable \ No newline at end of file diff --git a/src/Microsoft.Android.Templates/android/AndroidApp1.csproj b/src/Microsoft.Android.Templates/android/AndroidApp1.csproj index 1fd328a5c21..4823037ce64 100644 --- a/src/Microsoft.Android.Templates/android/AndroidApp1.csproj +++ b/src/Microsoft.Android.Templates/android/AndroidApp1.csproj @@ -4,5 +4,6 @@ SUPPORTED_OS_PLATFORM_VERSION AndroidApp1 Exe + enable \ No newline at end of file diff --git a/src/Microsoft.Android.Templates/android/MainActivity.cs b/src/Microsoft.Android.Templates/android/MainActivity.cs index 7e49926946e..b48279dbee4 100644 --- a/src/Microsoft.Android.Templates/android/MainActivity.cs +++ b/src/Microsoft.Android.Templates/android/MainActivity.cs @@ -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); diff --git a/src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj b/src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj index 5eb4df59b23..83ef401621d 100644 --- a/src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj +++ b/src/Microsoft.Android.Templates/androidlib/AndroidLib1.csproj @@ -3,5 +3,6 @@ net6.0-android SUPPORTED_OS_PLATFORM_VERSION AndroidLib1 + enable \ No newline at end of file diff --git a/src/Microsoft.Android.Templates/androidlib/Class1.cs b/src/Microsoft.Android.Templates/androidlib/Class1.cs index 1004d5770c0..5eebfbb4e60 100644 --- a/src/Microsoft.Android.Templates/androidlib/Class1.cs +++ b/src/Microsoft.Android.Templates/androidlib/Class1.cs @@ -1,5 +1,3 @@ -using System; - namespace AndroidLib1 { public class Class1 diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets index 709ebebf4ce..f1f26f23542 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets @@ -106,4 +106,7 @@ $(ApplicationVersion) + + + diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ImplicitNamespaceImports.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ImplicitNamespaceImports.targets new file mode 100644 index 00000000000..1f92097bbb3 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.ImplicitNamespaceImports.targets @@ -0,0 +1,50 @@ + + + + + + + + + + + + <_AndroidGeneratedImplicitNamespaceImportFile>$(IntermediateOutputPath)$(MSBuildProjectName).AndroidImplicitNamespaceImports$(DefaultLanguageSourceExtension) + + + <_AndroidImportFileLine Include="// %3Cautogenerated />"/> + <_AndroidImportFileLine Include="global using Bundle = global::Android.OS.Bundle%3B"/> + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs index 492df957658..ecb63355408 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs @@ -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] diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownProperties.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownProperties.cs index 43c92db5284..2f6bb15c9ad 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownProperties.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/KnownProperties.cs @@ -36,6 +36,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"; } } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XASdkProject.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XASdkProject.cs index 6882724244f..23048072bf3 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XASdkProject.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XASdkProject.cs @@ -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 (); @@ -61,6 +61,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") { diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidApplicationProject.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidApplicationProject.cs index a02e24fdd1b..570a3247b99 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidApplicationProject.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidApplicationProject.cs @@ -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 (); @@ -40,6 +41,7 @@ public XamarinAndroidApplicationProject (string debugConfigurationName = "Debug" { if (Builder.UseDotNet) { SetProperty (KnownProperties.OutputType, "Exe"); + SetProperty (KnownProperties.Nullable, "enable"); SetProperty ("XamarinAndroidSupportSkipVerifyVersions", "True"); SetProperty ("_FastDeploymentDiagnosticLogging", "True"); @@ -81,7 +83,7 @@ public XamarinAndroidApplicationProject (string debugConfigurationName = "Debug" } // it is exposed as public because we may want to slightly modify this. - public string DefaultMainActivity { + public virtual string DefaultMainActivity { get { return Language == XamarinAndroidProjectLanguage.FSharp ? default_main_activity_fs : default_main_activity_cs; } } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidLibraryProject.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidLibraryProject.cs index 86b3142c557..75d0c3d8223 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidLibraryProject.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinAndroidLibraryProject.cs @@ -15,6 +15,7 @@ public XamarinAndroidLibraryProject (string debugConfigurationName = "Debug", st : base (debugConfigurationName, releaseConfigurationName) { if (!Builder.UseDotNet) { + SetProperty (KnownProperties.Nullable, "enable"); SetProperty ("AndroidApplication", "False"); SetProperty ("AndroidResgenFile", Path.Combine ("Resources", "Resource.designer.cs")); } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinFormsAndroidApplicationProject.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinFormsAndroidApplicationProject.cs index 1d740f477cf..c7716b0685a 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinFormsAndroidApplicationProject.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Android/XamarinFormsAndroidApplicationProject.cs @@ -42,6 +42,8 @@ public XamarinFormsAndroidApplicationProject (string debugConfigurationName = "D : base (debugConfigurationName, releaseConfigurationName, packageName) { if (Builder.UseDotNet) { + // NOTE: `Xamarin.Forms` namespace will collide with `Android.App` otherwise: + SetProperty ("DisableImplicitNamespaceImports_Android", "true"); PackageReferences.Add (KnownPackages.XamarinForms_4_7_0_1142); } else { PackageReferences.Add (KnownPackages.XamarinForms_4_0_0_425677); @@ -76,6 +78,8 @@ public XamarinFormsAndroidApplicationProject (string debugConfigurationName = "D MainPage = MainPage_xaml_cs; } + public override string DefaultMainActivity => default_main_activity_cs; + public string MainPage { get; set; } protected virtual string MainPageXaml () => ProcessSourceTemplate (MainPage_xaml); diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/DotNet/MainActivity.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/DotNet/MainActivity.cs new file mode 100644 index 00000000000..ed408f5cf1b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Resources/DotNet/MainActivity.cs @@ -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