diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveJdkJvmPath.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveJdkJvmPath.cs new file mode 100644 index 00000000000..6213e37288f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveJdkJvmPath.cs @@ -0,0 +1,71 @@ +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System; +using System.IO; +using System.Linq; +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + /// + /// This MSBuild task's job is to find $(JdkJvmPath) used by $(AndroidGenerateJniMarshalMethods) + /// + public class ResolveJdkJvmPath : Task + { + public string JavaSdkPath { get; set; } + + [Output] + public string JdkJvmPath { get; set; } + + public override bool Execute () + { + try { + JdkJvmPath = GetJvmPath (); + } catch (Exception e) { + Log.LogCodedError ("XA5300", $"Unable to find {nameof (JdkJvmPath)}{Environment.NewLine}{e}"); + return false; + } + + if (string.IsNullOrEmpty (JdkJvmPath)) { + Log.LogCodedError ("XA5300", $"{nameof (JdkJvmPath)} is blank"); + return false; + } + + if (!File.Exists (JdkJvmPath)) { + Log.LogCodedError ("XA5300", $"JdkJvmPath not found at {JdkJvmPath}"); + return false; + } + + return !Log.HasLoggedErrors; + } + + string GetJvmPath () + { + var key = new Tuple (nameof (ResolveJdkJvmPath), JavaSdkPath); + var cached = BuildEngine4.GetRegisteredTaskObject (key, RegisteredTaskObjectLifetime.AppDomain) as string; + if (cached != null) { + Log.LogDebugMessage ($"Using cached value for {nameof (JdkJvmPath)}: {cached}"); + + return cached; + } + + JdkInfo info = null; + try { + info = new JdkInfo (JavaSdkPath); + } catch { + info = JdkInfo.GetKnownSystemJdkInfos (this.CreateTaskLogger ()).FirstOrDefault (); + } + + if (info == null) + return null; + + var path = info.JdkJvmPath; + if (string.IsNullOrEmpty (path)) + return null; + + BuildEngine4.RegisterTaskObject (key, path, RegisteredTaskObjectLifetime.AppDomain, allowEarlyCollection: false); + + return path; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs index 96bded448da..47affd4ea2a 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs @@ -50,9 +50,6 @@ public class ResolveSdks : Task [Output] public string JavaSdkPath { get; set; } - [Output] - public string JdkJvmPath { get; set; } - [Output] public string MonoAndroidToolsPath { get; set; } @@ -96,65 +93,18 @@ public override bool Execute () return false; } - try { - JdkJvmPath = GetJvmPath (); - } catch (Exception e) { - Log.LogCodedError ("XA5300", $"Unable to find {nameof (JdkJvmPath)}{Environment.NewLine}{e}"); - return false; - } - - if (string.IsNullOrEmpty (JdkJvmPath)) { - Log.LogCodedError ("XA5300", $"{nameof (JdkJvmPath)} is blank"); - return false; - } - - if (!File.Exists (JdkJvmPath)) { - Log.LogCodedError ("XA5300", $"JdkJvmPath not found at {JdkJvmPath}"); - return false; - } - MonoAndroidHelper.TargetFrameworkDirectories = ReferenceAssemblyPaths; Log.LogDebugMessage ($"{nameof (ResolveSdks)} Outputs:"); Log.LogDebugMessage ($" {nameof (AndroidSdkPath)}: {AndroidSdkPath}"); Log.LogDebugMessage ($" {nameof (AndroidNdkPath)}: {AndroidNdkPath}"); Log.LogDebugMessage ($" {nameof (JavaSdkPath)}: {JavaSdkPath}"); - Log.LogDebugMessage ($" {nameof (JdkJvmPath)}: {JdkJvmPath}"); Log.LogDebugMessage ($" {nameof (MonoAndroidBinPath)}: {MonoAndroidBinPath}"); Log.LogDebugMessage ($" {nameof (MonoAndroidToolsPath)}: {MonoAndroidToolsPath}"); //note: this task does not error out if it doesn't find all things. that's the job of the targets return !Log.HasLoggedErrors; } - - string GetJvmPath () - { - var key = new Tuple (nameof (ResolveSdks), JavaSdkPath); - var cached = BuildEngine4.GetRegisteredTaskObject (key, RegisteredTaskObjectLifetime.AppDomain) as string; - if (cached != null) { - Log.LogDebugMessage ($"Using cached value for {nameof (JdkJvmPath)}: {cached}"); - - return cached; - } - - Xamarin.Android.Tools.JdkInfo info = null; - try { - info = new Xamarin.Android.Tools.JdkInfo (JavaSdkPath); - } catch { - info = Xamarin.Android.Tools.JdkInfo.GetKnownSystemJdkInfos (this.CreateTaskLogger ()).FirstOrDefault (); - } - - if (info == null) - return null; - - var path = info.JdkJvmPath; - if (string.IsNullOrEmpty (path)) - return null; - - BuildEngine4.RegisterTaskObject (key, path, RegisteredTaskObjectLifetime.AppDomain, allowEarlyCollection: false); - - return path; - } } } diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj index 18761fa5376..b42f32795c3 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -142,6 +142,7 @@ + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 56922330dbc..b584525693e 100755 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -27,6 +27,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. + @@ -710,10 +711,14 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. - + + +