Skip to content

The APK with .NET 7 Android is twice the size compared to compiling with Xamarin.Android. #8212

Closed

Description

@fsaffioti

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

.net 7 android

Description

I am upgrading my app from Xamarin.Android to .NET 7, but the APK/AAB size is twice as large as the one built with Xamarin.Android.

Building and archiving the APK with .NET 7 (dotnet publish myapp.csproj -f net7.0-android -c Release) or from VSMac 17 results in a size that is twice as large as the APK/AAB built and archived with Xamarin.Android 13.

how can i reduce apk/aab, in xamarin.android is 30mb in .net7 android is 65mb ?

Xamarin Android project configuration

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{782BE5B1-8B1B-4D8F-B154-2C003ABA8504}</ProjectGuid>
    <ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <TemplateGuid>{84dd83c5-0fe3-4294-9419-09e7c8ba324f}</TemplateGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Myapp.Droid</RootNamespace>
    <AssemblyName>Myapp.Droid</AssemblyName>
    <FileAlignment>512</FileAlignment>
    <Deterministic>True</Deterministic>
    <AndroidApplication>True</AndroidApplication>
    <AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
    <AndroidResgenClass>Resource</AndroidResgenClass>
    <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
    <TargetFrameworkVersion>v13.0</TargetFrameworkVersion>
    <AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
    <MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
    <MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
    <AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
    <AndroidUseAapt2>true</AndroidUseAapt2>
    <AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugSymbols>True</DebugSymbols>
    <DebugType>portable</DebugType>
    <Optimize>True</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AndroidManagedSymbols>true</AndroidManagedSymbols>
    <AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
    <AndroidLinkMode>SdkOnly</AndroidLinkMode>
    <EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
  </PropertyGroup>

vs .net7-android configuration

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<TargetFramework>net7.0-android</TargetFramework>
		<SupportedOSPlatformVersion>27</SupportedOSPlatformVersion>
		<OutputType>Exe</OutputType>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
		<ApplicationId>myapp.Android</ApplicationId>
		<ApplicationVersion>1</ApplicationVersion>
		<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
	</PropertyGroup>

	<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
		<AndroidManagedSymbols>true</AndroidManagedSymbols>
		<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
		<AndroidLinkMode>SdkOnly</AndroidLinkMode>
		<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
		<Optimize>True</Optimize>
	</PropertyGroup>

nuget packages used in main project

	<ItemGroup>
		<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.3" />
		<PackageReference Include="Xamarin.AndroidX.Lifecycle.Common" Version="2.6.1.3" />
		<PackageReference Include="Xamarin.Firebase.Messaging" Version="123.1.2.2" />
		<PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.8.1.3" />
		<PackageReference Include="Xamarin.AndroidX.ConstraintLayout" Version="2.1.4.6" />
		<PackageReference Include="Xamarin.AndroidX.Lifecycle.Process" Version="2.6.1.3" />
		<PackageReference Include="Xamarin.Google.Android.Material" Version="1.9.0.2" />
		<PackageReference Include="Xamarin.AndroidX.Fragment" Version="1.6.0.1" />
		<PackageReference Include="Xamarin.Controls.SignaturePad" Version="3.0.0" />
		<PackageReference Include="Xamarin.AndroidX.Navigation.Fragment" Version="2.6.0.1" />
	</ItemGroup>

nuget packages used in submodules projects

	<ItemGroup>
		<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
		<PackageReference Include="Xamarin.Essentials" Version="1.7.7" />
	</ItemGroup>
	<ItemGroup>
		<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.3" />
		<PackageReference Include="Xamarin.AndroidX.RecyclerView" Version="1.3.0.3" />
		<PackageReference Include="Xamarin.Google.Android.Material" Version="1.9.0.2" />
		<PackageReference Include="Com.Airbnb.Android.Lottie" Version="4.2.2" />
		<PackageReference Include="System.Reactive" Version="6.0.0" />
		<PackageReference Include="Refractored.GifImageView" Version="2.0.0" />
		<PackageReference Include="Mindscape.Raygun4Net" Version="5.6.0" />
	</ItemGroup>
	<ItemGroup>
		<PackageReference Include="Xamarin.AndroidX.Camera.Camera2" Version="1.2.3.1" />
		<PackageReference Include="Xamarin.AndroidX.ConstraintLayout" Version="2.1.4.6" />
		<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.3" />
		<PackageReference Include="Xamarin.Google.Android.Material" Version="1.9.0.2" />
		<PackageReference Include="Xamarin.AndroidX.Camera.View" Version="1.2.3.1" />
		<PackageReference Include="Xamarin.AndroidX.Camera.Lifecycle" Version="1.2.3.1" />
	</ItemGroup>


	<ItemGroup>
		<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
	</ItemGroup>
	<ItemGroup>
		<PackageReference Include="SQLiteNetExtensions" Version="2.1.0" />
		<PackageReference Include="NLog" Version="5.2.2" />
		<PackageReference Include="libphonenumber-csharp" Version="8.13.16" />
		<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.2" />
		<PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.2" />
		<PackageReference Include="Xamarin.Essentials" Version="1.7.7" />
		<PackageReference Include="SkiaSharp" Version="2.88.3" />
		<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	</ItemGroup>

