Skip to content

Commit 064301b

Browse files
[One .NET] AOT support
Helpful reading: * https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/tasks/AotCompilerTask/MonoAOTCompiler.cs * https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/mono/netcore/nuget/Microsoft.NET.Runtime.MonoAOTCompiler.Task/README.md To make this work, I moved the existing `<Aot/>` MSBuild task calls to a new `_AndroidAot` MSBuild target in `Xamarin.Android.Legacy.targets`. In the .NET 6 targets, there is a *different* `_AndroidAot` MSBuild target that runs the new `<MonoAOTCompiler/>` MSBuild task. The `_AndroidAot` target runs per `$(RuntimeIdentifier)` after the linker completes. Native libraries are added to the `@(ResolvedFileToPublish)` item group to be included in the final `.apk`. To follow convention with Blazor WASM: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-4/#blazor-webassembly-ahead-of-time-aot-compilation The `$(RunAOTCompilation)` MSBuild property enables AOT. To help with backwards compatibility with "legacy" Xamarin.Android, I kept the `$(AotAssemblies)` MSBuild property intact.
1 parent b529d52 commit 064301b

File tree

12 files changed

+459
-274
lines changed

12 files changed

+459
-274
lines changed

Documentation/guides/OneDotNet.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ It is recommended to migrate to the new linker settings, as
190190
[linker]: https://docs.microsoft.com/dotnet/core/deploying/trimming-options
191191
[linker-full]: https://docs.microsoft.com/dotnet/core/deploying/trimming-options#trimmed-assemblies
192192

193+
## AOT
194+
195+
`$(RunAOTCompilation)` will be the new MSBuild property for enabling
196+
AOT. This is the same property used for [Blazor WASM][blazor].
197+
`$(AotAssemblies)` will also enable AOT, in order to help with
198+
migration from "legacy" Xamarin.Android to .NET 6.
199+
200+
It is recommended to migrate to the new `$(RunAOTCompilation)`
201+
property, as `$(AotAssemblies)` will eventually be deprecated.
202+
203+
[blazor]: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-4/#blazor-webassembly-ahead-of-time-aot-compilation
204+
193205
## dotnet cli
194206

195207
There are currently a few "verbs" we are aiming to get working in

build-tools/automation/azure-pipelines.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ variables:
4949
# - This is a non-fork branch with name containing "mono-" (for Mono bumps)
5050
IsMonoBranch: $[and(ne(variables['System.PullRequest.IsFork'], 'True'), or(contains(variables['Build.SourceBranchName'], 'mono-'), contains(variables['System.PullRequest.SourceBranch'], 'mono-')))]
5151
RunAllTests: $[or(eq(variables['XA.RunAllTests'], true), eq(variables['IsMonoBranch'], true))]
52-
DotNetNUnitCategories: '& TestCategory != DotNetIgnore & TestCategory != AOT & TestCategory != MkBundle & TestCategory != MonoSymbolicate & TestCategory != PackagesConfig & TestCategory != StaticProject & TestCategory != Debugger & TestCategory != SystemApplication'
52+
DotNetNUnitCategories: '& TestCategory != DotNetIgnore & TestCategory != MkBundle & TestCategory != MonoSymbolicate & TestCategory != PackagesConfig & TestCategory != StaticProject & TestCategory != Debugger & TestCategory != SystemApplication'
5353
NUnit.NumberOfTestWorkers: 4
5454
GitHub.Token: $(github--pat--vs-mobiletools-engineering-service2)
5555
CONVERT_JAVADOC_TO_XMLDOC: $[ne(variables['Build.DefinitionName'], 'Xamarin.Android-PR')]
@@ -811,6 +811,17 @@ stages:
811811
artifactFolder: net6-Interpreter
812812
useDotNet: true
813813

