Skip to content

Commit

Permalink
[Microsoft.Android.Sdk] use net5.0-android $(TargetFramework)
Browse files Browse the repository at this point in the history
After bumping to .NET 5.0.100-rc.1.20426.3, we can now use:

    <TargetFramework>net5.0-android</TargetFramework>

On the first try, this failed with:

    Microsoft.NET.TargetFrameworkInference.targets(113,5): error NETSDK1139: The target platform identifier android was not recognized.

Our SDK needs to specify:

    <TargetPlatformSupported Condition=" '$(TargetPlatformIdentifier)' == 'Android' ">true</TargetPlatformSupported>

After doing this, everything seemed to work as before.

I updated tests so they will use `net5.0-android` going forward.

~~ @(SupportedTargetPlatform) ~~

A second part of this change is the need to fill out
`$(TargetPlatformVersion)`, `@(AndroidSupportedTargetPlatform)`, and
`@(SupportedTargetPlatform)` similar to:

https://github.com/dotnet/sdk/blob/18ee4eac8b3abe6d554d2e0c39d8952da0f23ce5/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.WindowsSupportedTargetPlatforms.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`.

~~ BuildOutsideVisualStudio ~~

The only other change is here is to ignore the
`BuildOutsideVisualStudio` test that triggers an MSBuild bug in .NET 5
rc1. The build log reports `BUILD FAILED 0 Error(s)`, however...

This is fixed in dotnet/msbuild#5703, which
will tentatively land in .NET 5 rc2.

We can workaround this by setting `$MSBuildDoNotTruncateConditions=1`.
I set this environment variable when launching `dotnet build` in our
MSBuild tests.
  • Loading branch information
