From 620c73f82d72eb5cf14a89c745a39fe07b1123db Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 13 Aug 2020 10:38:59 -0500 Subject: [PATCH] [Microsoft.Android.Sdk] use net5.0-android $(TargetFramework) After bumping to .NET 5.0.100-rc.2.20459.1, we can now use: net5.0-android On the first try, this failed with: error NETSDK1139: The target platform identifier android was not recognized. Our SDK needs to specify: true After doing this, everything seemed to work as before. I updated tests so they will use `net5.0-android` going forward. ~~ @(SdkSupportedTargetPlatform) ~~ A second part of this change is the need to fill out `$(TargetPlatformVersion)`, `@(AndroidSdkSupportedTargetPlatform)`, and `@(SdkSupportedTargetPlatform)` similar to: https://github.com/dotnet/sdk/blob/c43785736edc8d4bc927df3b557c1609d028cd93/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.WindowsSdkSupportedTargetPlatforms.props Using this data, the dotnet/sdk will run the `GenerateTargetPlatformDefineConstants` MSBuild target: https://github.com/dotnet/sdk/blob/a0e17b2b5d2ec75c16e1083b1f926a258126c73a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets#L188-L198 This fills out `$(DefineConstants)` with `ANDROID;ANDROID21;...;ANDROID30`. --- .gitignore | 3 +- Configuration.props | 2 + Documentation/guides/OneDotNet.md | 2 +- .../GenerateSupportedPlatforms.cs | 83 +++++++++++++++++++ .../automation/azure-pipelines-oss.yaml | 2 +- build-tools/automation/azure-pipelines.yaml | 2 +- src/Xamarin.Android.Build.Tasks/.gitignore | 2 + .../targets/Microsoft.Android.Sdk.targets | 4 + .../Android/XASdkProject.cs | 2 +- .../Common/DotNetXamarinProject.cs | 2 +- .../Xamarin.Android.Build.Tasks.targets | 20 +++++ .../CodeBehindBuildTestsDotNet.csproj | 2 +- .../CommonSampleLibraryDotNet.csproj | 2 +- 13 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs diff --git a/.gitignore b/.gitignore index a9f77ed3c21..69f44a9d9f1 100644 --- a/.gitignore +++ b/.gitignore @@ -19,8 +19,7 @@ logcat-*.txt apk-sizes-*.txt *.rawproto *.binlog -src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props -src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BundledVersions.props +*.ProjectImports.zip *~ external/monodroid/ external/mono/ diff --git a/Configuration.props b/Configuration.props index 6d7d9e67e60..f9871cf6263 100644 --- a/Configuration.props +++ b/Configuration.props @@ -20,6 +20,8 @@ v4.4 19 + + 21 $(AndroidFirstApiLevel) <_IsRunningNuGetRestore Condition="$(RestoreTaskAssemblyFile.EndsWith('NuGet.exe', StringComparison.InvariantCultureIgnoreCase))">True diff --git a/Documentation/guides/OneDotNet.md b/Documentation/guides/OneDotNet.md index 36320eaec60..e86d98f5e09 100644 --- a/Documentation/guides/OneDotNet.md +++ b/Documentation/guides/OneDotNet.md @@ -206,7 +206,7 @@ The following instructions can be used for early preview testing. ```xml - net5.0 + net5.0-android android.21-arm64 Exe diff --git a/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs b/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs new file mode 100644 index 00000000000..be696414456 --- /dev/null +++ b/build-tools/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateSupportedPlatforms.cs @@ -0,0 +1,83 @@ +using System.IO; +using System.Linq; +using System.Xml; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tools.BootstrapTasks +{ + /// + /// Generates Microsoft.Android.Sdk.SupportedPlatforms.props + /// Similar to: https://github.com/dotnet/sdk/blob/18ee4eac8b3abe6d554d2e0c39d8952da0f23ce5/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.WindowsSupportedTargetPlatforms.props + /// + public class GenerateSupportedPlatforms : Task + { + /// + /// A list of AndroidApiInfo.xml files produced by Mono.Android.targets + /// + [Required] + public string [] AndroidApiInfo { get; set; } + + /// + /// The output file to generate + /// + [Required] + public string OutputFile { get; set; } + + /// + /// $(AndroidMinimumDotNetApiLevel) from Configuration.props + /// + [Required] + public int MinimumApiLevel { get; set; } + + public override bool Execute () + { + if (AndroidApiInfo.Length == 0) { + Log.LogError ("This task requires at least one AndroidApiInfo.xml file!"); + return false; + } + + var versions = new AndroidVersions ( + AndroidApiInfo.Select (d => Path.GetDirectoryName (d))); + var settings = new XmlWriterSettings { + OmitXmlDeclaration = true, + Indent = true, + }; + using (var writer = XmlWriter.Create (OutputFile, settings)) { + + writer.WriteComment ($@" +*********************************************************************************************** +{Path.GetFileName (OutputFile)} + +Specifies the supported Android platform versions for this SDK. + +*********************************************************************************************** +"); + writer.WriteStartElement ("Project"); + + writer.WriteStartElement ("PropertyGroup"); + writer.WriteStartElement ("TargetPlatformVersion"); + writer.WriteAttributeString ("Condition", " '$(TargetPlatformVersion)' == '' "); + writer.WriteString (versions.MaxStableVersion.ApiLevel.ToString ()); + writer.WriteEndElement (); // + writer.WriteEndElement (); // + + writer.WriteStartElement ("ItemGroup"); + foreach (AndroidVersion version in versions.InstalledBindingVersions + .Where (v => v.ApiLevel >= MinimumApiLevel) + .OrderBy (v => v.ApiLevel)) { + writer.WriteStartElement ("AndroidSdkSupportedTargetPlatform"); + writer.WriteAttributeString ("Include", version.ApiLevel.ToString ()); + writer.WriteEndElement (); // + } + writer.WriteStartElement ("SdkSupportedTargetPlatform"); + writer.WriteAttributeString ("Condition", " '$(TargetPlatformIdentifier)' == 'Android' "); + writer.WriteAttributeString ("Include", "@(AndroidSdkSupportedTargetPlatform)"); + + writer.WriteEndDocument (); // + } + + return !Log.HasLoggedErrors; + } + } +} diff --git a/build-tools/automation/azure-pipelines-oss.yaml b/build-tools/automation/azure-pipelines-oss.yaml index e944c35758e..2c0c3fe3e92 100644 --- a/build-tools/automation/azure-pipelines-oss.yaml +++ b/build-tools/automation/azure-pipelines-oss.yaml @@ -27,7 +27,7 @@ variables: PREPARE_FLAGS: PREPARE_CI=1 PREPARE_CI_PR=1 DotNetCoreVersion: 3.1.201 # Version number from: https://github.com/dotnet/installer#installers-and-binaries - DotNetCorePreviewVersion: 5.0.100-preview.7.20307.3 + DotNetCorePreviewVersion: 5.0.100-rc.2.20459.1 stages: - stage: mac_stage diff --git a/build-tools/automation/azure-pipelines.yaml b/build-tools/automation/azure-pipelines.yaml index 360d807db61..21b8e1dd119 100644 --- a/build-tools/automation/azure-pipelines.yaml +++ b/build-tools/automation/azure-pipelines.yaml @@ -52,7 +52,7 @@ variables: NUnitConsoleVersion: 3.11.1 DotNetCoreVersion: 3.1.201 # Version number from: https://github.com/dotnet/installer#installers-and-binaries - DotNetCorePreviewVersion: 5.0.100-preview.7.20307.3 + DotNetCorePreviewVersion: 5.0.100-rc.2.20459.1 HostedMacMojave: Hosted Mac Internal Mojave HostedMac: Hosted Mac Internal HostedWinVS2019: Hosted Windows 2019 with VS2019 diff --git a/src/Xamarin.Android.Build.Tasks/.gitignore b/src/Xamarin.Android.Build.Tasks/.gitignore index 1d5bc710589..62cccf88ea7 100644 --- a/src/Xamarin.Android.Build.Tasks/.gitignore +++ b/src/Xamarin.Android.Build.Tasks/.gitignore @@ -1,2 +1,4 @@ +/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.BundledVersions.props +/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.SupportedPlatforms.props /Xamarin.Android.Common.props /Xamarin.Android.BuildInfo.txt diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets index bf62c310729..a7a2728b28e 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.targets @@ -19,9 +19,13 @@ + + true + + 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 558e96c9ce3..aeb2593fdae 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 @@ -73,7 +73,7 @@ public static void SaveGlobalJson (string directory) public XASdkProject (string outputType = "Exe") { Sdk = $"Microsoft.Android.Sdk/{SdkVersion}"; - TargetFramework = "net5.0"; + TargetFramework = "net5.0-android"; TargetSdkVersion = AndroidSdkResolver.GetMaxInstalledPlatform ().ToString (); PackageName = PackageName ?? string.Format ("{0}.{0}", ProjectName); diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetXamarinProject.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetXamarinProject.cs index 3e21b996124..726400de3ee 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetXamarinProject.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/DotNetXamarinProject.cs @@ -26,7 +26,7 @@ protected DotNetXamarinProject (string debugConfigurationName = "Debug", string SetProperty ("AssemblyName", () => AssemblyName ?? ProjectName); if (Builder.UseDotNet) { - SetProperty ("TargetFramework", "net5.0"); + SetProperty ("TargetFramework", "net5.0-android"); SetProperty ("EnableDefaultItems", "false"); SetProperty ("AppendTargetFrameworkToOutputPath", "false"); SetProperty ("AppendRuntimeIdentifierToOutputPath", "false"); diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets index c99302bdce5..6493f9f5b7f 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets @@ -1,6 +1,7 @@ + @@ -10,6 +11,10 @@ <_SharedRuntimeBuildPath Condition=" '$(_SharedRuntimeBuildPath)' == '' ">$(XAInstallPrefix)xbuild-frameworks\MonoAndroid\ <_GeneratedProfileClass>$(MSBuildThisFileDirectory)$(IntermediateOutputPath)Profile.g.cs + + $(ResolveReferencesDependsOn); + _GenerateSupportedPlatforms; + _CopyNDKTools; _GenerateXACommonProps; @@ -266,6 +271,21 @@ Replacements="@PACKAGE_VERSION@=$(ProductVersion);@PACKAGE_VERSION_BUILD@=$(XAVersionCommitCount);@NDK_ARMEABI_V7_API@=$(AndroidNdkApiLevel_ArmV7a);@NDK_ARM64_V8A_API@=$(AndroidNdkApiLevel_ArmV8a);@NDK_X86_API@=$(AndroidNdkApiLevel_X86);@NDK_X86_64_API@=$(AndroidNdkApiLevel_X86_64);@BUNDLETOOL_VERSION@=$(XABundleToolVersion)"> + + + <_AndroidApiInfo Include="$(XAInstallPrefix)xbuild-frameworks\MonoAndroid\*\AndroidApiInfo.xml" /> + + + + + - net5.0 + net5.0-android Xamarin.Android.Tests.CodeBehindBuildTests Exe True diff --git a/tests/CodeBehind/CommonSampleLibrary/CommonSampleLibraryDotNet.csproj b/tests/CodeBehind/CommonSampleLibrary/CommonSampleLibraryDotNet.csproj index 77123a4abb2..7b6fc6109b3 100644 --- a/tests/CodeBehind/CommonSampleLibrary/CommonSampleLibraryDotNet.csproj +++ b/tests/CodeBehind/CommonSampleLibrary/CommonSampleLibraryDotNet.csproj @@ -1,6 +1,6 @@ - net5.0 + net5.0-android CommonSampleLibrary CommonSampleLibrary false