Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add GetAndroidDependencies Target
Browse files Browse the repository at this point in the history
Fixes dotnet#1269

This commit adds the `GetAndroidDependencies` target to the
`Xamarin.Android.Common.targets`. Its purpose is to examine
the various settings in the project and report which android
sdk build-tools, platform-tools etc are required.

`GetAndroidDependencies` will output an @(AndroidDependency)
with Version metadata Valid component names are platform,
build-tool, platform-tool and tool.
  • Loading branch information
dellis1972 committed Feb 10, 2018
1 parent 7c31899 commit fd62d2f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks
{
public class CalculateProjectDependencies : Task
{
const int DefaultMinSDKVersion = 11;

[Required]
public string TargetFrameworkVersion { get; set; }

[Required]
public ITaskItem ManifestFile { get; set; }

[Required]
public string BuildToolsVersion { get; set; }

public string PlatformToolsVersion { get; set; }

public string ToolsVersion { get; set; }

[Output]
public ITaskItem [] Dependencies { get; set; }

ITaskItem CreateAndroidDependency (string include, string version)
{
if (string.IsNullOrEmpty (version))
return new TaskItem (include);

return new TaskItem (include, new Dictionary<string, string> {
{ "Version", version }
});
}

public override bool Execute ()
{
var dependencies = new List<ITaskItem> ();
var targetApiLevel = MonoAndroidHelper.SupportedVersions.GetApiLevelFromFrameworkVersion (TargetFrameworkVersion);
var manifest = AndroidAppManifest.Load (ManifestFile.ItemSpec, MonoAndroidHelper.SupportedVersions);
var manifestApiLevel = manifest.TargetSdkVersion ?? manifest.MinSdkVersion ?? DefaultMinSDKVersion;
dependencies.Add (CreateAndroidDependency ("platform", $"{Math.Max (targetApiLevel.Value, manifestApiLevel)}"));
dependencies.Add (CreateAndroidDependency ("build-tool", BuildToolsVersion));
if (!string.IsNullOrEmpty (PlatformToolsVersion)) {
dependencies.Add (CreateAndroidDependency ("platform-tool", PlatformToolsVersion));
}
if (!string.IsNullOrEmpty (ToolsVersion)) {
dependencies.Add (CreateAndroidDependency ("tool", ToolsVersion));
}
Dependencies = dependencies.ToArray ();
return !Log.HasLoggedErrors;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
<Compile Include="Tasks\JavaToolTask.cs" />
<Compile Include="Tasks\GenerateLayoutCodeBehind.cs" />
<Compile Include="Tasks\CalculateLayoutCodeBehind.cs" />
<Compile Include="Tasks\CalculateProjectDependencies.cs" />
<Compile Include="$(MonoSourceFullPath)\mcs\tools\pdb2mdb\BitAccess.cs">
<Link>pdb2mdb\BitAccess.cs</Link>
</Compile>
Expand Down
19 changes: 19 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<UsingTask TaskName="Xamarin.Android.Tasks.BuildApk" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CalculateAdditionalResourceCacheDirectories" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CalculateLayoutCodeBehind" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CalculateProjectDependencies" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CheckForRemovedItems" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CheckTargetFrameworks" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.CompileToDalvik" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
Expand Down Expand Up @@ -2767,6 +2768,24 @@ because xbuild doesn't support framework reference assemblies.
DependsOnTargets="$(InstallDependsOnTargets)">
</Target>


<!-- SDK Management Targets -->
<Target Name="GetAndroidDependencies" DependsOnTargets="$(GetAndroidDependenciesDependsOn)" Returns="@(AndroidDependency)">
<PropertyGroup>
<_ProjectAndroidManifest>$(ProjectDir)$(AndroidManifest)</_ProjectAndroidManifest>
</PropertyGroup>
<Error Text="AndroidManifest file does not exist" Condition="'$(_ProjectAndroidManifest)'!='' And !Exists ('$(_ProjectAndroidManifest)')"/>
<CalculateProjectDependencies
TargetFrameworkVersion="$(TargetFrameworkVersion)"
ManifestFile="$(_ProjectAndroidManifest)"
BuildToolsVersion="$(AndroidSdkBuildToolsVersion)"
PlatformToolsVersion="$(AndroidSdkPlatformToolsVersion)"
ToolsVersion="$(AndroidSdkToolsVersion)"
>
<Output TaskParameter="Dependencies" ItemName="AndroidDependency" />
</CalculateProjectDependencies>
</Target>

<Import Project="$(MSBuildThisFileDirectory)Xamarin.Android.Common.Debugging.targets"
Condition="Exists('$(MSBuildThisFileDirectory)Xamarin.Android.Common.Debugging.targets')"/>

Expand Down

0 comments on commit fd62d2f

Please sign in to comment.