xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] Android APK is 1MB larger after upgrading Xamarin.Forms from SR4 to SR5 #14727

Open holecekp opened 3 years ago

holecekp commented 3 years ago

Description

I have upgraded Xamarin.Forms 5.0 to SR5 in my project and I was surprised that the APK file became suddenly almost 1 MB larger.

I have tried also to create a new shell project using the default template in Visual Studio and I can confirm that also for this project the size grows rapidly just by upgrading the Xamarin.Forms Nuget to the latest SR5 version. I have set linker to link everything and enabled r8 to get the smallest APK size possible. I had to add Preserve attributes on a few places to make the project run work without crashing. Here is a comparison of the sizes:

Xamarin.Forms 5.0.0.2083: 17.8 MB Xamarin.Forms 5.0.0.2125: 18.6 MB

The difference is almost 1 MB for the default project created by Visual Studio (and it is even more for more complex apps). The biggest increase seems to be in the classes.dex file.

What caused this? Is this extra size added by some external dependencies? Or is it a linker problem?

Steps to Reproduce

  1. Create an empty shell project using the default Visual Studio template.
  2. Archive it.
  3. Update Xamarin.Forms Nuget, archive it again and observe the difference of the APKs sizes.

Expected Behavior

All service releases should produce APKs with similar sizes as they does not bring any new features.

Actual Behavior

Upgrading Xamarin.Forms to SR5 increases APK size significantly.

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Community 2019 Version 16.11.5 VisualStudio.16.Release/16.11.5+31729.503 Microsoft .NET Framework Version 4.8.04084 Installed Version: Community Visual C++ 2019 00435-60000-00000-AA088 Microsoft Visual C++ 2019 ASP.NET and Web Tools 2019 16.11.75.64347 ASP.NET and Web Tools 2019 ASP.NET Web Frameworks and Tools 2019 16.11.75.64347 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 16.11.75.64347 Azure App Service Tools v3.0.0 Azure Functions and Web Jobs Tools 16.11.75.64347 Azure Functions and Web Jobs Tools C# Tools 3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Microsoft Library Manager 2.1.113+g422d40002e.RR Install client-side libraries easily to any web project Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards Microsoft Visual Studio Tools for Containers 1.2 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container. Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package Mono Debugging for Visual Studio 16.10.15 (552afdf) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 5.11.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info Razor (ASP.NET Core) 16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725 Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 16.0.62107.28140 Microsoft SQL Server Data Tools TypeScript Tools 16.0.30526.2002 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 16.11.0-beta.21322.6+488cc578cafcd261d90d748d8aaa7b8b091232dc Microsoft Visual F# Tools Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio. Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Foo 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio Xamarin 16.11.000.190 (d16-11@2391ed9) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 16.11.0.17 (remotes/origin/11e0001f0b17269345e80b58fb3adf1ba4efe2cd@11e0001f0) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 16.10.5 (355b57a) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 11.4.0.5 (d16-11/7776c9f) Xamarin.Android Reference Assemblies and MSBuild support. Mono: c633fe9 Java.Interop: xamarin/java.interop/d16-11@48766c0 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.35.4@85460d3 Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@683f375 Xamarin.iOS and Xamarin.Mac SDK 15.0.0.8 (0796d78dc) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Workaround

Downgrade Xamarin.Forms to the nearest older version that works for you (this is SR3 in my case, because I had problems with SR4).

jfversluis commented 3 years ago

Probably due to #14101 then, I wouldn't know what else @jonathanpeppers is this something that sounds familiar to you?

jonathanpeppers commented 3 years ago

Can someone share the .apk files of before and after?

If you don't want to actually share the .apk, you can also use this tool: https://www.nuget.org/packages/apkdiff/

> apkdiff -f before.apk after.apk

It prints out the contents of what actually changed.

I would also only compare Release builds you're shipping.

holecekp commented 3 years ago

I have not downloaded akpdiff yet (I will post its output later), but I have used APK analyzer from Android Studio which can also compare the newer version with the old one. The biggest change is in classes.dex. It is a release build with linker and r8 enabled: sr4_vs_sr5_compare

jonathanpeppers commented 3 years ago

So if classes.dex increases, that just means there is now more Java code in there.

So are the newer AndroidX classes just larger? Can you share the before/after classes.dex files? I use a tool called dexdump to compare. This looks like a screenshot of Android Studio, I'm not sure if it has a "diff view" between two .apk files.

holecekp commented 3 years ago

I am sending the dex files for both Xamarin.Forms versions: dex files.zip

I am also including the source for the project. But this is just the default project created by Visual Studio template with updated Nugets and a few settings for smaller size (linker, r8): EmptyXamarinSizeTest.zip

I have just noticed that when I am upgrading the Nuget to SR5, Visual Studio shows a dialog with changes preview. It lists not not only updates to AndroidX packages but also a few new ones:

Xamarin.AndroidX.Concurrent.Futures
Xamarin.AndroidX.ConstrainedLayout
Xamarin.AndroidX.ConstrainedLayout.Core
Xamarin.AndroidX.DynamicAnimation
Xamarin.AndroidX.Tracing.Tracing

Maybe these are the reason for the size increase. However, 1 MB is quite a lot when a new service release with only bug fixes and no new functionality is installed. So I am wondering if the things added by SR5 are really code that is used or something unused that should be linked out.

jonathanpeppers commented 3 years ago

Yes, there appear to be many more java types:

--class_defs_size     : 2788
++class_defs_size     : 3475

Here is a list of the types, and they just seem to match the newer AndroidX libraries:

You said you are using AndroidLinkTool=r8, is it possible you have some proguard rules that are keeping more classes than they should?

holecekp commented 3 years ago

I have checked that there is no custom proguard rules file. It is a starter project created by Visual Studio template with the following steps: 1) Create a new project of type "Mobile App (Xamarin.Forms)" in Visual Studio 2019 2) One screen of the new project wizard asks which template should be used. I kept the default choice (Flyout) here. 3) I have modified just two settings in the properties for Android project: Code shrinker = r8 and Linking = Sdk and User Assemblies. I had to add Preserve attribute on some places after changing these settings. 4) Finally, I have updated the Nugets. 5) I have built the release version, checked it in the emulator, and archived it into APK.

jonathanpeppers commented 3 years ago

I think the newer AndroidX libraries are just larger. What is the size difference when you turn off r8? Are we sure it's actually shrinking things?

holecekp commented 3 years ago

I checked that R8 is doing something. When I turn it off, the APK is 756 kB larger (19,3 MB in total).