runtimeconfiguration.json

{
  "runtimeOptions": {
    "tfm": "net7.0",
    "frameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "7.0.0"
      },
      {
        "name": "Microsoft.Android",
        "version": ""
      }
    ],
    "configProperties": {
      "Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability": false,
      "System.AggressiveAttributeTrimming": true,
      "System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization": false,
      "System.Diagnostics.Debugger.IsSupported": false,
      "System.Diagnostics.Tracing.EventSource.IsSupported": false,
      "System.Globalization.Invariant": false,
      "System.Net.Http.EnableActivityPropagation": false,
      "System.Net.Http.UseNativeHttpHandler": true,
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Reflection.NullabilityInfoContext.IsSupported": false,
      "System.Resources.ResourceManager.AllowCustomResourceTypes": false,
      "System.Resources.UseSystemResourceKeys": true,
      "System.Runtime.InteropServices.BuiltInComInterop.IsSupported": false,
      "System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting": false,
      "System.Runtime.InteropServices.EnableCppCLIHostActivation": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
      "System.StartupHookProvider.IsSupported": false,
      "System.Threading.Thread.EnableAutoreleasePool": false,
      "System.Text.Encoding.EnableUnsafeUTF7Encoding": false,
      "Xamarin.Android.Net.UseNegotiateAuthentication": false
    }
  }
}

Steps to Reproduce

dotnet publish myapp.csproj -f net7.0-android -c Release

Did you find any workaround?

no

Relevant log output

No response