jonathanpeppers committed Sep 4, 2020
1 parent 7079083 commit 321e5ee
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 8 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 2 additions & 0 deletions Configuration.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<!-- Should correspond to the first value from `$(API_LEVELS)` in `build-tools/api-xml-adjuster/Makefile` -->
<AndroidFirstFrameworkVersion Condition="'$(AndroidFirstFrameworkVersion)' == ''">v4.4</AndroidFirstFrameworkVersion>
<AndroidFirstApiLevel Condition="'$(AndroidFirstApiLevel)' == ''">19</AndroidFirstApiLevel>
<!-- The min API level supported by Microsoft.Android.Sdk, should refactor/remove when this value is the same as $(AndroidFirstApiLevel) -->
<AndroidMinimumDotNetApiLevel Condition="'$(AndroidMinimumDotNetApiLevel)' == ''">21</AndroidMinimumDotNetApiLevel>
<AndroidFirstPlatformId Condition="'$(AndroidFirstPlatformId)' == ''">$(AndroidFirstApiLevel)</AndroidFirstPlatformId>
<_IsRunningNuGetRestore Condition="$(RestoreTaskAssemblyFile.EndsWith('NuGet.exe', StringComparison.InvariantCultureIgnoreCase))">True</_IsRunningNuGetRestore>
<!-- *Latest* *stable* API level binding that we support; used when building src/Xamarin.Android.Build.Tasks -->
Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides/OneDotNet.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ The following instructions can be used for early preview testing.
```xml
<Project Sdk="Microsoft.Android.Sdk/10.0.100">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net5.0-android</TargetFramework>
<RuntimeIdentifier>android.21-arm64</RuntimeIdentifier>
<OutputType>Exe</OutputType>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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
/// </summary>
public class GenerateSupportedPlatforms : Task
{
/// <summary>
/// A list of AndroidApiInfo.xml files produced by Mono.Android.targets
/// </summary>
[Required]
public string [] AndroidApiInfo { get; set; }

/// <summary>
/// The output file to generate
/// </summary>
[Required]
public string OutputFile { get; set; }

/// <summary>
/// $(AndroidMinimumDotNetApiLevel) from Configuration.props
/// </summary>
[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 (@"
***********************************************************************************************
Microsoft.Android.Sdk.SupportedPlatforms.props
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 (); // </TargetPlatformVersion>
writer.WriteEndElement (); // </PropertyGroup>

writer.WriteStartElement ("ItemGroup");
foreach (AndroidVersion version in versions.InstalledBindingVersions
.Where (v => v.ApiLevel >= MinimumApiLevel)
.OrderBy (v => v.ApiLevel)) {
writer.WriteStartElement ("AndroidSupportedTargetPlatform");
writer.WriteAttributeString ("Include", version.ApiLevel.ToString ());
writer.WriteEndElement (); // </AndroidSupportedTargetPlatform>
}
writer.WriteStartElement ("SupportedTargetPlatform");
writer.WriteAttributeString ("Condition", " '$(TargetPlatformIdentifier)' == 'Android' ");
writer.WriteAttributeString ("Include", "@(AndroidSupportedTargetPlatform)");

writer.WriteEndDocument (); // </Project>
}

return !Log.HasLoggedErrors;
}
}
}
2 changes: 1 addition & 1 deletion build-tools/automation/azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.20426.3
HostedMacMojave: Hosted Mac Internal Mojave
HostedMac: Hosted Mac Internal
HostedWinVS2019: Hosted Windows 2019 with VS2019
Expand Down
2 changes: 2 additions & 0 deletions src/Xamarin.Android.Build.Tasks/.gitignore
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
</PropertyGroup>

<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
<PropertyGroup>
<TargetPlatformSupported Condition=" '$(TargetPlatformIdentifier)' == 'Android' ">true</TargetPlatformSupported>
</PropertyGroup>

<!-- Default item includes (globs and implicit references) -->
<Import Project="Microsoft.Android.Sdk.DefaultItems.targets" />
<Import Project="Microsoft.Android.Sdk.SupportedPlatforms.props" />
<!-- Build ordering, should be imported before Xamarin.Android.Common.targets -->
<Import Project="$(MSBuildThisFileDirectory)Microsoft.Android.Sdk.BuildOrder.targets" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ protected bool BuildInternal (string projectOrSolution, string target, string []
}
}

// TODO: workaround until: https://github.com/dotnet/msbuild/pull/5703
if (UseDotNet) {
psi.EnvironmentVariables ["MSBuildDoNotTruncateConditions"] = "1";
}

//NOTE: commit messages can "accidentally" cause test failures
// Consider if you added an error message in a commit message, then wrote a test asserting the error no longer occurs.
// Both Jenkins and VSTS have an environment variable containing the full commit message, which will inexplicably cause your test to fail...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protected bool Execute (params string [] args)
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// TODO: workaround until: https://github.com/dotnet/msbuild/pull/5703
p.StartInfo.EnvironmentVariables ["MSBuildDoNotTruncateConditions"] = "1";

p.ErrorDataReceived += (sender, e) => {
if (e.Data != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask AssemblyFile="$(BootstrapTasksAssembly)" TaskName="Xamarin.Android.Tools.BootstrapTasks.GenerateProfile" />
<UsingTask AssemblyFile="$(BootstrapTasksAssembly)" TaskName="Xamarin.Android.Tools.BootstrapTasks.GenerateSupportedPlatforms" />
<UsingTask AssemblyFile="$(BootstrapTasksAssembly)" TaskName="Xamarin.Android.Tools.BootstrapTasks.UnzipDirectoryChildren" />
<UsingTask AssemblyFile="$(PrepTasksAssembly)" TaskName="Xamarin.Android.BuildTools.PrepTasks.ReplaceFileContents" />
<UsingTask AssemblyFile="$(PrepTasksAssembly)" TaskName="Xamarin.Android.BuildTools.PrepTasks.Which" />
Expand All @@ -10,6 +11,10 @@
<PropertyGroup>
<_SharedRuntimeBuildPath Condition=" '$(_SharedRuntimeBuildPath)' == '' ">$(XAInstallPrefix)xbuild-frameworks\MonoAndroid\</_SharedRuntimeBuildPath>
<_GeneratedProfileClass>$(MSBuildThisFileDirectory)$(IntermediateOutputPath)Profile.g.cs</_GeneratedProfileClass>
<ResolveReferencesDependsOn>
$(ResolveReferencesDependsOn);
_GenerateSupportedPlatforms;
</ResolveReferencesDependsOn>
<BuildDependsOn>
_CopyNDKTools;
_GenerateXACommonProps;
Expand Down Expand Up @@ -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)">
</ReplaceFileContents>
</Target>
<Target Name="_FindAndroidApiInfo">
<ItemGroup>
<_AndroidApiInfo Include="$(XAInstallPrefix)xbuild-frameworks\MonoAndroid\*\AndroidApiInfo.xml" />
</ItemGroup>
</Target>
<Target Name="_GenerateSupportedPlatforms"
DependsOnTargets="_FindAndroidApiInfo"
Inputs="$(BootstrapTasksAssembly);$(MSBuildThisFile);@(_AndroidApiInfo)"
Outputs="Microsoft.Android.Sdk\targets\Microsoft.Android.Sdk.SupportedPlatforms.props">
<GenerateSupportedPlatforms
AndroidApiInfo="@(_AndroidApiInfo)"
MinimumApiLevel="$(AndroidMinimumDotNetApiLevel)"
OutputFile="Microsoft.Android.Sdk\targets\Microsoft.Android.Sdk.SupportedPlatforms.props"
/>
</Target>
<Target Name="_GenerateProfileClass"
BeforeTargets="CoreCompile"
Inputs="@(_SharedRuntimeAssemblies)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.Android.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net5.0-android</TargetFramework>
<RootNamespace>Xamarin.Android.Tests.CodeBehindBuildTests</RootNamespace>
<OutputType>Exe</OutputType>
<AndroidGenerateLayoutBindings>True</AndroidGenerateLayoutBindings>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.Android.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net5.0-android</TargetFramework>
<RootNamespace>CommonSampleLibrary</RootNamespace>
<AssemblyName>CommonSampleLibrary</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down

0 comments on commit 321e5ee

Please sign in to comment.