From 6839072209c86c6d3aa7e04a89f4c302406e8183 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Fri, 15 May 2020 16:17:51 -0400 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Support cmdline-tools Context: https://github.com/xamarin/xamarin-android/pull/4567 The Android SDK `lint` utility is now in *two* Android SDK packages: the `tools` package, and the `cmdline-tools` package, e.g. as of commit d99facb1, *both* of these exist on e.g. macOS: * `$HOME/android-toolchain/sdk/tools/bin/lint` * `$HOME/android-toolchain/sdk/cmdline-tools/1.0/bin/lint` There are two important differences of interest: 1. `tools/bin/lint` will not run on OpenJDK 11. 2. `cmdline-tools/1.0/bin/lint` has a *lower version* than `tools/bin/lint`. *Much* lower: % ~/android-toolchain/sdk/tools/bin/lint --version lint: version 26.1.1 % ~/android-toolchain/sdk/cmdline-tools/1.0/bin/lint --version lint: version 3.6.0 (1) means that we can't use the previous `lint` in an OpenJDK11 world, and thus we should migrate to the cmdline-tools `lint`. Support this by adding a new `$(AndroidCommandLineToolsVersion)` MSBuild property to `Xamarin.Android.Common.props.in` and `Microsoft.Android.Sdk.props`, which allows controlling which `cmdline-tools` package version to use, if more than one is present. The default value is `1.0`. (2) means that the `` Task is *broken* when using the cmdline-tools version of `lint`, becasuse certain `lint --disable` flags are protected by version checks, which are now all "wrong" when we "expect" >= 26.1.1 and instead get 3.6.0. Attempt to address (2) by assuming that we're using the cmdline-tools package if `ToolPath` contains `cmdline-tools`, in which case we treat `lint` as newer than any previous version. (This may be less than ideal.) --- .../targets/Microsoft.Android.Sdk.Tooling.targets | 1 + .../targets/Microsoft.Android.Sdk.props | 1 + .../Tasks/CalculateProjectDependencies.cs | 5 +++++ src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs | 6 ++++-- .../Tasks/ResolveAndroidTooling.cs | 7 +++++++ src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs | 9 +++++++++ .../Xamarin.Android.Common.props.in | 1 + .../Xamarin.Android.Common.targets | 5 +---- .../Xamarin.Android.Legacy.targets | 1 + .../Xamarin.Android.Tooling.targets | 2 ++ 10 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Tooling.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Tooling.targets index b34844b4b95..0642a552b6e 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Tooling.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Tooling.targets @@ -37,6 +37,7 @@ called for "legacy" projects in Xamarin.Android.Legacy.targets. AndroidUseAapt2="$(AndroidUseAapt2)" AotAssemblies="$(AotAssemblies)" Aapt2ToolPath="$(Aapt2ToolPath)" + CommandLineToolsVersion="$(AndroidCommandLineToolsVersion)" SequencePointsMode="$(_AndroidSequencePointsMode)" ProjectFilePath="$(MSBuildProjectFullPath)" LintToolPath="$(LintToolPath)" diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.props b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.props index 816d775a459..f520b43d877 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.props +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.props @@ -4,6 +4,7 @@ true + 1.0 1.8.0 1.8.0 false diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CalculateProjectDependencies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CalculateProjectDependencies.cs index a19c4e820ab..e62ce998190 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/CalculateProjectDependencies.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CalculateProjectDependencies.cs @@ -13,6 +13,8 @@ public class CalculateProjectDependencies : AndroidTask const int DefaultMinSDKVersion = 11; + public string CommandLineToolsVersion { get; set; } + [Required] public string TargetFrameworkVersion { get; set; } @@ -58,6 +60,9 @@ public override bool RunTask () if (!string.IsNullOrEmpty (PlatformToolsVersion)) { dependencies.Add (CreateAndroidDependency ("platform-tools", PlatformToolsVersion)); } + if (!string.IsNullOrEmpty (CommandLineToolsVersion)) { + dependencies.Add (CreateAndroidDependency ($"cmdline-tools/{CommandLineToolsVersion}", CommandLineToolsVersion)); + } if (!string.IsNullOrEmpty (ToolsVersion)) { dependencies.Add (CreateAndroidDependency ("tools", ToolsVersion)); } diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs index fda5a3ea03b..d1ee15cbfcc 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs @@ -195,17 +195,19 @@ public override bool RunTask () return false; } + bool fromCmdlineTools = ToolPath.IndexOf ("cmdline-tools", StringComparison.OrdinalIgnoreCase) >= 0; + Version lintToolVersion = GetLintVersion (GenerateFullPathToTool ()); Log.LogDebugMessage (" LintVersion: {0}", lintToolVersion); foreach (var issue in DisabledIssuesByVersion) { - if (lintToolVersion >= issue.Value) { + if (fromCmdlineTools || lintToolVersion >= issue.Value) { if (string.IsNullOrEmpty (DisabledIssues) || !DisabledIssues.Contains (issue.Key)) DisabledIssues = issue.Key + (!string.IsNullOrEmpty (DisabledIssues) ? "," + DisabledIssues : ""); } } foreach (var issue in DisabledIssuesByVersion) { - if (lintToolVersion < issue.Value) { + if (!fromCmdlineTools || (lintToolVersion < issue.Value)) { DisabledIssues = CleanIssues (issue.Key, lintToolVersion, DisabledIssues, nameof (DisabledIssues)); EnabledIssues = CleanIssues (issue.Key, lintToolVersion, EnabledIssues, nameof (EnabledIssues) ); } diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAndroidTooling.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAndroidTooling.cs index 4f9731f251f..063c8bacba1 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAndroidTooling.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAndroidTooling.cs @@ -23,6 +23,8 @@ public class ResolveAndroidTooling : AndroidTask public string AndroidSdkBuildToolsVersion { get; set; } + public string CommandLineToolsVersion { get; set; } + public string ProjectFilePath { get; set; } public string SequencePointsMode { get; set; } @@ -80,8 +82,13 @@ public override bool RunTask () string toolsZipAlignPath = Path.Combine (AndroidSdkPath, "tools", ZipAlign); bool findZipAlign = (string.IsNullOrEmpty (ZipAlignPath) || !Directory.Exists (ZipAlignPath)) && !File.Exists (toolsZipAlignPath); + var commandLineToolsDir = MonoAndroidHelper.AndroidSdk.GetCommandLineToolsPaths (CommandLineToolsVersion) + .FirstOrDefault () ?? ""; + var lintPaths = new string [] { LintToolPath ?? string.Empty, + commandLineToolsDir, + Path.Combine (commandLineToolsDir, "bin"), Path.Combine (AndroidSdkPath, "tools"), Path.Combine (AndroidSdkPath, "tools", "bin"), }; diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs index 96d73e22fc7..9bb97632aa6 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs @@ -43,6 +43,11 @@ public class ResolveSdks : AndroidTask public string [] ReferenceAssemblyPaths { get; set; } + public string CommandLineToolsVersion { get; set; } + + [Output] + public string CommandLineToolsPath { get; set; } + [Output] public string AndroidNdkPath { get; set; } @@ -94,6 +99,10 @@ public override bool RunTask () AndroidSdkPath = MonoAndroidHelper.AndroidSdk.AndroidSdkPath; JavaSdkPath = MonoAndroidHelper.AndroidSdk.JavaSdkPath; + CommandLineToolsPath = MonoAndroidHelper.AndroidSdk.GetCommandLineToolsPaths (CommandLineToolsVersion) + .FirstOrDefault () ?? + Path.Combine (AndroidSdkPath, "tools"); + if (string.IsNullOrEmpty (AndroidSdkPath)) { Log.LogCodedError ("XA5300", Properties.Resources.XA5300_Android_SDK); return false; diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in index 6e5ebd1ed88..aea2b1056b0 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in @@ -9,6 +9,7 @@ true 1.8.0 1.6.0 + 1.0 {abi}{versionCode:D5} UpdateGeneratedFiles True diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 6674db836dc..c230c44e5ae 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -657,10 +657,6 @@ because xbuild doesn't support framework reference assemblies. - - - - @@ -2849,6 +2845,7 @@ because xbuild doesn't support framework reference assemblies. +