From 9eb7649f5f9f5412497c914190f3576eff6dda8a Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Mon, 16 Dec 2024 17:01:29 -0600 Subject: [PATCH] [Mono.Android] introduce `#if NATIVEAOT` for `monodroid_get_log_categories()` In a NativeAOT context, we don't have `libmonodroid.so` *at all*. This change introduces a new `#if NATIVEAOT` block in `Logger.cs` to avoid calling `monodroid_get_log_categories()` and using `Default | Assembly` by default. I also reworked the build, so that the `$(AndroidRuntime)` property is defined in `Configuration.props`. Other projects can eventually use it as-needed. In a future PR, we could consider p/invoke into `__system_property_get()` and parse values. But we might want to reconsider the *names* of some of the system properties as they have `debug.mono.*` prefixes. Hardcoding a default value is at least *better*, so it won't crash. --- Configuration.props | 8 +++++--- .../create-packs/Microsoft.Android.Runtime.proj | 1 - build-tools/scripts/DotNet.targets | 14 +++++++++++--- src/Mono.Android/Android.Runtime/Logger.cs | 7 +++++++ src/Mono.Android/Mono.Android.csproj | 7 ++++--- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Configuration.props b/Configuration.props index ff1173c1d4b..db6017ebbc4 100644 --- a/Configuration.props +++ b/Configuration.props @@ -40,6 +40,7 @@ $(AndroidLatestStableFrameworkVersion) True $(AndroidLatestStableApiLevel) + Mono portable True latest @@ -60,7 +61,7 @@ False <_XABinRelativeInstallPrefix>lib\xamarin.android $(MSBuildThisFileDirectory)bin\$(Configuration)\$(_XABinRelativeInstallPrefix)\ - <_MonoAndroidNETOutputRoot>$(XAInstallPrefix)xbuild-frameworks\Microsoft.Android\ + <_MonoAndroidNETOutputRoot>$(XAInstallPrefix)xbuild-frameworks\Microsoft.Android.$(AndroidRuntime)\ <_MonoAndroidNETDefaultOutDir>$(_MonoAndroidNETOutputRoot)$(AndroidApiLevel)\ $(BuildOutputDirectory)lib\packs\Microsoft.Android.Ref.$(AndroidApiLevel)\$(AndroidPackVersion)\ref\$(DotNetTargetFramework)\ $(BuildOutputDirectory)lib\packs\$(MicrosoftAndroidSdkPackName)\$(AndroidPackVersion)\ @@ -214,7 +215,8 @@ - - + + + diff --git a/build-tools/create-packs/Microsoft.Android.Runtime.proj b/build-tools/create-packs/Microsoft.Android.Runtime.proj index 9c3c22c1390..96f7d691886 100644 --- a/build-tools/create-packs/Microsoft.Android.Runtime.proj +++ b/build-tools/create-packs/Microsoft.Android.Runtime.proj @@ -11,7 +11,6 @@ projects that use the Microsoft.Android framework in .NET 6+. android-arm64 - Mono Microsoft.Android.Runtime.$(AndroidRuntime).$(AndroidApiLevel).$(AndroidRID) Microsoft.Android runtime components for API $(AndroidApiLevel). Please do not reference directly. <_AndroidRuntimePackAssemblyPath>runtimes\$(AndroidRID)\lib\$(DotNetTargetFramework) diff --git a/build-tools/scripts/DotNet.targets b/build-tools/scripts/DotNet.targets index cda033d1108..1683919fd96 100644 --- a/build-tools/scripts/DotNet.targets +++ b/build-tools/scripts/DotNet.targets @@ -29,22 +29,30 @@ + + + + + DependsOnTargets="BuildExtraApiLevels;BuildOtherRuntimes"> diff --git a/src/Mono.Android/Android.Runtime/Logger.cs b/src/Mono.Android/Android.Runtime/Logger.cs index 29e13cd6302..9bf9aa8809e 100644 --- a/src/Mono.Android/Android.Runtime/Logger.cs +++ b/src/Mono.Android/Android.Runtime/Logger.cs @@ -58,12 +58,19 @@ public static void Log (LogLevel level, string appname, string? log) { } } + #if !NATIVEAOT [DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)] extern static uint monodroid_get_log_categories (); + #endif static Logger () { + #if NATIVEAOT + // TODO: p/invoke into __system_property_get + Categories = LogCategories.Default | LogCategories.Assembly; + #else // !NATIVEAOT Categories = (LogCategories) monodroid_get_log_categories (); + #endif // !NATIVEAOT } } } diff --git a/src/Mono.Android/Mono.Android.csproj b/src/Mono.Android/Mono.Android.csproj index 5b062533b27..2c0c4bd2bda 100644 --- a/src/Mono.Android/Mono.Android.csproj +++ b/src/Mono.Android/Mono.Android.csproj @@ -13,7 +13,8 @@ 0618;0809;0108;0114;0465;8609;8610;8614;8617;8613;8764;8765;8766;8767;RS0041 $(WarningsAsErrors);CS2002 true - $(DefineConstants);JAVA_INTEROP + $(DefineConstants);JAVA_INTEROP;MONO + $(DefineConstants);JAVA_INTEROP;NATIVEAOT $(BaseIntermediateOutputPath)$(Configuration)\$(TargetFramework)\android-$(AndroidPlatformId)\ false CoreBuild @@ -410,7 +411,7 @@ <_RefExtras Include="$(OutputPath)*.*" Exclude="$(OutputPath)*.dll" /> <_SourceFiles Include="$(OutputPath)Mono.Android.*" /> <_SourceFiles Include="$(OutputPath)Java.Interop.*" /> - <_RuntimePackFiles Include="@(_SourceFiles)" AndroidRID="%(AndroidAbiAndRuntimeFlavor.AndroidRID)" AndroidRuntime="%(AndroidAbiAndRuntimeFlavor.AndroidRuntime)" /> + <_RuntimePackFiles Include="@(_SourceFiles)" AndroidRID="%(AndroidAbiAndRuntimeFlavor.AndroidRID)" />