Xamarin - apk 应用大小

18

因此,在使用Xamarin.Forms开发应用程序的所有辛勤工作之后,当我今天尝试创建发布版本时,我惊讶地发现应用程序大小约为25MB-31MB(在SDK仅链接、ProGuard、为每个ABI创建apk后)。我的资源文件夹只有几KB大小。实际上,调试模式下的apk只有5MB,并且我很高兴看到这一点。后来我意识到,这是因为“使用共享运行时”选项,我们不应该在发布模式下使用它。

然后我尝试创建一个空白的Xamarin.Android应用程序,但是链接SDK和用户程序集、ProGuard、为每个abi创建的发布版仍然是8MB到13MB左右。

根据以下链接,最小大小为2.9MB的Hello World应用程序,但我无法创建这样大小的应用程序。https://developer.xamarin.com/guides/android/advanced_topics/application_package_sizes/

对于我的应用程序和我创建的空白应用程序,必要的dll似乎很大(例如mscorlib.dll是2.2MB等等,而链接后将变成1MB),这是提取apk后我在程序集文件夹中看到的情况。 enter image description here

在最近的微软技术会议中,我了解到“9 News”应用程序(下面链接)是使用Xamarin构建的,并且创建者出现在台上。但是我对它的应用程序大小感到惊讶。它只有5.85MB。我不确定如何实现这一点?

有人能帮帮我吗?

https://play.google.com/store/apps/details?id=nineNewsAlerts.nine.com

我也很想知道微软是否在做一些改进以提高应用包的大小?或者如果他们将.NET Core引入Xamarin,这个问题是否会得到解决?


2
请注意:您链接的应用程序大小为19.76MB(5.8MB是下载大小,而不是磁盘大小...)。 - SushiHangover
2
正确的,我只关心下载大小。用户在安装应用程序之前在Play Store上看到的大小。此外,我注意到一旦安装了每个应用程序,即使是使用Java创建的应用程序,它们在磁盘上的大小也会更大。因此,这并不一定重要。 - My Helper
3
哇!我刚试着将我的应用发布到应用商店,并且下载大小只显示为9.x兆字节,这太神奇了。我原以为在创建APK时看到的大小会与在Play商店上显示的大小一样。 我正在进行更多的优化,以看是否能进一步减小APK的大小,并会回来在这里发布我的结果。 - My Helper
3个回答

14
Xamarin Android APK 文件比通常的文件大,因为它们包括将 C# 代码转换为 Java 代码,并在 Android 操作系统中运行的 Xamarin 库。
使用以下选项,您可以减小 APK 文件大小:
  • 配置: Active (Release).
  • 平台: Active (Any CPU)。这些选项是为了构建应用程序的 APK 文件以供Google Play Store使用。
  • 使用共享运行时: false。如果设置为 true,则 APK 将使用 Mono 运行时进行执行。在通过 USB 进行调试时,Mono 运行时会自动安装,但在发布版 APK 中不会安装。如果设备上未安装 Mono 运行时并且此选项在发布版 APK 中设置为 true,则应用程序会崩溃。
  • 为每个所选 ABI 生成一个软件包 (.apk): false。为了兼容性,请创建尽可能多的平台版本的 APK 文件。
  • 启用 Multi-Dex: true,但如果您的应用程序不是很复杂(即具有少于 65536 个变量或方法),则可以将其设置为 false (请参见此处)。
  • 启用开发人员仪表盘 (调试和分析): 对于发布版 APK,请设置为false
  • 链接: SDK 和用户程序集。这将使 Xamarin 编译器从 SDK 和您的代码中删除所有未使用的类,从而减小 APK 文件的大小。
  • 支持的架构: 选择全部,以确保兼容性。
以下是示例截图:

enter image description here


我认为APK的大小今天不是什么问题,因为许多应用程序都有很大的大小;例如,WhatsApp大约是50MB(大)。 - Alexandre
2
抱歉,但我对我的问题中提到的大多数/全部内容都有所了解。我的问题集中在两个链接/要点上。
  1. 为什么“Hello World”应用程序不是文档中说的2.9 MB?
  2. 对于“9 News”是如何实现那种大小的,您有什么想法/任何输入吗? (链接在我的问题中)
- My Helper

7
这是我在Google Play控制台中Xamarin.Forms应用程序大小的统计数据。您可以看到,一旦我认真对待这个问题并远离默认的Xamarin设置,它的大小就会大幅下降。

enter image description here

