android包大小,应用程序包大小

应用程序包大小Application Package Size

02/05/2018

本文内容

本文将讨论 Xamarin.Android 应用程序包的组成部分,以及可用于在开发的调试和发布阶段进行高效包部署的相关策略。This article examines the constituent parts of a Xamarin.Android application package and the associated strategies that can be used for efficient package deployment during debug and release stages of development.

概述Overview

Xamarin.Android 可使用各种机制来最大程度地减小包的大小,同时保持高效调试和发布部署过程。Xamarin.Android uses a variety of mechanisms to minimize package size while maintaining an efficient debug and release deploy process. 在本文中,我们将了解 Xamarin.Android 版本和调试部署工作流,并了解 Xamarin.Android 平台如何确保我们生成和发布小的应用程序包。In this article, we look at the Xamarin.Android release and debug deployment workflow and how the Xamarin.Android platform ensures that we build and release small application packages.

版本包Release Packages

若要提供完全包含的应用程序,包必须包含应用程序、关联库、内容、Mono 运行时以及所需的基类库 (BCL) 程序集。To ship a fully contained application, the package must include the application, the associated libraries, the content, the Mono runtime, and the required Base Class Library (BCL) assemblies. 例如,如果我们使用默认的“Hello World”模板,则完整的包生成内容将如下所示:For example, if we take the default “Hello World” template, the contents of a complete package build would look like this:

5697aa736b9e74e68f91d1a63d5851fe.png5697aa736b9e74e68f91d1a63d5851fe.png

15.8 MB - 比我们所需的下载大小要大。15.8 MB is a larger download size than we’d like. 问题在于 BCL 库,因为它们包含 mscorlib、系统和 Mono.Android,以此提供大量的必需组件来运行应用程序。The problem is the BCL libraries, as they include mscorlib, System, and Mono.Android, which provide a lot of the necessary components to run your application. 但是,它们还在应用程序中提供了可能不使用的功能,因此最好将这些组件排除在外。However, they also provide functionality that you may not be using in your application, so it may be preferable to exclude these components.

当我们构建用于分发的应用程序时,会执行一个称为“链接”的过程来检查应用,并移除不直接使用的任何代码。When we build an application for distribution, we execute a process, known as Linking, that examines the application and removes any code that is not directly used. 此过程类似于垃圾回收为堆分配内存提供的功能。This process is similar to the functionality that Garbage Collection provides for heap-allocated memory. 但是,与在对象上操作不同,链接将在代码上运行。But instead of operating over objects, linking operates over your code. 例如,System.dll 中有一个完整的命名空间,用于发送和接收电子邮件,但是,如果应用程序不使用此功能,那么该代码只会浪费空间。For example, there is a whole namespace in System.dll for sending and receiving email, but if your application does not make use of this functionality, that code is just wasting space. 在 Hello World 应用程序上运行链接器之后,现在我们的包如下所示:After running the linker on the Hello World application, our package now looks like this:

0b973cdd4db9126e6c0473733f99a0bc.png0b973cdd4db9126e6c0473733f99a0bc.png

正如我们所看到的,这会移除大量未使用的 BCL。As we can see, this removes a significant amount of the BCL that was not being used. 请注意,BCL 的最终大小要取决于实际使用的应用程序。Note that the final BCL size is dependent on what the application actually uses. 例如,如果我们查看一个名为 ApiDemo 的更大的示例应用程序,会看到 BCL 组件的大小增加了,因为 ApiDemo 使用的 BCL 数量要大于 Hello,World:For example, if we take a look at a more substantial sample application called ApiDemo, we can see that the BCL component has increased in size because ApiDemo uses more of the BCL than Hello, World does:

b74d17af8a9a307b0f0d092a38db9c5f.png

如此处所示,应用程序包大小通常要比应用程序及其依赖项大约要大 2.9 MB。As illustrated here, your application package size will generally be about 2.9 MB larger than your application and its dependencies.

调试包Debug Packages

针对调试版本的处理操作略有不同。Things are handled slightly differently for debug builds. 当重新部署到设备时,应用程序需要尽可能快,因此,我们优化了调试包的部署速度,而不是大小。When redeploying repeatedly to a device, an application needs to be as fast as possible, so we optimize debug packages for speed of deployment rather than size.

