Skip to content

Commit

Permalink
Exclude assemblies from NativeAOT app bundles (#18532)
Browse files Browse the repository at this point in the history
Since NativeAOT generates native libraries and executables that do not
rely on assemblies they were compiled from, all managed assemblies can
be excluded from the application bundle.

This reduces the size of the application bundle by `3,17Mb` (or ~19%
compared to the baseline)

| MAUI ios app | Base | This PR | diff (%) |
|--------------|-----------|-----------|----------|
| SOD (Mb)     | 50,13 | 41,93 | -16,3%      |
| .ipa (Mb)    | 16,59  | 13,43  | -19%      |

Fixes: #18472
  • Loading branch information
ivanpovazan committed Jul 6, 2023
1 parent 7ed8c08 commit bcb8e5c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
11 changes: 11 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,17 @@
</ItemGroup>
</Target>

<Target Name="_ExcludeUnusedFilesFromTheBundle"
Condition="'$(_UseNativeAot)' == 'true'"
AfterTargets="_LinkNativeExecutable"
BeforeTargets="CopyFilesToPublishDirectory">
<ItemGroup>
<!-- We don't ship assemblies with NativeAOT -->
<!-- Exclude assemblies explicitly until https://github.com/dotnet/runtime/issues/87187 is not resolved -->
<ResolvedFileToPublish Remove="@(ResolvedFileToPublish)" Condition="'%(ResolvedFileToPublish.Extension)' == '.dll'" />
</ItemGroup>
</Target>

<PropertyGroup>
<_GenerateBindingsDependsOn>
_ComputeBindingVariables;
Expand Down
2 changes: 2 additions & 0 deletions runtime/coreclr-bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,11 @@
void
xamarin_bridge_register_product_assembly (GCHandle* exception_gchandle)
{
#if !defined (NATIVEAOT)
MonoAssembly *assembly;
assembly = xamarin_open_and_register (PRODUCT_DUAL_ASSEMBLY, exception_gchandle);
xamarin_mono_object_release (&assembly);
#endif // !defined (NATIVEAOT)
}

MonoMethod *
Expand Down
7 changes: 5 additions & 2 deletions runtime/monotouch-main.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ - (void) memoryWarning: (NSNotification *) sender
if (xamarin_register_assemblies != NULL)
xamarin_register_assemblies ();

#if !defined (NATIVEAOT)
if (xamarin_executable_name) {
assembly = xamarin_open_and_register (xamarin_executable_name, &exception_gchandle);
xamarin_process_fatal_exception_gchandle (exception_gchandle, "An exception occurred while opening the main executable");
Expand All @@ -454,14 +455,16 @@ - (void) memoryWarning: (NSNotification *) sender
xamarin_process_fatal_exception_gchandle (exception_gchandle, "An exception occurred while opening an assembly");
}

#if SUPPORTS_DYNAMIC_REGISTRATION
if (xamarin_supports_dynamic_registration) {
MonoReflectionAssembly *rassembly = mono_assembly_get_object (mono_domain_get (), assembly);
xamarin_register_entry_assembly (rassembly, &exception_gchandle);
xamarin_mono_object_release (&rassembly);
xamarin_process_fatal_exception_gchandle (exception_gchandle, "An exception occurred while opening the entry assembly");
}
#endif // SUPPORTS_DYNAMIC_REGISTRATION
#else
assembly = NULL;
(void)exception_gchandle;
#endif // !defined (NATIVEAOT)

DEBUG_LAUNCH_TIME_PRINT ("\tAssembly register time");

Expand Down

6 comments on commit bcb8e5c

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.