除了通常在谷歌搜索主题时找到的措施(链接、关注自己的资源、每个ABI的APK等),以下是我自己发现的一些小技巧和问题:

  • Enabling Managed TLS Provider shoves of ~1MB - by default you have 'Native TLS 1.2' option selected in Project Options -> Android Options -> Advanced button -> SSL/TLS implementation drop down. Managed TLS is version 1.1 only, might be an issue from someone
  • Upgrade to Xamarin.Forms 4.1 gives ~1MB reduction (comparing to 3.x and 4.0) - they removed some of unused yet referenced Android Compatability Libraries
  • Enabling aapt2 packager gives ~1MB reduction for App Compat libs resources only (those referenced and included by Xamarin.Forms). Might give more if you have many of your own resources. You can have aapt2 further striping down resources by using the following extra args in you Android's .csproj file:

    <PropertyGroup>
       <AndroidUseAapt2>true</AndroidUseAapt2>
       <AndroidAapt2LinkExtraArgs>--no-version-vectors -c en-rUS</AndroidAapt2LinkExtraArgs>
    </PropertyGroup>
    
  • Turning on D8 and R8 gives another ~1MB for a small Xamarin.Forms app (Project Options -> Android Options or .csproj)

    <PropertyGroup>
        <AndroidDexTool>d8</AndroidDexTool>
        <AndroidLinkTool>r8</AndroidLinkTool>
    </PropertyGroup>
    
  • SkiaSharp 1.60.3 gives ~2MB economy per architecture, compared to more recent versions (e.g. 1.68.0). It's worthwhile looking out for dependencies and their versions bloating your APK.

  • Disabling AoT (if you had it previously enabled, it's off by default) can easily cut your app size in half. Turning on Ahead-of-Time complication could have been a deliberate step to improve your app's start-up performance. There's a clear trade-off, either you go with AoT and have huge app size OR you don't have it and keep size minimal with slow app launches. One of the points to consider when deciding on the trade-off... Most ASO articles (as well as Google Play Console) mention how important it is to have app size small to increase installs conversion. Clearly when browsing Google Play it's app download size that is displayed, not app start time.

  • Adjusting AoT to minimize it's footprint, add the following in .csproj for your Release configuration:

    <PropertyGroup>
        <AotAssemblies>true</AotAssemblies>
        <AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments>
    </PropertyGroup>
    
  • Doing Partial AoT. By default AoT goes through all assemblies resolved by the project and adds 30+ .so files in the /lib directory of your APK. There're numerous app compat libs, BCL and system libs. One might want to have control over which assemblies to put through AoT and which ones to ignore in order find a sweet spot between performance and app size. Well, there's no such thing available as standard feature (at least I didn't find one). You can play with Xamarin build process by making changes to Xamarin.Android.Common.targets - you can find some insights into this process here. I found my golden mean by AoT'ing only three libs (1 my own XF shared UI lib and 2 XF libs):

    <_ResolvedUserAssemblies2 Include="Tmp/android/assets/Xamarin.Forms.Core.dll;Tmp/android/assets/Saplin.CPDT.UICore.dll;Tmp/android/assets/Xamarin.Forms.Platform.Android.dll;" />

P.S.: 启用链接(SDK 和用户程序集)- 如果您广泛使用 XAML 和绑定,90% 的可能性是您会发现应用程序出现故障。将自己的库添加到链接器例外 - XF 类库托管您的 UI,任何类库您创建和可能使用反射的类库。
P.P.S: 通过我的缩小努力,部分 AoT 和支付异步/延迟加载 Xamarin.Forms UI(herehere),我实现了显着的应用程序大小减小(请参见上图),并且应用程序启动时间也有了显着的改善(在 Nokia N1 上通过 ADB 测量):
  • 无 AOT,同步 UI(ms):3613、3586、3576
  • 部分 AOT,异步 UI(ms):2202、2255、2258

-1

我了解你在创建移动应用程序时遇到的问题,你总是希望创建小型的apk文件,以便用户可以轻松下载和使用。

为此,请尝试减少项目中的代码重复,并减小所有图像的大小。

请按照以下步骤操作,这将有助于减小您的apk文件大小:

  1. 右键单击droid属性。
  2. 打开Android选项 ->
  3. 点击打包,然后勾选每个所选ABI生成一个包(.apk)。
  4. 同时检查Multi-Dex和proguard选项。
  5. 现在转到链接器选项,在链接下拉菜单中选择仅SDK组件。
  6. 现在转到高级选项卡-
  7. 检查支持的架构下的所有选项。
  8. 保存所有设置并将您的项目保持在发布模式下,清理并构建您的项目,然后您就可以创建减小后的apk文件大小。

使用以下链接获取更多信息。

http://motzcod.es/post/112072508362/how-to-keep-your-android-app-size-down


实际上,我已经按照我在问题中提到的做了所有这些事情! - My Helper
您已经使用了Linking SDK和用户程序集。建议选择仅使用SDK程序集选项。这应该可以解决问题。另外,有时在压缩apk文件时,功能会中断,因此需要重新创建。希望这可以帮助您。 - Prabhat Maurya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接