814+
- template: yaml-templates/apk-instrumentation.yaml
815+
parameters:
816+
configuration: $(XA.Build.Configuration)
817+
testName: Mono.Android.NET_Tests-Aot
818+
project: tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj
819+
testResultsFiles: TestResult-Mono.Android.NET_Tests-$(XA.Build.Configuration)-Aot.xml
820+
extraBuildArgs: /p:RunAOTCompilation=true
821+
artifactSource: bin/Test$(XA.Build.Configuration)/net6.0-android/Mono.Android.NET_Tests-Signed.apk
822+
artifactFolder: net6-aot
823+
useDotNet: true
824+
814825
- task: MSBuild@1
815826
displayName: shut down emulator
816827
inputs:

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.After.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This file is imported *after* the Microsoft.NET.Sdk/Sdk.targets.
1818
<Import Project="..\tools\Xamarin.Android.Bindings.Core.targets" />
1919
<Import Project="..\tools\Xamarin.Android.Bindings.ClassParse.targets" />
2020
<Import Project="Microsoft.Android.Sdk.AndroidLibraries.targets" />
21+
<Import Project="Microsoft.Android.Sdk.Aot.targets" Condition=" '$(AndroidApplication)' == 'true' " />
2122
<Import Project="Microsoft.Android.Sdk.Application.targets" Condition=" '$(AndroidApplication)' == 'true' " />
2223
<Import Project="Microsoft.Android.Sdk.AssemblyResolution.targets" />
2324
<Import Project="Microsoft.Android.Sdk.ILLink.targets" />
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!--
2+
***********************************************************************************************
3+
Microsoft.Android.Sdk.Aot.targets
4+
5+
.NET 6 AOT support. You can find "legacy" Xamarin.Android AOT support
6+
in Xamarin.Android.Legacy.targets.
7+
8+
For <MonoAOTCompiler/> usage, see:
9+
* https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/tasks/AotCompilerTask/MonoAOTCompiler.cs
10+
* https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/mono/netcore/nuget/Microsoft.NET.Runtime.MonoAOTCompiler.Task/README.md
11+
12+
These targets are running within the _ComputeFilesToPublishForRuntimeIdentifiers target.
13+
They run in a context of an inner build with a single $(RuntimeIdentifier).
14+
15+
***********************************************************************************************
16+
-->
17+
<Project>
18+
19+
<UsingTask TaskName="Xamarin.Android.Tasks.GetAotArguments" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
20+
21+
<Target Name="_AndroidAotInputs">
22+
<ItemGroup>
23+
<_AndroidAotInputs Include="@(ResolvedFileToPublish)" Condition=" '%(Extension)' == '.dll' " />
24+
</ItemGroup>
25+
</Target>
26+
27+
<Target Name="_AndroidAot"
28+
Condition=" '$(AotAssemblies)' == 'true' and '$(RuntimeIdentifier)' != '' "
29+
DependsOnTargets="_AndroidAotInputs"
30+
Inputs="@(_AndroidAotInputs)"
31+
Outputs="$(_AndroidStampDirectory)_AndroidAot.stamp">
32+
<GetAotArguments
33+
AndroidAotMode="$(AndroidAotMode)"
34+
AndroidNdkDirectory="$(_AndroidNdkDirectory)"
35+
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
36+
AndroidApiLevel="$(_AndroidApiLevel)"
37+
ManifestFile="$(_OuterIntermediateOutputPath)android\AndroidManifest.xml"
38+
AndroidSequencePointsMode="$(_SequencePointsMode)"
39+
AotAdditionalArguments="$(AndroidAotAdditionalArguments)"
40+
AotOutputDirectory="$(_AndroidAotBinDirectory)"
41+
RuntimeIdentifier="$(RuntimeIdentifier)"
42+
EnableLLVM="$(EnableLLVM)"
43+
Profiles="@(_AotProfiles)">
44+
<Output PropertyName="_AotArguments" TaskParameter="Arguments" />
45+
</GetAotArguments>
46+
<ItemGroup>
47+
<_MonoAOTAssemblies Include="@(_AndroidAotInputs->'%(FullPath)')" AotArguments="$(_AotArguments)" />
48+
</ItemGroup>
49+
<MakeDir Directories="$(IntermediateOutputPath)aot\" />
50+
<MonoAOTCompiler
51+
Assemblies="@(_MonoAOTAssemblies)"
52+
CompilerBinaryPath="@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier', '$(RuntimeIdentifier)'))"
53+
DisableParallelAot="$(_DisableParallelAot)"
54+
LibraryFormat="So"
55+
LLVMPath="$(_LLVMPath)"
56+
Mode="$(AndroidAotMode)"
57+
OutputDir="$(IntermediateOutputPath)aot\"
58+
OutputType="Library"
59+
UseAotDataFile="false"
60+
UseLLVM="$(EnableLLVM)">
61+
<Output TaskParameter="CompiledAssemblies" ItemName="_AotCompiledAssemblies" />
62+
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
63+
</MonoAOTCompiler>
64+
<Touch Files="$(_AndroidStampDirectory)_AndroidAot.stamp" AlwaysCreate="true" />
65+
<ItemGroup>
66+
<ResolvedFileToPublish
67+
Include="@(_AotCompiledAssemblies->'%(LibraryFile)')"
68+
ArchiveFileName="libaot-$([System.IO.Path]::GetFileName('%(_AotCompiledAssemblies.LibraryFile)'))"
69+
/>
70+
</ItemGroup>
71+
</Target>
72+
</Project>

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ _ResolveAssemblies MSBuild target.
3131
</PropertyGroup>
3232

3333
<Target Name="_ComputeFilesToPublishForRuntimeIdentifiers"
34-
DependsOnTargets="_FixupIntermediateAssembly;ResolveReferences;ComputeFilesToPublish"
34+
DependsOnTargets="_FixupIntermediateAssembly;ResolveReferences;ComputeFilesToPublish;_AndroidAot"
3535
Returns="@(ResolvedFileToPublish)">
3636
<ItemGroup>
3737
<ResolvedFileToPublish

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
<AndroidManifest Condition=" '$(AndroidManifest)' == '' and Exists ('Properties\AndroidManifest.xml') and !Exists ('AndroidManifest.xml') ">Properties\AndroidManifest.xml</AndroidManifest>
6363
<AndroidManifest Condition=" '$(AndroidManifest)' == '' ">AndroidManifest.xml</AndroidManifest>
6464
<GenerateApplicationManifest Condition=" '$(GenerateApplicationManifest)' == '' ">true</GenerateApplicationManifest>
65+
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == 'true' ">true</RunAOTCompilation>
66+
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' ">false</RunAOTCompilation>
67+
<AotAssemblies>$(RunAOTCompilation)</AotAssemblies>
6568

6669
<!--
6770
Runtime libraries feature switches defaults

0 commit comments

Comments
 (0)