Xamarin-APK应用程序大小 [英] Xamarin - apk App Size

查看:220
本文介绍了Xamarin-APK应用程序大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在使用Xamarin.Forms开发应用程序的所有艰苦工作之后,当我今天尝试创建发行版时,我震惊地看到应用程序大小为〜25MB-〜31MB(在仅SDK链接之后,ProGuard ,为每个abi创建apk).我的资源文件夹仅以KB为单位.在调试模式下,apk实际上只有5MB,很高兴看到它.后来我意识到这是因为我们不应该在发布模式下使用使用共享运行时"选项.

然后,我尝试创建一个空白的Xamarin.Android应用,但该版本使用Linking SDK&每个abi的用户程序集,ProGuard,APK仍在〜8MB至〜13MB之间.

通过以下链接,Hello World应用程序的最小大小为2.9MB,但是我无法创建该大小. https://developer.xamarin.com/guides/android/advanced_topics/application_package_sizes/

对于我的应用程序和我创建的空白应用程序,所需的dll似乎很高(例如mscorlib.dll为2.2mb等,链接后链接所指的位置将变为1 mb),这就是我所看到的提取apk后的Assembly文件夹

在最近的一次Microsoft技术会议中,我碰巧知道"9 News"应用程序(下面的链接)是使用xamarin构建的,并且创作者都在舞台上.但是,我对它的应用程序大小感到惊讶.它只有5.85 MB.我不确定如何实现?

有人可以帮助我吗?

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

我也很好奇Microsoft是否在做一些事情来改善应用程序包的大小?或者,如果他们将.NET Core引入xamarin,这将得到解决吗?

解决方案

Xamarin Android APK文件的大小比平常大,因为它们包含Xamarin库,该库将C#代码转换为Java代码,以便在Android OS中运行.

使用以下选项,您可以减小APK的大小:

  • 配置:活动(发布).
  • 平台:活动(任何CPU).这些对于为Google Play商店构建APK是必需的.
  • 使用共享运行时:.如果将其设置为true,则APK将使用Mono Runtime执行.通过USB调试时会自动安装Mono Runtime,但不会在Release APK中安装.如果设备中未安装Mono Runtime,并且在Release APK中将此选项设置为true,则该应用程序将崩溃.
  • 为每个选定的ABI生成一个软件包(.apk): false .出于兼容性考虑,请为尽可能多的平台创建APK.
  • 启用Multi-Dex : true ,但是如果您的应用不是很复杂(即,变量或方法少于65536个, 请参阅此处).
  • 启用开发人员检测(调试和配置文件):(对于Release APK).
  • 链接: SDK和用户程序集.这将使Xamarin编译器从SDK和您的代码中删除所有未使用的类,从而减小APK的大小.
  • 受支持的体系结构:出于兼容性原因,全选.

这是一个打印屏幕示例:

So, after all the hard-work developing the app using Xamarin.Forms, when I tried to create a release build today, I was shocked to see the app size is ~25MB - ~31MB (after SDK Only Linking, ProGuard, Creating apk for each abi). My resource folder is only in KBs. The apk actually was just 5MB in debug mode and was so happy to see it. I later realized that this is because of the option "Use Shared Runtime" which we are not supposed to use in Release Mode.

I then tried creating a blank Xamarin.Android app, but the release build with Linking SDK & User Assemblies, ProGuard, APK for each abi is still ~8MB to ~13MB.

Per the below link the minimum size is 2.9MB with the Hello World app, however I am unable to create such size. https://developer.xamarin.com/guides/android/advanced_topics/application_package_sizes/

For my app and the blank app I created, the necessary dlls seem to be high (example mscorlib.dll is 2.2mb etc, where as the link says after linking it will become 1 mb) Here is what I see as my assembly folder after extracting the apk

In one of the recent Microsoft tech conferences I happened to know that "9 News" app (link below) was built using xamarin, and the creators were present on the stage. However I was surprised with it's app size. It is only 5.85 MB. I am unsure how that is achieved?

Can any one help me with these?

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

I am also curious to know if Microsoft doing something to improve app package sizes? Or will this be resolved if they bring .NET core to xamarin?

解决方案

Xamarin Android APK files have a larger size than usual because they include the Xamarin libraries, that translate C# code to Java code, to run in Android OS.

Using the following options, you can reduce the APK size:

  • Configuration: Active (Release).
  • Platform: Active (Any CPU). These are necessary to build your APK for the Google Play Store.
  • Use Shared Runtime: false. If you set it to true, the APK will use Mono Runtime to execute. The Mono Runtime is installed automatically when debugging through USB, but not in the Release APK. If Mono Runtime is not installed in the device and this option is set to true in the Release APK, the app will crash.
  • Generate one package (.apk) per selected ABI: false. Create your APK for as many platforms as possible, for compatibility reasons.
  • Enable Multi-Dex: true, but you can set it to false if your app is not very complex (that is, has less than 65536 variables or methods, see here).
  • Enable developer instrumentation (debugging and profiling): false for Release APK.
  • Linking: SDK and User Assemblies. This will make the Xamarin compiler to remove all unused classes from SDK and your code, reducing the APK size.
  • Supported architectures: Select all, for compatibility reasons.

Here is a print screen example:

这篇关于Xamarin-APK应用程序大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