Activity

  1. added
    Area: App+Library Build
    needs-triage
    on Jul 24, 2023
  2. jonathanpeppers commented on Jul 24, 2023

    @jonathanpeppers
    Member

    This is very similar to: dotnet/maui#9453 (comment)

    Google Play splits up app bundles per architecture. Are you comparing the numbers Google Play shows for your app?

    Alternatively, you can build for one architecture as mentioned at the link above. It will give you a rough idea of what will happen on Google Play. Let us know your results from doing this, thanks!

  3. added
    need-info
    and removed
    needs-triage
    on Jul 24, 2023
  4. fsaffioti commented on Jul 24, 2023

    @fsaffioti
    Author

    @jonathanpeppers thanks for quick response and your help is really appreciated

    Actually, I was mostly comparing the APK (the AAB is slightly smaller than the APK) and I wasn't looking at the APK size on Google Play. Instead, I was considering the APK size on Appcenter with our current settings, as we distribute the app through Appcenter to QA.

    for the app in Xamarin.Android on Google play and Appcenter <AndroidSupportedAbis>armeabi-v7a;x86;arm64-v8a;x86_64</AndroidSupportedAbis>
    apk is 34 MB
    aab is 30 MB

    for the app in .net7 Android with dotnet publish myapp.csproj -f net7.0-android -c Release -r android-arm64 AotAssemblies && AndroidEnableProfiledAot blank
    apk 33.8 MB
    aab 30 MB

    for the app in .net7 Android with dotnet publish myapp.csproj -f net7.0-android -c Release -r android-arm64 and AndroidEnableProfiledAot =false && AotAssemblies blank
    apk 53.8 MB
    aab 50 MB

    for the app in .net7 Android with with dotnet publish myapp.csproj -f net7.0-android -c Release and AndroidEnableProfiledAot =true && AotAssemblies blank
    apk 68.7
    aab 64.4

    for the app in .net7 Android with with dotnet publish myapp.csproj -f net7.0-android -c Release and AndroidEnableProfiledAot =false && AotAssemblies blank
    apk 140.4
    aab 136.1

    for the app in .net7 Android with dotnet publish myapp.csproj -f net7.0-android -c Release -r android-arm64 AotAssemblies=false && AndroidEnableProfiledAot blank
    apk 30.2 MB
    aab 26.8 MB

    Summarize
    with android-arm64 yes i have the same size probably the default in xamarin.android is just one ABI

    then i have noticed with AndroidEnableProfiledAot =true the APK/AAB is very large but documentation says exactly the contrary .
    image

    what's the different between AotAssemblies && AndroidEnableProfiledAot ?
    Should both be true for production? should both be blank?

  5. ghost removed
    need-info
    on Jul 24, 2023
  6. ghost added
    need-attention
    on Jul 24, 2023
  7. jonathanpeppers commented on Jul 24, 2023

    @jonathanpeppers
    Member

    We try to set the best defaults for you, where you can just leave the settings blank. Release builds by default will be:

    • AotAssemblies/RunAOTCompilation (both do the same thing) are true. This enables AOT compilation, resulting in a larger app.
    • AndroidEnableProfiledAot is true. This only AOT's a portion of your app, code that would be commonly called at startup or in your app's main project. It makes an AOT'd app a bit smaller, but no effect if AOT is off.

    For your numbers above, make sure you compare a Xamarin.Android & .NET 7 app with the same values for AotAssemblies and AndroidEnableProfiledAot. Then AndroidSupportedAbis=arm64-v8a is the equivalent of -r android-arm64.

  8. gabsamples6 commented on Sep 18, 2023

    @gabsamples6

    @jonathanpeppers just want to say that same app - the Maui version has less code

    • xamarin 37.8 mb
    • maui 67.68

    I have looked at all the properties that are set and I cannot spot a difference. There is definetely an issue there...

    Any suggestions on what properties I should set to minimize size of the app apart from AOT?

  9. jonathanpeppers commented on Sep 18, 2023

    @jonathanpeppers
    Member

    @gabsamples6 are these numbers from Google Play? Or are you compiling a single architecture to estimate?

    The comment above explains further: #8212 (comment)

  10. added
    need-info
    and removed
    need-attention
    on Sep 18, 2023
  11. microsoft-github-policy-service commented on Sep 18, 2023

    @microsoft-github-policy-service
    Contributor

    Hi @fsaffioti. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

  12. gabsamples6 commented on Sep 18, 2023

    @gabsamples6

    @jonathanpeppers these are not google play because we cannot ship the maui app till critical bugs are fixed :)..

    We use Azure Pipeline to deploy to appcenter (for dev -uat etc) and those are taken from appcenter...

    Interestingly - this is something I wanted to ask and I googled but could not find the answer and would be grateful.

    in Xamarin forms we could set <AndroidSupportedAbis>armeabi-v7a;arm64-v8a</AndroidSupportedAbis>
    in maui there is no UI that I can see to set the architectures

    So now I am not sure what architecture I am building... is it something that I have to define in the pipeline and how - since there is no UI or do I just amend the csproj file manually?

    We want to get to the bottom of this on why the Maui app size is nearly double.. and fix whatever we are doing wrong..

    Appreciate your input

  13. added
    need-attention
    and removed
    need-info
    on Sep 18, 2023
  14. jonathanpeppers commented on Sep 18, 2023

    @jonathanpeppers
    Member

    Yes, as mentioned here:

    It is the RuntimeIdentifier(s) property. By default it is RuntimeIdentifiers=android-arm;android-arm64;android-x86;android-x64.

    If you want to compare sizes Xamarin vs .NET 7, build each one for a single architecture like ARM64. This gives you an idea before submission to Google Play (final source of truth).

  15. removed
    need-attention
    on Mar 20, 2024
  16. added
    possibly-stale
    on May 17, 2024
  17. gabsamples6 commented on Jun 2, 2024

    @gabsamples6

    There is no doubt at all that a Maui app is way bigger than a xamarin one

  18. added
    need-attention
    and removed
    possibly-stale
    on Jun 2, 2024
  19. locked and limited conversation to collaborators on Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App+Library Buildneed-attention

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions