diff --git a/build-tools/create-packs/Microsoft.Android.Sdk.proj b/build-tools/create-packs/Microsoft.Android.Sdk.proj
index c89ddba191a..257e0c83892 100644
--- a/build-tools/create-packs/Microsoft.Android.Sdk.proj
+++ b/build-tools/create-packs/Microsoft.Android.Sdk.proj
@@ -76,6 +76,7 @@ core workload SDK packs imported by WorkloadManifest.targets.
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\Microsoft.Android.Sdk\Sdk\**" PackagePath="Sdk" />
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Microsoft.Android.Sdk.ILLink\PreserveLists\**" PackagePath="PreserveLists" />
<_PackageFiles Include="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\Microsoft.Android.Sdk\targets\**" PackagePath="targets" />
+ <_PackageFiles Include="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\dotnet.aotprofile" PackagePath="targets" />
<_PackageFiles Include="$(IntermediateOutputPath)UnixFilePermissions.xml" PackagePath="data" Condition=" '$(HostOS)' != 'Windows' " />
diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets
index e6ec675b479..918b5a26aaa 100644
--- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets
+++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets
@@ -69,11 +69,13 @@ They run in a context of an inner build with a single $(RuntimeIdentifier).
TempDirectory="$([MSBuild]::EnsureTrailingSlash($(_AotOutputDirectory)))%(FileName)"
AotArguments="$(_AotArguments),temp-path=$([System.IO.Path]::GetFullPath(%(_MonoAOTAssemblies.TempDirectory)))"
/>
+
+ DependsOnTargets="_ResolveSdks;_ResolveMonoAndroidSdks">
-
+
diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
index f0b2f664ec3..61d9b1ef053 100644
--- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
+++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
@@ -727,6 +727,11 @@ because xbuild doesn't support framework reference assemblies.
/>
+
+
+ $(MonoAndroidBinDirectory)
+
+
diff --git a/src/Xamarin.Android.Build.Tasks/dotnet.aotprofile b/src/Xamarin.Android.Build.Tasks/dotnet.aotprofile
new file mode 100644
index 00000000000..3ebd741ae4d
Binary files /dev/null and b/src/Xamarin.Android.Build.Tasks/dotnet.aotprofile differ
diff --git a/src/monodroid/jni/monodroid-glue.cc b/src/monodroid/jni/monodroid-glue.cc
index cfcc43c5533..ceed687efad 100644
--- a/src/monodroid/jni/monodroid-glue.cc
+++ b/src/monodroid/jni/monodroid-glue.cc
@@ -1582,8 +1582,52 @@ MonodroidRuntime::set_profile_options ()
value.assign (prop_value);
}
- // setenv(3) makes copies of its arguments
- setenv ("DOTNET_DiagnosticPorts", value.get (), 1);
+ // NET6+ supports only the AOT Mono profiler, if the prefix is absent or different than 'aot:' we consider the
+ // property to contain value for the dotnet tracing profiler.
+ constexpr char AOT_PREFIX[] = "aot:";
+ if (!value.starts_with (AOT_PREFIX)) {
+ // setenv(3) makes copies of its arguments
+ setenv ("DOTNET_DiagnosticPorts", value.get (), 1);
+ return;
+ }
+
+ constexpr char OUTPUT_ARG[] = "output=";
+ constexpr size_t OUTPUT_ARG_LEN = sizeof(OUTPUT_ARG) - 1;
+ constexpr size_t start_index = sizeof(AOT_PREFIX); // one char past ':'
+
+ dynamic_local_string output_path;
+ bool have_output_arg = false;
+ string_segment param;
+
+ while (value.next_token (start_index, ',', param)) {
+ dynamic_local_string temp;
+ temp.assign (param.start (), param.length ());
+ if (!param.starts_with (OUTPUT_ARG)) {
+ continue;
+ }
+
+ output_path.assign (param.start () + OUTPUT_ARG_LEN, param.length () - OUTPUT_ARG_LEN);
+ have_output_arg = true;
+ break;
+ }
+
+ if (!have_output_arg) {
+ constexpr char PROFILE_FILE_NAME_PREFIX[] = "profile.";
+ constexpr char AOT_EXT[] = "aotprofile";
+
+ output_path
+ .assign_c (androidSystem.get_override_dir (0))
+ .append (MONODROID_PATH_SEPARATOR)
+ .append (PROFILE_FILE_NAME_PREFIX)
+ .append (AOT_EXT);
+
+ value
+ .append (OUTPUT_ARG)
+ .append (output_path.get (), output_path.length ());
+ }
+
+ log_warn (LOG_DEFAULT, "Initializing profiler with options: %s", value.get ());
+ debug.monodroid_profiler_load (androidSystem.get_runtime_libdir (), value.get (), output_path.get ());
}
#else // def NET6
inline void