Skip to content

Commit 4a71aa6

Browse files
[One .NET] Enable Profiled AOT by default for Release builds (#6547)
To improve startup performance for `Release` builds on .NET 6+: 1. Default `$(RunAOTCompilation)` to `true` 2. Default `$(AndroidEnableProfiledAot)` to `true`. Going forward, you would have to explicitly turn these off if you want them to be off. (You *may* want them to be off if you care more about `.apk` size than startup perf, as Profiled AOT tends to add 10-20% to the `.apk` size, per ABI.) We also can drop the `android-aot` workload, and just include the [dotnet/runtime AOT packs][0] in the `android` workload. These packs are already inserted into Visual Studio, so we can rely on them. Since the new project options screen evaluates MSBuild, we don't appear to need to do anything on the UI side: > Code Generation & Runtime > > AOT | Enable Ahead-of-Time (AOT) Compilation > ⬛ Debug > ✅ Release > > Startup Tracing | Enable startup tracing > ⬛ Debug > ✅ Release [0]: https://github.com/dotnet/runtime/blob/c7adf5598f27e59644030ddae4e382cbda74e41e/src/mono/nuget/Microsoft.NET.Workload.Mono.Toolchain.Manifest/WorkloadManifest.json.in#L29-L40
1 parent 71e6ecd commit 4a71aa6

File tree

10 files changed

+39
-12
lines changed

10 files changed

+39
-12
lines changed

Documentation/guides/OneDotNet.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,28 @@ property, as `$(AotAssemblies)` will eventually be deprecated.
176176

177177
[blazor]: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-4/#blazor-webassembly-ahead-of-time-aot-compilation
178178

179+
We want to choose the optimal settings for startup time and app size.
180+
By default `Release` builds will default to:
181+
182+
```xml
183+
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
184+
<RunAOTCompilation>true</RunAOTCompilation>
185+
<AndroidEnableProfiledAot>true</AndroidEnableProfiledAot>
186+
</PropertyGroup>
187+
```
188+
This is the behavior when `$(RunAOTCompilation)` and
189+
`$(AndroidEnableProfiledAot)` are blank.
190+
191+
So if you would like to *disable* AOT, you would need to explicitly
192+
turn these settings off:
193+
194+
```xml
195+
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
196+
<RunAOTCompilation>false</RunAOTCompilation>
197+
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
198+
</PropertyGroup>
199+
```
200+
179201
## dotnet cli
180202

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

build-tools/create-packs/Directory.Build.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@
112112
<Output TaskParameter="Result" ItemName="_NuGetSources" />
113113
</XmlPeek>
114114

115-
<!-- dotnet workload install android-aot -->
115+
<!-- dotnet workload install android -->
116116
<PropertyGroup>
117117
<_TempDirectory>$(DotNetPreviewPath)..\.xa-workload-temp-$([System.IO.Path]::GetRandomFileName())</_TempDirectory>
118118
</PropertyGroup>
119119
<ItemGroup>
120120
<_NuGetSources Include="$(OutputPath.TrimEnd('\'))" />
121121
<_PreviewPacks Condition=" '$(AndroidLatestStableApiLevel)' != '$(AndroidLatestUnstableApiLevel)' " Include="$(XamarinAndroidSourcePath)bin\Build$(Configuration)\nuget-unsigned\Microsoft.Android.Ref.$(AndroidLatestUnstableApiLevel).*.nupkg" />
122-
<_InstallArguments Include="android-aot" />
122+
<_InstallArguments Include="android" />
123123
<_InstallArguments Include="android-$(AndroidLatestUnstableApiLevel)" Condition=" '@(_PreviewPacks->Count())' != '0' " />
124124
<_InstallArguments Include="--skip-manifest-update" />
125125
<_InstallArguments Include="--verbosity diag" />

build-tools/create-packs/vs-workload.in.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<Replacement>@PACK_VERSION_SHORT@</Replacement>
1111
</ShortNames>
1212
<ComponentResources Include="android" Version="@WORKLOAD_VERSION@" Category=".NET" Title=".NET SDK for Android" Description=".NET SDK Workload for building Android applications."/>
13-
<ComponentResources Include="android-aot" Version="@WORKLOAD_VERSION@" Category=".NET" Title=".NET SDK for Android with AOT" Description=".NET SDK Workload for building Android applications with AOT support."/>
1413
<WorkloadPackages Include="$(NuGetPackagePath)\Microsoft.NET.Sdk.Android.Manifest*.nupkg" Version="@WORKLOAD_VERSION@" />
1514
</ItemGroup>
1615
</Project>

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
@@ -71,9 +71,12 @@
7171
<AndroidManifest Condition=" '$(AndroidManifest)' == '' and Exists ('Properties\AndroidManifest.xml') and !Exists ('AndroidManifest.xml') ">Properties\AndroidManifest.xml</AndroidManifest>
7272
<AndroidManifest Condition=" '$(AndroidManifest)' == '' ">AndroidManifest.xml</AndroidManifest>
7373
<GenerateApplicationManifest Condition=" '$(GenerateApplicationManifest)' == '' ">true</GenerateApplicationManifest>
74+
<!-- Default to AOT in Release mode -->
75+
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == '' and '$(Configuration)' == 'Release' ">true</RunAOTCompilation>
7476
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == 'true' ">true</RunAOTCompilation>
7577
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' ">false</RunAOTCompilation>
7678
<AotAssemblies>$(RunAOTCompilation)</AotAssemblies>
79+
<AndroidEnableProfiledAot Condition=" '$(AndroidEnableProfiledAot)' == '' and '$(RunAOTCompilation)' == 'true' ">true</AndroidEnableProfiledAot>
7780

7881
<!--
7982
Runtime libraries feature switches defaults

src/Xamarin.Android.Build.Tasks/Microsoft.NET.Sdk.Android/WorkloadManifest.in.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@
1414
"Microsoft.Android.Templates"
1515
],
1616
"platforms": [ "win-x64", "linux-x64", "osx-x64", "osx-arm64" ],
17-
"extends" : [ "microsoft-net-runtime-android" ]
18-
},
19-
"android-aot": {
20-
"description": ".NET SDK Workload for building Android applications with AOT support.",
21-
"platforms": [ "win-x64", "linux-x64", "osx-x64", "osx-arm64" ],
22-
"extends" : [ "android", "microsoft-net-runtime-android-aot" ]
17+
"extends" : [ "microsoft-net-runtime-android", "microsoft-net-runtime-android-aot" ]
2318
}
2419
},
2520
"packs": {

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public void SmokeTestBuildWithSpecialCharacters ([Values (false, true)] bool for
132132
new XamarinAndroidApplicationProject ();
133133
proj.ProjectName = testName;
134134
proj.IsRelease = true;
135+
// TODO: AOT fails https://github.com/xamarin/xamarin-android/issues/6685
136+
// .NET 6 uses AOT by default for Release
137+
proj.AotAssemblies = false;
135138
if (forms) {
136139
proj.PackageReferences.Clear ();
137140
proj.PackageReferences.Add (KnownPackages.XamarinForms_4_7_0_1142);

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public void BuildReleaseArm64 ([Values (false, true)] bool forms)
7575
new XamarinFormsAndroidApplicationProject () :
7676
new XamarinAndroidApplicationProject ();
7777
proj.IsRelease = true;
78+
proj.AotAssemblies = false; // Release defaults to Profiled AOT for .NET 6
7879
proj.SetAndroidSupportedAbis ("arm64-v8a");
7980
proj.SetProperty ("LinkerDumpDependencies", "True");
8081
proj.SetProperty ("AndroidUseAssemblyStore", "False");

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,7 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease, bool aot, bo
532532
};
533533
proj.MainActivity = proj.DefaultMainActivity.Replace (": Activity", ": AndroidX.AppCompat.App.AppCompatActivity");
534534
proj.SetProperty ("AndroidUseAssemblyStore", usesAssemblyStore.ToString ());
535-
if (aot) {
536-
proj.SetProperty ("RunAOTCompilation", "true");
537-
}
535+
proj.SetProperty ("RunAOTCompilation", aot.ToString ());
538536
proj.OtherBuildItems.Add (new AndroidItem.InputJar ("javaclasses.jar") {
539537
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
540538
});
@@ -634,6 +632,10 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease, bool aot, bo
634632
} else {
635633
helper.AssertContainsEntry ("assemblies/System.Private.CoreLib.dll", shouldContainEntry: expectEmbeddedAssembies);
636634
}
635+
if (aot) {
636+
helper.AssertContainsEntry ($"lib/{abi}/libaot-{proj.ProjectName}.dll.so");
637+
helper.AssertContainsEntry ($"lib/{abi}/libaot-System.Linq.dll.so");
638+
}
637639
}
638640
}
639641

tests/MSBuildDeviceIntegration/Tests/BundleToolTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public void OneTimeSetUp ()
6868
var bytes = new byte [1024];
6969
app = new XamarinFormsMapsApplicationProject {
7070
IsRelease = true,
71+
AotAssemblies = false, // Release defaults to Profiled AOT for .NET 6
7172
PackageName = "com.xamarin.bundletooltests",
7273
};
7374
app.OtherBuildItems.Add (new AndroidItem.AndroidAsset ("foo.bar") {

tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ public void RunWithInterpreterEnabled ([Values (false, true)] bool isRelease)
614614

615615
proj = new XamarinAndroidApplicationProject () {
616616
IsRelease = isRelease,
617+
AotAssemblies = false, // Release defaults to Profiled AOT for .NET 6
617618
};
618619
var abis = new string[] { "armeabi-v7a", "arm64-v8a", "x86", "x86_64" };
619620
proj.SetAndroidSupportedAbis (abis);

0 commit comments

Comments
 (0)