Android 在复制和安装包方面相对较慢,因此,我们希望包大小尽可能地小。Android is relatively slow to copy and install a package, so we want the package size to be as small as possible. 如上所述,最大程度降低包大小的一个可行方法就是通过链接器。As we discussed above, one possible way to minimize package size is via the linker. 但是链接速度比较慢,而且我们通常只想要部署应用程序自上次部署以来有更改的部分。However, linking is slow and we generally want to deploy only the parts of the application that have changed since the last deployment. 为实现此目的,我们将应用程序从核心 Xamarin.Android 组件中分离了出来。To accomplish this, we separate our application from the core Xamarin.Android components.

第一次在设备上进行调试时,我们复制了两个较大的包:共享运行时 和共享平台 。The first time we debug on device, we copy two large packages called Shared Runtime and Shared Platform. 共享运行时包含 Mono 运行时和 BCL,而共享平台包含 Android API 级别的特定程序集:Shared Runtime contains the Mono Runtime and BCL, while Shared Platform contains Android API level specific assemblies:

122c986b5ed8efedb69606c476cf8b75.png122c986b5ed8efedb69606c476cf8b75.png

复制这些核心组件只进行一次,因为这需要相当多的时间,但允许任何后续运行的应用程序在调试模式下使用它们。Copying these core components is only done once as it takes quite a bit of time, but allows any subsequent applications running in debug mode to utilize them. 最后,我们将复制小而快速的实际应用程序:Finally, we copy the actual application, which is small and quick:

3e6e239de6cb900803598a478043bae6.png

快速程序集部署Fast Assembly Deployment

“快速程序集部署” 生成选项可用于进一步减小调试安装包的大小,方法是通过在应用程序包中将程序集排除在外、直接在设备上安装程序集且只进行一次,并仅复制自上次部署以来有修改的文件。The Fast Assembly Deployment build option can be used to further decrease the size of the debug install package by not including the assemblies in the application's package, installing the assemblies directly on the device only once and only copying over files that have been modified since the last deployment.

若要启用“快速程序集部署” ,请执行以下操作:To enable Fast Assembly Deployment, do the following:

右键单击“解决方案资源管理器”中的 Android 项目,并选择“选项” 。Right click on the Android Project in the Solution Explorer and select Options.

在“项目选项”对话框中,选择“Android 版本” :From the Project Options dialog select Android Build :

55cc962a386756ab2b5fb3e693f10cad.png

选中“使用共享 Mono 运行时”复选框 和“快速程序集部署” 复选框:Check the Use shared Mono runtime checkbox and the Fast assembly deployment checkboxes:

7731ec4f87ebcd5ada6bfd949e548264.png

单击“确定” 按钮,以保存所做的更改并关闭“项目选项”对话框。Click the OK button to save the changes and close the Project Options dialog.

下一次构建应用程序进行调试时,将直接在设备上安装程序集(如果尚未安装),并且会在设备上安装一个较小的应用程序包(不包括程序集)。The next time the application is built for debug, the assemblies will be installed directly on the device (if they haven't already been) and a smaller application package (that does not include the assemblies) will be installed on the device. 这将缩短部署应用程序更改并运行测试所需的时间。This will shorten the time it takes to get changes to the application up and running for testing.

通过持久长时间首次部署共享运行时和共享平台,每次对应用程序进行更改时,都可以快速轻松地部署新版本,以便拥有一个快速更改/部署/运行周期。By enduring the long first deploy of the shared runtime and shared platform, every time we make a change to the application, we can deploy the new version quickly and painlessly, so we can have a fast change/deploy/run cycle.

总结Summary

本文将探讨 Xamarin.Android 版本和调试配置文件打包的各个方面。In this article we examined the facets of Xamarin.Android Release and Debug profile packaging. 此外,我们还讨论了 Android 平台 Mono 用于在开发的调试和发布阶段进行高效包部署的相关策略。Additionally, we looked at the strategies that the Mono for Android platform uses to facilitate efficient package deployment during debug and release stages of development.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值