maui icon indicating copy to clipboard operation
maui copied to clipboard

MAUI App bundle size is too large on release mode (second attempt)

Open voroninp opened this issue 1 year ago • 5 comments

Description

I have exactly the same problem described in this issue.

Whatever I do, I cannot decrease the app size below 18Mb.

Steps to Reproduce

I've created a default MAUI project, removed all the platforms except android. I also removed AnyCPU target from configuration. I am publishing the app for ARM64 architecture with enabled trimming and disabled AOT.

I see that the published package still contains libraries targeting x86 and x64, and other architectures. When I create separate package per abi, the size of the package drops to 11Mb, but assemblies folder contains assemblies targeting other platforms. image

Is it an expected behavior?

Link to public reproduction project repository

https://github.com/voroninp/MauiTest

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

voroninp avatar Dec 18 '23 10:12 voroninp

@jonathanpeppers thoughts?

PureWeen avatar Dec 18 '23 15:12 PureWeen

Verified this issue with Visual Studio 17.10.0 Preview 3 (8.0.3).I can not repro this issue. image

ninachen03 avatar Apr 17 '24 06:04 ninachen03

@ninachen03 , do I get you right that 17Mb is expected size of the app?

voroninp avatar Apr 17 '24 07:04 voroninp

@ninachen03

dotnet workload list:

Installed Workload Id      Manifest Version       Installation Source                                            
-----------------------------------------------------------------------------------------------------------------
android                    34.0.95/8.0.100        SDK 8.0.300-preview.24203, VS 17.10.34814.14, VS 17.9.34728.123
ios                        17.2.8053/8.0.100      SDK 8.0.300-preview.24203, VS 17.10.34814.14, VS 17.9.34728.123
maccatalyst                17.2.8053/8.0.100      SDK 8.0.300-preview.24203, VS 17.10.34814.14, VS 17.9.34728.123
maui-windows               8.0.7/8.0.100          SDK 8.0.300-preview.24203, VS 17.10.34814.14, VS 17.9.34728.123
wasm-tools                 8.0.4/8.0.100          SDK 8.0.300-preview.24203, VS 17.10.34814.14, VS 17.9.34728.123

Microsoft Visual Studio Community 2022 (64-bit) - Preview Version 17.10.0 Preview 4.0

csproj:

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFrameworks>net8.0-android34.0</TargetFrameworks>
		<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
		<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->

		<!-- Note for MacCatalyst:
		The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
		When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
		The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
		either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
		<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->

		<OutputType>Exe</OutputType>
		<RootNamespace>MauiAndroidAOT</RootNamespace>
		<UseMaui>true</UseMaui>
		<SingleProject>true</SingleProject>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>

		<!-- Display name -->
		<ApplicationTitle>MauiAndroidAOT</ApplicationTitle>

		<!-- App Identifier -->
		<ApplicationId>com.companyname.mauiandroidaot</ApplicationId>

		<!-- Versions -->
		<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
		<ApplicationVersion>1</ApplicationVersion>

		<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
		<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
		<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
		<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
		<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
		<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
		<Platforms>AnyCPU;ARM64</Platforms>
	</PropertyGroup>

	<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-android34.0|ARM64'">
	  <AndroidLinkTool>r8</AndroidLinkTool>
	  <TrimMode>link</TrimMode>
	</PropertyGroup>

	<ItemGroup>
		<!-- App Icon -->
		<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

		<!-- Splash Screen -->
		<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

		<!-- Images -->
		<MauiImage Include="Resources\Images\*" />
		<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

		<!-- Custom Fonts -->
		<MauiFont Include="Resources\Fonts\*" />

		<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
		<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
	</ItemGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
		<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
		<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
	</ItemGroup>
</Project>

When I publish, it's 25Mb+

voroninp avatar Apr 22 '24 12:04 voroninp

Verified this issue with Visual Studio 17.10 Preview 6 (8.0.21). Can repro it. image

image

ninachen03 avatar May 13 '24 06:05 ninachen03

@voroninp: are you uploading your app to a "store" such as the Google Play store? Or are you instead having users download your app from a web site?

If you're uploading to the Google Play Store, then you should be uploading .aab files, not .apk files:

From August 2021, new apps are required to publish with the Android App Bundle on Google Play.

The benefit to App Bundles (.aab files) is that when a customer downloads your app from the Google Play Store, the .apk that is downloaded will not contain certain artifacts that won't be used by that device. For example, if the .aab contains both arm64 and x64 native libraries and the target device only runs arm64 binaries, then the x64 native libraries will not be sent to the target device.

Your screenshots omit the lib directory structure; there should also be several lib/arm64-v8a/lib*.so files and lib/x64/lib*.so files and…. All of which contribute to the .aab size and to a "local universal" .apk, but not to the .apk downloaded by your app's users from the Google Play Store.

You can explicitly control which runtimes are included in the .aab by setting the $(RuntimeIdentifiers) MSBuild property:

dotnet build -c Release -f net8.0-android -p:RuntimeIdentifiers=android-arm64

will produce a .aab file which contains only arm64-v8a runtime files, not x86/x64/etc. This will also cause there to only be assemblies/assemblies.blob and assemblies/assemblies.arm64_v8a.blob files, and not assemblies/assemblies.x86_64.blob/etc. files

.NET for Android in .NET 9 will be further altering how assemblies are packaged, removing the assemblies*.blob files entirely…and placing the data that was previously within them into additional lib/ABI/libassemblies.ABI.blob.so files. This will in turn allow App Store .apk downloads to be smaller -- no assemblies.x86_64.blob anymore for arm64 devices to download! -- though .aab uploads will be larger (no CPU-agnostic assemblies.blob file, so more data is duplicated across ABIs). See also:

  • https://github.com/xamarin/xamarin-android/issues/8168
  • https://github.com/xamarin/xamarin-android/commit/86260ed36dfe1a90c8ed6a2bb1cd0607d637f403

Rephrased, if you're using the Google Play Store for app distribution, then the .aab and (local) .apk file sizes are less important. Instead, you'd want to track the .apk size specific to your device, which you can get by using bundletool

jonpryor avatar May 20 '24 19:05 jonpryor