Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidDefineConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (C) 2011 Xamarin, Inc. All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
Expand All @@ -16,21 +17,25 @@ public class GetAndroidDefineConstants : Task
public string ProductVersion { get; set; }

[Output]
public string AndroidDefineConstants { get; set; }
public ITaskItem[] AndroidDefineConstants { get; set; }

public override bool Execute ()
{
var sb = new StringBuilder ();
var constants = new List<ITaskItem> ();

if (!string.IsNullOrEmpty (ProductVersion)) {
sb.AppendFormat ("__XAMARIN_ANDROID_{0}__;", Regex.Replace (ProductVersion, "[^A-Za-z0-9]", "_"));
var version = Regex.Replace (ProductVersion, "[^A-Za-z0-9]", "_");
constants.Add (new TaskItem ($"__XAMARIN_ANDROID_{version}__"));
}
sb.Append ("__MOBILE__;__ANDROID__");

for (int i = 1; i <= AndroidApiLevel; ++i)
sb.Append (";__ANDROID_").Append (i).Append ("__");
constants.Add (new TaskItem ("__MOBILE__"));
constants.Add (new TaskItem ("__ANDROID__"));

AndroidDefineConstants = sb.ToString ();
for (int i = 1; i <= AndroidApiLevel; ++i) {
constants.Add (new TaskItem ($"__ANDROID_{i}__"));
}

AndroidDefineConstants = constants.ToArray ();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ public void BuildBasicApplicationReleaseFSharp ()
}
}

[Test]
public void FSharpAppHasAndroidDefine ()
{
var proj = new XamarinAndroidApplicationProject () {
Language = XamarinAndroidProjectLanguage.FSharp,
};
proj.Sources.Add (new BuildItem ("Compile", "IsAndroidDefined.fs") {
TextContent = () => @"
module Xamarin.Android.Tests
// conditional compilation; can we elicit a compile-time error?
let x =
#if __ANDROID__
42
#endif // __ANDROID__

printf ""%d"" x
",
});
using (var b = CreateApkBuilder ("temp/" + nameof (FSharpAppHasAndroidDefine))) {
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
}
}

[Test]
public void BuildApplicationAndClean ([Values (false, true)] bool isRelease)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,12 @@ because xbuild doesn't support framework reference assemblies.

<!-- Get the defined constants for this API Level -->
<GetAndroidDefineConstants AndroidApiLevel="$(_SupportedApiLevel)" ProductVersion="$(MonoAndroidVersion)">
<Output TaskParameter="AndroidDefineConstants" PropertyName="AndroidDefineConstants" />
<Output TaskParameter="AndroidDefineConstants" ItemName="AndroidDefineConstants" />
</GetAndroidDefineConstants>

<CreateProperty Value="$(DefineConstants);$(AndroidDefineConstants)">
<Output TaskParameter="Value" PropertyName="DefineConstants" />
</CreateProperty>
<PropertyGroup>
<DefineConstants>$(DefineConstants);@(AndroidDefineConstants)</DefineConstants>
</PropertyGroup>
</Target>

<Target Name="AndroidPrepareForBuild" DependsOnTargets="$(_OnResolveMonoAndroidSdks)" />
Expand Down